Game Development Reference
In-Depth Information
Now we want the x value when the y value on this line is 76, i.e. the y
value for vertex B. A line is defined as
y = mx + c
where m is the slope and c is a constant.
Since we know that the vertex (28, 16) is on the line, we can calculate
c as
c = y - mx = 16 - 2.254
×
28 = -47.112
We also know that the vertex (150, 291) is on this line, so a quick check
gives
y = mx + c = 2.254
×
150 - 47.112 = 290.98
which rounded up is the 291 y value of this vertex.
Having calculated this constant, we can rearrange the equation for a
line to derive the x value:
x =( y - c )/ m
For our line we know that y is 76 and the slope is 2.254, so the x value on
the line AC when y is 76 is
(76 - (-47.112))/2.254 = 54.62
So the point D is (54.62, 76).
When we draw the triangle, we use the same technique that we have
used to determine the point D to determine the horizontal values for the
start and end of each horizontal line. To calculate the end points of each
horizontal line in the triangle ABD we will also need to know the slope and
constant value for the line AB. Using this information, we know the start
and end x values for each integer y value in the triangle ABD. Having
drawn the upper triangle ABD, we go on to draw the lower triangle DBC.
For this triangle we need to know the slope and constant value for the line
BC. We can then go on to draw each horizontal line in the triangle
DBC.
One end of the line will be the slope from y [min] to y [max] and the other
end will change when the y value reaches the mid value. The code works
through each section in turn.