0
\$\begingroup\$

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.

enter image description here

enter image description here

\$\endgroup\$
1
  • 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\$ Commented Sep 4, 2020 at 21:24

2 Answers 2

2
\$\begingroup\$

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, and dp1 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.

\$\endgroup\$
2
  • \$\begingroup\$ @Kromster, nope, then they will both be facing away from each other. \$\endgroup\$
    – Bram
    Commented 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\$
    – Kromster
    Commented Sep 5, 2020 at 20:25
1
\$\begingroup\$

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.

enter image description here

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.

\$\endgroup\$
2
  • \$\begingroup\$ This only works if the two vertices are on the same plane \$\endgroup\$
    – Raildex
    Commented Sep 9, 2020 at 10:37
  • \$\begingroup\$ @Raildex on the second thought, that depends on what OP considers "look at each other" \$\endgroup\$
    – Kromster
    Commented Sep 9, 2020 at 12:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .