void pontilharReta(){ Color gray = Color(0.8, 0.8, 0.8, 1); if (outcodeOut == outcode0) { auxPoints->push_back(Point2D(x0, y0, 10, gray)); points->at(0).setX(x); points->at(0).setY(y); outcode0 = computeOutCode(x0, y0); } else { auxPoints->push_back(Point2D(x1, y1, 10, gray)); points->at(1).setX(x); points->at(1).setY(y); outcode1 = computeOutCode(x1, y1); } points->pop_back(); auxPoints->push_back(Point2D(x, y, 10, Color(0,0,0,0))); step++; finished = true; }
int clip(double *x0, double *y0, double *x1, double *y1, double xmin, double ymin, double xmax, double ymax) { int outcode0 = computeOutCode(*x0, *y0, xmin, ymin, xmax, ymax); int outcode1 = computeOutCode(*x1, *y1, xmin, ymin, xmax, ymax); int accept = 0; int changed = 0; while (1) { if (!(outcode0 | outcode1)) { // Bitwise OR is 0. Trivially accept and get out of loop accept = 1; break; } else if (outcode0 & outcode1) { // Bitwise AND is not 0. Trivially reject and get out of loop break; } else { // failed both tests, so calculate the line segment to clip // from an outside point to an intersection with clip edge double x = *x0, y = *y0; // At least one endpoint is outside the clip rectangle; pick it. int outcodeOut = outcode0 ? outcode0 : outcode1; // Now find the intersection point; // use formulas y = y0 + slope * (x - x0), x = x0 + (1 / slope) * (y - y0) 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; } // Now we move outside point to intersection point to clip // and get ready for next pass. if (outcodeOut == outcode0) { *x0 = x; *y0 = y; outcode0 = computeOutCode(*x0, *y0, xmin, ymin, xmax, ymax); changed = 1; } else { *x1 = x; *y1 = y; outcode1 = computeOutCode(*x1, *y1, xmin, ymin, xmax, ymax); changed = 1; } } } if (accept == 0) { return 0; } else { return changed + 1; } }
//http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm bool Rectangle::intersect(blib::math::Line l) const { // compute outcodes for P0, P1, and whatever point lies outside the clip rectangle int outcode0 = computeOutCode(l.p1); int outcode1 = computeOutCode(l.p2); bool accept = false; while (true) { if (!(outcode0 | outcode1)) { // Bitwise OR is 0. Trivially accept and get out of loop accept = true; break; } else if (outcode0 & outcode1) { // Bitwise AND is not 0. Trivially reject and get out of loop break; } else { // failed both tests, so calculate the line segment to clip // from an outside point to an intersection with clip edge float x, y; // At least one endpoint is outside the clip rectangle; pick it. int outcodeOut = outcode0 ? outcode0 : outcode1; // Now find the intersection point; // use formulas y = y0 + slope * (x - x0), x = x0 + (1 / slope) * (y - y0) if (outcodeOut & TOP) { // point is above the clip rectangle x = l.p1.x + (l.p2.x - l.p1.x) * (bottomright.y - l.p1.y) / (l.p2.y - l.p1.y); y = bottomright.y; } else if (outcodeOut & BOTTOM) { // point is below the clip rectangle x = l.p1.x + (l.p2.x - l.p1.x) * (topleft.y - l.p1.y) / (l.p2.y - l.p1.y); y = topleft.y; } else if (outcodeOut & RIGHT) { // point is to the right of clip rectangle y = l.p1.y + (l.p2.y - l.p1.y) * (bottomright.x - l.p1.x) / (l.p2.x - l.p1.x); x = bottomright.x; } else if (outcodeOut & LEFT) { // point is to the left of clip rectangle y = l.p1.y + (l.p2.y - l.p1.y) * (topleft.x - l.p1.x) / (l.p2.x - l.p1.x); x = topleft.x; } // Now we move outside point to intersection point to clip // and get ready for next pass. if (outcodeOut == outcode0) { l.p1 = glm::vec2(x, y); outcode0 = computeOutCode(l.p1); } else { l.p2 = glm::vec2(x, y); outcode1 = computeOutCode(l.p2); } } } return accept; }
CohenSutherlandStep(Drawer *drawer, vector<Point2D> *points, vector<Point2D> *auxPoints, double xMin, double yMin, double xMax, double yMax){ this->drawer = drawer; this->points = points; this->auxPoints = auxPoints; this->xMin = xMin; this->yMin = yMin; this->xMax = xMax; this->yMax = yMax; Point2D a = points->at(0); Point2D b = points->at(1); x0 = a.getX(); y0 = a.getY(); x1 = b.getX(); y1 = b.getY(); // compute outcodes for P0, P1, and whatever point lies outside the clip rectangle outcode0 = computeOutCode(x0, y0); outcode1 = computeOutCode(x1, y1); }