Пример #1
0
/* ============================================================================================= */
void WindowClipper::cohen_sutherland_clipping(Line &line)
/* ============================================================================================= */
{
    DCoordVector newVertices;
    double x1 = line.p1()->xnc();
    double x2 = line.p2()->xnc();
    double y1 = line.p1()->ync();
    double y2 = line.p2()->ync();
    int outcode1 = computeOutcode(x1, y1);
    int outcode2 = computeOutcode(x2, y2);
    bool accept = false;

    while(true) {
        if (!(outcode1 | outcode2)) {
            accept = true;
            break;
        } else if (outcode1 & outcode2) {
            break;
        } else {
            double x, y;

            int outcodeOut = outcode1 ? outcode1 : outcode2;

            if ( outcodeOut & TOP) {
                x = x1 + (x2 - x1) * (Y_MAX - y1) / (y2 - y1);
                y = Y_MAX;
            } else if (outcodeOut & BOTTOM) {
                x = x1 + (x2 - x1) * (Y_MIN - y2) / (y2 - y1);
                y = Y_MIN;
            } else if (outcodeOut & RIGHT) {
                y = y1 + (y2 - y1) * (X_MAX - x1) / (x2 - x1);
                x = X_MAX;
            } else if(outcodeOut & LEFT) {
                y = y1 + (y2 - y1) * (X_MIN - x1) / (x2 - x1);
                x = X_MIN;
            }

            if (outcodeOut == outcode1) {
                x1 = x;
                y1 = y;
                outcode1 = computeOutcode(x1, y1);
            } else {
                x2 = x;
                y2 = y;
                outcode2 = computeOutcode(x2, y2);
            }
        }
    }

    if (accept) {
        DEBUG_MSG("X1: " << x1);
        DEBUG_MSG("Y1: " << y1);
        DEBUG_MSG("X2: " << x2);
        DEBUG_MSG("Y2: " << y2);
        newVertices.push_back(new Coord<double>(x1, y1));
        newVertices.push_back(new Coord<double>(x2, y2));
    }
    line.update_normalized_coords(newVertices);
}
Line CohenShutterlandAlgorithm::getLineInView(Line line,Point viewPortStart,Point viewPortEnd) {
	
	Point linePosition = line.getPosition();
	int x0 = line.getBeginPoint().x + linePosition.x;
	int y0 = line.getBeginPoint().y + linePosition.y;
	int x1 = line.getEndPoint().x + linePosition.x;
	int y1 = line.getEndPoint().y + linePosition.y;

	Point point1(x0,y0);
	Point point2(x1,y1);

	
	int outcode1 = computeOutcode(point1,viewPortStart,viewPortEnd);
	int outcode2 = computeOutcode(point2,viewPortStart,viewPortEnd);
	while(true) {

		int outcodeOut = outcode1 ? outcode1 : outcode2;
		if (!(outcode1|outcode2)){
			return Line(Point(x0,y0),Point(x1,y1),line.getColor());	
		}	
		else if (outcode1 & outcode2){
			return Line(Point(0,0),Point(0,0),Color(0,0,0,0));
		}
		else {
			int x,y;
			if ( outcodeOut & TOP ) {
				x = x0 + (x1-x0) * (viewPortStart.y - y0) / (y1-y0) ;
				y = viewPortStart.y;
			} else if (outcodeOut & BOTTOM) {
				x = x0 + (x1-x0) * (viewPortEnd.y - y0) / (y1-y0);
				y = viewPortEnd.y;
			} else if (outcodeOut & LEFT ) {
				y = y0 + (y1-y0) * (viewPortStart.x - x0) / (x1-x0);
				x = viewPortStart.x;
			} else if (outcodeOut & RIGHT) {
				y = y0 + (y1-y0) * (viewPortEnd.x - x0 ) / (x1-x0);
				x = viewPortEnd.x;
			}
			
			if (outcodeOut == outcode1) {
				x0 = x;
				y0 = y;
				outcode1 = computeOutcode(Point(x0,y0),viewPortStart,viewPortEnd);
			}
			else {
				x1 = x;
				y1 = y;
				outcode2 = computeOutcode(Point(x1,y1),viewPortStart,viewPortEnd);
			}
		}
	}
}