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

Contains point #4

Open
kylepritchard opened this issue Mar 31, 2022 · 2 comments
Open

Contains point #4

kylepritchard opened this issue Mar 31, 2022 · 2 comments

Comments

@kylepritchard
Copy link

kylepritchard commented Mar 31, 2022

Hi,

I am working through your book and enjoying it so far. Well written and reminding me of all the things I learnt during my engineering degree. When writing tests for the contains_point function for a generic polygon using winding number algorithm the algorithm seems to fail when a square/rectangle is entered as a polygon. Is this to be expected? In my mind it should work but I cant figure out why it doesn't unless an absolute value is generated from the angle sum.

@angelsolaorbaiceta
Copy link
Owner

Hi there!

Thanks a lot for your kind words. Happy to hear you're enjoying the book.

The case you're describing should definitely work; there might be a bug somewhere in the code.

Would you be so nice as to include a small snippet of code that reproduces the error you describe? I'm currently out from home without a laptop to work, but as soon as I get back I'll take a look at this.

Best,
Ángel

@kylepritchard
Copy link
Author

Hi,

I looked a little more today when time allowed and I was slightly wrong about the square/rectangle issue. The only issue I see is with the way the points of the polygon are listed. In my testing the polygon points were added moving clockwise through the polygon hence the angles always ended up negative which yields a failure as the test case is limited to positive angles. I'm not sure if it is conventional to list the points in counter-clockwise manner or if the algorithm should take that into account by checking the absolute value of the angle sum? I modified the test file slightly to illustrate the case.

class TestPolygon(unittest.TestCase):
    vertices = [
        Point(0, 0),
        Point(30, 0),
        Point(0, 30),
    ]

    polygon_1 = Polygon(vertices)

    def test_contains_point_polygon_1(self):
        point = Point(10, 10)
        self.assertTrue(self.polygon_1.contains_point(point))

    vertices_2 = [
        Point(0, 0),
        Point(0, 30),
        Point(30, 0),
    ]

    polygon_2 = Polygon(vertices_2)

    def test_contains_point_polygon_2(self):
        point = Point(10, 10)
        self.assertTrue(self.polygon_2.contains_point(point))  <== Fails (FALSE)

The test will fail on polygon_2 which is the same polygon with the points listed in reverse order.

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

2 participants