-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Polygon2d.centroid #117
Polygon2d.centroid #117
Conversation
Tests pass for me locally, I wonder if there is a problem with ci. |
Yeah, that was unlucky timing, I broke CI briefly today - I'm doing some vacation hacking on a laptop that doesn't have Node (and therefore doesn't have I think things should be fixed now, so maybe merge |
@ianmackenzie I rebased with master so it works now! Wonder if this approach looks good to you? Then I can add an offset relative to the first point to improve precision. |
On my phone so I haven't fully looked through in detail, but I think there might be an issue in |
That would also mean you wouldn't have to reconstruct the first loop using |
@ianmackenzie good catch, will work to fix this |
@ianmackenzie I addressed this issue! It made me think about loops with only two points, but that is actually fine, because that doesn't change the sum, i.e. for |
Yeah, definitely makes sense to ignore degenerate loops with 0 or 1 points - I don't think you need to go into the math to see that a 'loop' with 0, 1 or frankly even 2 points has zero area and therefore zero 'mass' =) I think the rest of the code looks good, but would you be willing to add one more test that uses the existing triangulation code to check the centroid calculation? It should be possible (although much more expensive than your implementation!) to compute the centroid of a polygon by triangulating the polygon and then calculating the weighted centroid of all the resulting triangles. It should be possible to test that with something similar to the existing Quantity.sum (List.map Triangle2d.area triangles) you'll probably have to have something like (untested) triangles
|> List.map
(\triangle ->
Vector2d.from Point2d.origin (Triangle2d.centroid triangle)
|> Vector2d.scaleBy (Area.inSquareMeters (Triangle2d.area triangle))
)
|> List.foldl Vector2d.plus Vector2d.zero
|> Vector2d.scaleBy
(1 / Area.inSquareMeters (Quantity.sum (List.map Triangle2d.area triangles))) |
@ianmackenzie added the test! |
Thanks, looks great! Definitely an improvement on my hacky sample code =) Do you want to look at tweaking the calculation to use coordinate values relative to the first vertex? I think it should just end up meaning adding a couple extra |
@ianmackenzie sure, done in 0077089 |
Cool, looks good! I think this is ready to go then - OK to merge? |
@ianmackenzie sure, let’s merge! |
Coordination is definitely faster when we're in the same time zone, too bad I'm flying back to Toronto on Thursday morning 😛 |
Implemented polygon centroid according to the formula from here. Closes #64.
I wonder if the approach taken here is correct?
I haven't yet implemented the calculation relative to the first vertex. If the polygon is far from the origin, the precision may be bad.