Game Development Reference

In-Depth Information

Here,
xs
and
ys
are the
x
,
y
values of the starting point for the line. If
xs

is less than 0, then
cs
is given the value 1 shifted three places to the left,

which is 8. If
xs
is greater than or equal to the width of the canvas, then

4 is added to the code value. If the
y
value is less than 0, then 2 is added

to the code value, and finally if
ys
is greater or equal to the height of the

canvas, then 1 is added to the code value. This places the point in one of

the number sections of Figure 2.2, the number being the code value for a

point in that section. For example, if we have the point (-3, 16) on a

canvas that is 200 pixels square, then

cs = ((-3<0)<<3)|(-3>=200)<<2)|((16<0)<<1)|(16>=200)

cs = (1<<3)|(0<<2)|(0<<1)|0

cs = 8|0|0|0

cs = 8

From the code value, we know that the point (-3, 16) with respect to our

canvas is to the left in the section labelled 8 in the diagram.

The next step is to determine the slope of the line. This is done using

the
y
distance divided by the
x
distance. The
x
distance is the end
x
value

minus the start
x
value. The
y
distance is the end
y
value minus the start

y
value. The slope of this line is a floating point value; since the values for

x
and
y
are all integer values, we must remember to cast the integer

values as doubles to get a meaningful result for the slope. Now we have

a point location and a slope. By doing a bitwise And-ing of the start and

end locations, we determine whether the line remains off-screen

throughout its length.

Figure 2.2 Determining point location.

Search Nedrilad ::

Custom Search