Frogget 122 Report post Posted November 13, 2004 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. 0 Share this post Link to post Share on other sites
Sneftel 1788 Report post Posted November 13, 2004 Use the cross product of the vectors (p2-p1) and (p3-p1). If the resultant vector is towards you, the winding is counterclockwise. 0 Share this post Link to post Share on other sites
Frogget 122 Report post Posted November 13, 2004 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. 0 Share this post Link to post Share on other sites
Nik02 4352 Report post Posted November 13, 2004 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?... :) 0 Share this post Link to post Share on other sites
Frogget 122 Report post Posted November 13, 2004 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. 0 Share this post Link to post Share on other sites
mikeman 2942 Report post Posted November 13, 2004 How about this: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? 0 Share this post Link to post Share on other sites
Frogget 122 Report post Posted November 13, 2004 Yes, mikeman your idea seems to work (the second one) thanks for the help. 0 Share this post Link to post Share on other sites