Getting vertices in the right order

Started by
7 comments, last by Dark Star 23 years, 8 months ago
Hi all, I was just wondering if somebody could tell me how I could write a porgram that can put 3 triangle points in order. For example: P2 |\ |x\ |xx\ |xxx\ |xxxx\ |xxxxx\P1 ------- P3 how could I write a program to put the points in order. The problem is I cannot get the computer recognise order, because I want the computer to end up saying that: Point 1 = P2.x,P2.y Point 2 = P1.x,P1.y Point 3 = P3.x,P3.y which is in clockwise order so that the triangle that makes up my polygon will be in order when it comes to performing a specific calculation that I need to do. Thanks in advance. Dark Star
---------------------------------------------You Only Live Once - Don't be afriad to take chances.
Advertisement
is it for rasterizing the triangle? i''d think you''d want the verticies in ascending Y order if so. that''d be just a tad bit easier.

if i''m wrong and you do want them clockwise it''s easy. this''ll put them with y1 the top most vertex and then in clockwise order to y2 then y3.

1. put them in ascending Y order..
(1=lowest y value, 2=middle, 3=highest y value)

2. get the slopes of the sides running down from the 1st point
(get run/rise which is the opposite of what you''d think)
ie. leftslope=(x2-x1)/(y2-y1) and rightslope=(y3-y1)/(x3-x1)
make sure you calculate them in floating point

3. if leftslope is less than rightslope then.. the points are in the correct order now just from when you sorted by Y ascendingly

4. OR if leftslope is greater than rightslope.. switch point 2 and 3 and now they are in order.


-------
i hope that makes sense. couple things: watch out for "divide by zeros" when finding the slope, and if the slopes are equal then there really is no clockwise order if you think about it.. i guess just leave it in ascending Y order in that case, makes the most sense to me.

-werdup-
shmaLbas, I would think that you can replace step 2&3 by checking the X value of point 2 and 3.

e.g.

if (pt2.x < pt3.x)
{
SWAP(pt2, pt3);
}

I think that will work and is a lot more easier(and faster) than calculating the slope and comparing it... well? what do you think?

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..
I assume you want them in clockwise order so you get the correct normal right?

But I dont think it''s possible to come up with an algorithm for it cause only you can decide when a polygon is facing the viewer or away from them, you see what i mean?

You could ahve a triangle like so

A
|\
| \
| \
| \
B---C

And use that algo to find out the order ACB, but what if this triangle is actually facing away from the viewer at this point? Then the real order would be ABC cause that''s clockwise from the visible face.

Am I making sense at all?



ByteMe95::~ByteMe95()
ByteMe95::~ByteMe95()My S(h)ite
shmaLbas, I would think that you can replace step 2&3 by checking the X value of point 2 and 3.

e.g.

if (pt2.x < pt3.x)
{
SWAP(pt2, pt3);
}

I think that will work and is a lot more easier(and faster) than calculating the slope and comparing it... well? what do you think?

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..
actually, no you can''t take out steps 2 and 3..
look at this example, when i first wrote a triangle rasterizer i had thought i''d do it by just testing the X''s but it didn''t work, it would come out all odd looking...




-werdup-
er, the second one is the one to look at, the first one would work with just check X values, but not the second
-werdup-
try this

VECTOR tri;
VECTOR e(cameraPOS - tri);
c = DOT(e, tri.normal);

if c < 0 its anticlockwise
But how do you get the tri.normal without having the vertices in clockwise order?
;o)



ByteMe95::~ByteMe95()
ByteMe95::~ByteMe95()My S(h)ite

This topic is closed to new replies.

Advertisement