I need to flip polygons in my application. How do I determine if two vectors face each other, like in the second image below? Note that the origin of the vectors is also important, in addition to the direction.
-
2\$\begingroup\$ Note that these are not just directions you're comparing, but vertices and directions. E.g. if you swap the directions in the top left you get the bottom right, and one is OK and one is NO, even though they're the same directions drawn in different places on the screen. So the position also matters. \$\endgroup\$– Criticizing Israel not allowedCommented Sep 4, 2020 at 21:24
2 Answers
Given points P0
and P1
with normals N1
and N2
...
- Compute delta vector:
delta = P1 - P0
- Compute dot product with normal:
dp0 = dot(delta, N0)
- Compute dot product with normal:
dp1 = dot(delta, N1)
- If
dp0
is positive, anddp1
is negative then they see each other.
But frankly, this can be expressed even simpler:
They both look at each other if each normal looks at the other point.
N0
looks at P1
if dot( P1-P0, N0 )
is positive.
N1
looks at P0
if dot( P0-P1, N1 )
is positive.(*)
or dot (P1-P0, N1 )
is negative, as you already computed that vector for the first test.
-
\$\begingroup\$ @Kromster, nope, then they will both be facing away from each other. \$\endgroup\$– BramCommented Sep 5, 2020 at 20:21
-
\$\begingroup\$ I'm not sure if TS expects normals (-1;2 at 0;0) and (-2;2 at 2;0) to face each other or not. They both will be negative with delta. \$\endgroup\$– KromsterCommented Sep 5, 2020 at 20:25
Well, you can compare the distances between origins (d1
) and points offset by the normals (d2
) (scaled by fraction of the distance, to avoid overshoots). If the distance is smaller than between origins - normals are facing each other.
I'm not sure if you expect normals (-1;2 at 0;0) and (-2;2 at 2;0) to face each other or not though.