c++ - Knowing if middle vertex is left or right when sorting a triangle vertices by Y? -
i'm reading chris hecker's texture mapping articles , life of me can't understand how he's using y order of vertices determine if middle vertex on left or right.
sorting code:
if (y0 < y1) { if (y2 < y0) { top = 2; middle = 0; bottom = 1; middlecompare = 0; bottomcompare = 1; } else { top = 0; if (y1 < y2) { middle = 1; bottom = 2; middlecompare = 1; bottomcompare = 2; } else { middle = 2; bottom = 1; middlecompare = 2; bottomcompare = 1; } } } else { if (y2 < y1) { top = 2; middle = 1; bottom = 0; middlecompare = 1; bottomcompare = 0; } else { top = 1; if (y0 < y2) { middle = 0; bottom = 2; middlecompare = 3; bottomcompare = 2; } else { middle = 2; bottom = 0; middlecompare = 2; bottomcompare = 3; } } }
then writes:
if (bottomcompare > middlecompare) { middleisleft = 0; left = &toptobottom; right = &toptomiddle; } else { middleisleft = 1; left = &toptomiddle; right = &toptobottom; }
my questions:
- how relying on y order tell if middle vertex left or right? it's confusing cause sorting done on y, there no mention of x.
- obviously,
bottomcompare
,middlecompare
variables exist achieve comparison. in first half of comparison (the 'if'),middlecompare
equalsmiddle
,bottomcompare
equalsbottom
. in second half (the 'else')bottomcompare
equals3
whenbottom
equals0
, ,middlecompare
equals3
whenmiddle
equals0
. why that?
what missing is:
the polygon winding rule.
the vertexes of triangle/polygon should in specific order cw/ccw.
you should deciding target side line not point.
deciding single point mistake points in both sides.
so example if:
- order cw
x+
axis goes righty+
axis goes down- vertexes
a,b,c
then decide left/right side change in y
axis. while rasterizing of outlines (ab,bc,ca
) @ y
change between first , last point. example ab
line:
dy = (b.y-a.y) if (dy> 0) right if (dy< 0) left if (dy==0) both
for more info see related q/a closed convex polygon filling.
Comments
Post a Comment