15 PHBBox(
const double x0,
const double y0,
const double x1,
const double y1) :
16 _x0(x0), _y0(y0), _x1(x1), _y1(y1)
21 bool ClipLine(
double& x0,
double& y0,
double& x1,
double& y1)
const
23 int clipCode0 = ClipCode(x0,y0);
24 int clipCode1 = ClipCode(x1,y1);
26 while ( clipCode0 || clipCode1 )
31 if ( clipCode0 & clipCode1 )
return false;
34 if ( clipCode0 > 0 ) code = clipCode0;
35 else code = clipCode1;
39 if ( (code & BOTTOM) == BOTTOM )
44 x = x0 + (x1-x0)*(y-y0)/(y1-y0);
46 else if ( (code & TOP) == TOP )
51 x = x0 + (x1-x0)*(y-y0)/(y1-y0);
53 else if ( (code & LEFT) == LEFT )
57 y = y0 + (y1-y0)*(x-x0)/(x1-x0);
59 else if ( (code & RIGHT) == RIGHT )
63 y = y0 + (y1-y0)*(x-x0)/(x1-x0);
68 if ( code == clipCode0 )
74 clipCode0 = ClipCode(x0,y0);
82 clipCode1 = ClipCode(x1,y1);
89 void Print(std::ostream& os = std::cout)
91 os << _x0 <<
" " << _y0 <<
", " << _x1 <<
" " << _y1 << std::endl;
95 enum { RIGHT=1, BOTTOM=2, LEFT=4, TOP=8 };
97 int ClipCode(
const double x,
const double y)
const
100 if ( x > _x1 ) code |= RIGHT;
101 else if ( x < _x0 ) code |= LEFT;
102 if ( y > _y1 ) code |= TOP;
103 else if ( y < _y0 ) code |= BOTTOM;
void Print(std::ostream &os=std::cout)
PHBBox(const double x0, const double y0, const double x1, const double y1)
Construct with the boundaries in x and y.
bool ClipLine(double &x0, double &y0, double &x1, double &y1) const