Game Development Reference
In-Depth Information
red =(BYTE)col&0xFF;
green=(BYTE)(col&0xFF00)>>8;
blue =(BYTE)(col&0xFF0000)>>16;
d=0;
switch (m_bitdepth){
case 24:
if (dy<dx){
c=2*dx; m=2*dy;
while (x!= xe){
*ptr++=blue; *ptr++=green; *ptr++=red; //Set
the pixel
x+=xinc; d+=m;
if (xinc<0) ptr-=6;
if (d>dx){y+=yinc; d-=c; ptr-=width;}
}
}else{
c=2*dy; m=2*dx;
while (y!=ye){
*ptr++=blue; *ptr++=green; *ptr=red; //Set the
pixel
y+=yinc; d+=m; ptr-=width; ptr-=2;
if (d>dy){ x+=xinc;
d-=c;ptr+=xinc;ptr+=xinc;ptr+=xinc;}
}
}
break;
}
}
When drawing the line we need to decide whether our principle increment
axis is going to be x or y . The test involves simply testing which is greater,
the y distance or the x . If x is greater, then we use the x -axis. For every
column in the x -axis, we need to colour a pixel. If the line were horizontal,
then simply incrementing along the x -axis would be sufficient. But we also
need to find the y position. For each column the y value can increment by
1 or decrement by 1. The maximum slope of a line where x is the principle
axis is a 45° slope. For this slope, y is incremented for each x increment.
So the question we need to ask as we move to the next x column is: Do
we need to alter our y value?
To answer this question we use three variables. Double the x distance,
c , double the y distance, m , and an incremental value d . For each x value,