alexbol99 / flatten-js

Javascript library for 2d geometry
MIT License
535 stars 56 forks source link

area() fails on polygon with arcs #178

Open Res42 opened 1 month ago

Res42 commented 1 month ago

Polygon.area() fails on my polygon with the following error Illegal Parameters

You can check the detailed error in the repro: https://codesandbox.io/p/sandbox/bold-shadow-w76pzg

If you uncomment line 22 in index.ts you can see the rendered polygon.

alexbol99 commented 1 month ago

Hello, @Res42 ,

I can suggest workaround: change definition of the second arc to start with positive Pi, not negative:

{
      pc: {
        x: 103.45458984375003,
        y: 1.0254856264438104,
        name: "point"
      },
      r: 132.68218260706254,
      startAngle: 3.141592653589793,
      endAngle: 3.05590208005791,
      counterClockwise: false,
      name: "arc"
    },

Then the arc will be consistent with the previous one. Anyway, it should not give an error, I'll try to fix this situation

Res42 commented 1 month ago

Hey @alexbol99!

Thanks for the quick response.

I created this polygon by subtracting a circle from a rectangle. I updated the https://codesandbox.io/p/sandbox/bold-shadow-w76pzg to show this.

I don't know if your workaround covers this case, because I have a dynamic polygon so I don't know if I can detect if the resulting polygon has inconsistent arches.

As I checked out things, I found if I reverse() the resulting polygon, then the error does not occur. So I think I will try this as a workaround:

try {
 return p.area();
} catch {
  return p.clone().reverse().area();
}