0

If you have a set of three vertices at (x1, y1), (x2, y2) and (x3, y3), how would you determine whether the triangle defined by these three vertices is left-facing or right-facing?

Currently, I'm taking the cross-product to determine whether the vertices are clockwise or not, and with that knowledge I can determine whether the triangle is left-facing or right-facing while I'm sorting their y-coordinates.

This works fine, but the cross-product requires five subtractions and two multiplications.

Is there perhaps some simpler, faster way to determine if a triangle is left-facing that I'm missing?

T .
  • 4,874
  • 3
  • 23
  • 36
  • 2
    Could you be a bit more specific about what you mean by "left-facing"? – Mokosha Aug 11 '14 at 21:36
  • Left-facing would be a triangle where the (vertically) middle vertex lies to the left of the line connecting the top and bottom vertices. – T . Aug 11 '14 at 21:39
  • what for is this used? never heard of this kind of triangle classification before. – Spektre Aug 12 '14 at 11:24
  • 1
    Nit: you mean 5 subtractions, right? Though I guess if you only care about the sign of the result you could turn that into 4 subtractions and one comparison. – Mark Dickinson Aug 13 '14 at 17:28
  • You're right, it's 5 subtractions, nice catch. I've corrected that – T . Aug 13 '14 at 17:35

2 Answers2

2

That would depend on the cost of a floating point operation visavi the cost of a conditional statement (with the added cost of clearing the instruction pipeline half of the time).

My gut feeling is that your current solution is probably a fairly good one.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
0

I see it like this:

  1. find 2 points with min and max Y coordinate

    • if any two points have the same Y coordinate
    • then you have to chose one with X coordinate closer to the other Y min/max point
  2. test the 3.th points x coordinate

    • if it is less then any other point it is Left facing
    • if it is bigger then any other point it is Left facing

triangle facing

[Note]

  • this implementation requires several if states
  • which can be slower then to solve this by cross product ...
  • even if it has less operations

if your triangles are with defined winding (always CW or CCW)

  • triangle has A,B,C points
  • compute count of positive and negative dy of lines like this:

    int p=0,n=0;
    if (B.y-A.y>=0) p++; else n++;
    if (C.y-B.y>=0) p++; else n++;
    if (A.y-C.y>=0) p++; else n++;
    
  • now for CW

  • if (p<n) left_facing; else right_facing;
  • for CCW it is inegated ...
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • If the x-coordinate of the third vertex is between the x-coordinates of vertex 1 and 2, then you need additional calculations. – Klas Lindbäck Aug 12 '14 at 08:42
  • @KlasLindbäck I know what you mean but is the facing even defined in such case? btw your answer concludes the same as mine see notes :) – Spektre Aug 12 '14 at 11:20
  • 1
    Yes, the facing is defined. It is facing left if the third vertex is left of the side between vertex 1 and vertex 2. **Example:** (0, 0), (2, 5), (1, 3) is facing left while (0, 0), (2, 5), (1, 2) is facing right. – Klas Lindbäck Aug 12 '14 at 12:05