# Right or left triangle?

This topic is 4783 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am writing a triangle rasterizer and I can't seem to solve one problem: how do you tell if a triangle is a right or left triangle, given the three vertices? By right or left, I mean the direction that vectors from the top and bottom vertices to the middle vertex would point.

##### Share on other sites
Use the cross product of the vectors (p2-p1) and (p3-p1). If the resultant vector is towards you, the winding is counterclockwise.

##### Share on other sites
Maybe I wasn't quite clear on what my problem was. I can find the ordering of the vertices just fine, I just need to know whether the triangle is pointing right:
|| |  \ |  /| /|/or left:  /| / |/  |\  | \ |  \|

And I can't just test the x position of the middle vertex, since there are a lot of special cases where the triangle is tilted almost horizontally.

stupid formatting...I think you get the idea.

##### Share on other sites
Calculate the 2d vector between the [average of the top and bottom vertex] and the third vertex. From this vector, you can draw the following conclusions:

If x < 0 and y < 0 then the "arrow" is pointing top-left; calculate the ratio of x and y to determine the "leftness" or "topness".

If x > 0 and y < 0 then the "arrow" is pointing top-right; calculate the ratio of x and y to determine the "rightness" or "topness".

...need I go on?... :)

##### Share on other sites
There's one problem with your method, there are some triangles that this vector ends up being positive in the x direction, but the triangle is still a left triangle, while with most left triangles, it is negative in the x direction.
I can think of one solution to this problem, which would be to just do the inside-ouside half space test for the middle point with the opposite line, using one of the edges of the screen as a reference point, but that just seems kind of hacky and I was hoping someone knew of a better solution.

##### Share on other sites

Given the 2(top and bottom) vertices, you can calculate a line equation f(y)=x. There will be a special case where the line will be totally vertical, so you'll have f(y)=C(constant).

Anwyay,you know the Y coordinate of the middle vertex, so if you put that into the equation you'll get the x coordinate of the line in that y coord. If the vertex x coordinate is smaller than that, then the triangle points to the left, else to the right.

         /|         / |       /  |      /   |(x,vy) (vx,vy)...|...       (vx<x) so it points to the left      \   |       \  |        \ |         \|

-EDIT: Now that I'm thinking about it, it might be a simpler solution. If we order the vertices top-middle-bottom, the winding would be counter-clockwise for left triangles, and clockwise for right. Or am I wrong about this?

##### Share on other sites
Yes, mikeman your idea seems to work (the second one) thanks for the help.

##### Share on other sites

This topic is 4783 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.