Skip to content
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

mathUtil.polyArea does not produce accurate results #17

Open
JoeStrout opened this issue Dec 18, 2023 · 1 comment
Open

mathUtil.polyArea does not produce accurate results #17

JoeStrout opened this issue Dec 18, 2023 · 1 comment

Comments

@JoeStrout
Copy link
Owner

This came up as part of Advent of Code 2023, Day 18.

Given this polygon:

[[461938, 1], [461938, -56406], [818609, -56406], [818609, -919646],
 [1186329, -919646], [1186329, -1186328], [609066, -1186328], [609066, -356353], 
[497057, -356353], [497057, -1186328], [5411, -1186328], [5411, -500254], [0, -500254], [0, 1]]

...we get different answers using the built-in mathUtil.polyArea than we do with custom code implementing the Shoelace algorithm:

MathUtil.polyArea:  952408211456
Custom polygonArea: 952408144115

And the custom code's answer is correct. Here's the code for that:

polygonArea = function(verts)
    area = 0.0
    n = verts.len
    j = n - 1
    for i in range(0,n-1)
        area += (verts[j][0] + verts[i][0]) * (verts[j][1] - verts[i][1])
        j = i
    end for
    return abs(area / 2.0)
end function
@JoeStrout
Copy link
Owner Author

There are some hints that mathUtil.polyPerimeter may have issues, too. Better test thoroughly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant