How do I test to see if a polygon edge is facing another polygon? For the edge being tested to return true, the edge needs to be facing a polygon which has a different attribute than itself.

polygon example

In this example, I am iterating through the edges on Polygon A. Segment 0 and segment 1 would return true for facing the polygon, where segment 2 would return false, because, while it is still facing a polygon, the polygon has the same 'green' attribute.

  • $\begingroup$ Your example is not visible. Do you work in 2D or 3D? $\endgroup$ – user18490 Jan 30 '17 at 21:53
  • $\begingroup$ Strange, I can see it. 2D. Here is another link to the example: bit.ly/2kIc4Wg $\endgroup$ – agarfield Jan 30 '17 at 22:30
  • $\begingroup$ You can also raytrace lines. Cast a ray perpendicular to line and test the lines in the scene. Like ray-tracing but in 2D. That would work and would give you accurate results. $\endgroup$ – user18490 Feb 1 '17 at 20:03
  • $\begingroup$ In your picture, Segment 2 does face a polygon of a different colour; Polygon D, but you say it should return false for Segment 2. This implies you need to see if the path is occluded by polygons of the same colour, right? $\endgroup$ – PeteUK Feb 2 '17 at 23:27
  • $\begingroup$ Yes. That's another piece of it. The segment will return false if the closest edge which the edge is facing is of the same type. $\endgroup$ – agarfield Feb 3 '17 at 6:22

You can take the normal of the line segment you're testing and find the dot product of that normal and the normal of each segment of the polygon you're testing against. If it is positive, then they are facing the same way. If the dot product is negative, then the line segments are facing in opposite directions.

If they are facing in opposite directions, then they're either pointing at each other, or away from each other. You can figure out which by calculating a vector from a point on the first segment to a point on the other segment and seeing if it's facing the same direction as its normal (by again using the dot product). If so, they are facing each other, otherwise they're facing away.

You can skip all the tests above by first testing if the polygons are the same color and only doing the tests if they're not.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.