Search code examples
c++graphicspolygonraytracing

ray casting algorithm without side coordinates


Im trying to define if a point is inside a polygon. Only the coordinates of the corners are given. After some research I found the Ray casting algorithm but it looks like i need a vector filled with the coordinates of the polygons side. I tried to calculate these coordinates as well, but it doesn't look like this is the solution.

Maybe I'm interpreting the algorithm wrong so it would be nice if someone could push me into the right direction.


Solution

  • I am assuming you are trying to do this for any kind of polygon.

    Check this out to get a handle on the techniques for solving complicated polygons. This is actually what you probably want and it is the ray casting algorithm you mentioned before. http://alienryderflex.com/polygon/

    A short explanation of that is you have a polygon, you know its points. Construct connections between the points (vectors). Cast a ray across the entire polygon through the point you are trying to test.

    At each intersection of the ray with one of the polygon vectors increment a counter by 1 starting from 0. If you intersect the point and that counter is even it is not in the polygon. If the counter is odd then that point is inside the polygon.