The 3D Story
Your guide to the third dimension.

Drawing Polygons

To draw a polygon is a rather complex procedure, but it can be done rather easy if you think things through.
The strategy I use is base on splitting the polygon into horisontal lines. The other part to make the strategy easier it to restrict the shape to a triangle (which an be used to represent any shape(!)).
How do we approach this problem then? It's quite easy. As we know that it is a triangle we're dealing with, we also know that there are always two lines that will be crossed by the horisontal line. There will not be one, nor three (which is the maximum of triangles).
We then sort these three points according to their Y-values. We end up with three points : min, med and max. These three are sorted from the lowest Y-value to the highest.



We then iterate through all the possible Y-values, i.e. from Ymin to Ymax. We use a very simple formula to calculate the line between two of the points each time. These are the connections used :



The (x,y) point is the resulting point.

T=p1y-p2y

if T=0, then T=1
(To avoid a division by zero.)

X=p2y+((p1x-p2x)/T)*(Y-p2y)

The formulas shown above has been derived from connections between the two triagles, (p1x,p1y)-(p2x,p2y)-(p1x,p2y) and the otherone (p1x,p1y)-(X,Y)-(p1x,Y). This formula is now used in the following way.
As we iterate through the Y-values from Ymin to Ymax we use this formula to find one point between (Xmin,Ymin) and (Xmax,Ymax), and, if Y<Ymed, the one between (Xmin,Ymin) and (Xmed,Ymed), otherwise (Y>=Ymed), the one between (Xmed,Ymed) and (Xmax,Ymax).
If all this sound complicated, just go through the code in BOX3.CPP really carefully and you'll get the main picture.



[ Previous | Main Page | Next ]
Copyright©1997 Johan E. Thelin