Game Development Reference
In-Depth Information
Drawing a flat coloured polygon
In order to paint a filled polygon we need to raster scan the polygon. That
is, we need to break up the polygon into horizontal lines and draw each of
these in turn. To keep things simple we will work only with triangles. If the
polygon has four sides then we draw one half first and then the next. A
triangle can sometimes be orientated so that one of its sides is horizontal,
but an arbitrary triangle can be split into two triangles, each with one
horizontal side.
Figure 2.5 Dividing a triangle into two, each with a horizontal line.
To determine the starting point of each horizontal line in the traingle we
need the slope of all three lines. In our code we first order the points by
height; to create variables y [max], y [mid] and y [min]. The slope of each
side is the y distance divided by the x distance. Using this information, we
can determine the start and end points of each horizontal line and draw
the line for each y value. The starting point for each horizontal line is given
by the starting point for this slope, the slope of the line and the current y
If we look at an example then it will be clearer. The vertices of the
triangle in Figure 2.5 are
A(28, 16)
B(268, 76)
C(150, 291)
Taking the line AC, we can calculate point D using the following technique.
First determine the slope of line AC.
The slope of the line is the y distance divided by the x distance:
(291 - 16)/(150 - 28) = 275/122 = 2.254
Search Nedrilad ::

Custom Search