Jump to content
  • Advertisement
Sign in to follow this  
Frogget

Right or left triangle?

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!