Game Development Reference
In-Depth Information
BOOL CCanvas::Clip(int &xs, int &ys, int &xe, int &ye){
int cs, ce;
doublem;
//Calculate the slope of the line (xs,ys)-(xe,ye)
m = (double)(ye-ys)/(double)(xe-xs);
while (cs|ce){
cs=((xs<0)<<3)|((xs>=m_width)<<2)|((ys<0)<<1)|
(ys>=m_height);
ce=((xe<0)<<3)|((xe>=m_width)<<2)|((ye<0)<<1)|
(ye>=m_height);
if (cs & ce) return FALSE;
if (cs){
if (cs & 8) ys-=(int)((double)xs*m), xs=0; else
if (cs & 4) ys+=(int)((double)(m_width-xs)*m),
xs=m_width-1; else
if (cs & 2) xs-=(int)((double)ys/m), ys=0; else
if (cs & 1) xs+=(int)((double)(m_height-ys)/m),
ys=m_height-1;
}
if (ce){
if (ce & 8) ye+=(int)((double)(0-xe)*m), xe=0; else
if (ce & 4) ye+=(int)((double)(m_width-xe)*m),
xe=m_width-1; else
if (ce & 2) xe+=(int)((double)(0-ye)/m), ye=0; else
if (ce & 1) xe+=(int)((double)(m_height-ye)/m),
ye=m_height-1;
}
}
return TRUE;
}
Running this function with the values (-10, 100)-(150, 300) for a 200
pixel square canvas gives the following results:
Slope is 1.25
Loop 1 cs = 8 ce = 1 Starting point ( -10, 100) End point (150,300)
Loop 2 cs = 0 ce = 0 Starting point ( 0, 112) End point (70,199)
We now know both the starting and ending points are on-screen. If neither
were on-screen then there is nothing to do, so we exit. Assuming we
actually have something to draw, we adjust the drawing width to the
canvas storage width. Remember a 24-bit file will have a storage width
Search Nedrilad ::




Custom Search