I was reading Cohen–Sutherland algorithm on Wikipedia and I am confused in these two parts,
int ComputeOutCode(double x, double y) {
...
if (x < xmin) code |= LEFT;
else if (x > xmax) code |= RIGHT;
if (y < ymin) code |= BOTTOM;
else if (y > ymax) code |= TOP;
...
}
and
if (outcodeOut & TOP) { // point is above the clip rectangle
x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
y = ymax;
} else if (outcodeOut & BOTTOM) { // point is below the clip rectangle
x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
y = ymin;
} else if (outcodeOut & RIGHT) { // point is to the right of clip rectangle
y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
x = xmax;
} else if (outcodeOut & LEFT) { // point is to the left of clip rectangle
y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
x = xmin;
}
What I am confused is, How does it handle points who are top-left, top right, etc. It just handles top, left, right and bottom.
Why use |
in code |= LEFT;
and outcodeOut & TOP
in if-else
below it? I know that these two somehow handle all 8 possible cases but I don't know how.