Point in polygon problem

Given a point and a set of sides which make up a convex polygon, output true if the point is inside of the polygon, otherwise output false.

I understand that this problem is basic but I struggle to find a solution for it. After trying to solve it myself I looked up some solutions but they don’t work either if I understand the problem correctly. For example, the solution posted on geeks for geeks doesn’t work for the following case:

Coordinates of the point which needs to be checked: (-2,0)
Sides of the polygon (one line corresponds to one side by connecting the two given points on that line):

(-4 -1), (-5 0)
(-1 1), (0 4)
(-5 0), (-5 1)
(-5 1), (-1 1)
(5 0), (3 -4)
(3 -4), (2 -5)
(0 4), (4 4)
(-2 -1), (-4 -1)
(4 4), (5 0)
(2 -5), (-2 -1)

The point is inside of the polygon but the geeks for geeks solution (and many others) claim that the point is outside.

Am I missing something or is the geeks for geeks solution wrong?

If you read the comments of geeksforgeeks many people have pointed out the wrongness of code with sample input/output so yes the code is actually wrong. You follow this or this

1 Like