Write the code to determine if a 2D point is inside a 2D closed polygon.
Anonymous
Write a function that given two line segments L1 and L2, determine if they intersect. Computer a point (x1,y1) outside the polygon (this is easy because such a point can be found simply by finding the bounding box of the polygon and finding a point outside that bounding box). Since polygon is represented by n segments L1...LN, determine how many of the segments intersect with the line segment formed by (x1,y1) and the point mentioned in the problem. If the number is ODD, the problem point is INSIDE, otherwise outside. Note that we have to be careful about intersections where the line segment formed by (x1,y1) and the point mentioned at one of the polygon's vertices (thus making the line segment intersect with TWO of the polygon's line segments instead of just one). Tweaking the algorithm to deal with this problem is easy.
Check out your Company Bowl for anonymous work chats.