Liang - Barsk 线段裁剪算法

GLint clipTest(GLfloat p, GLfloat q, GLfloat * u1, GLfloat * u2) { GLfloat r; GLint returnValue = true; if( p < 0.0 ) { r = q / p; if( r > *u2) returnValue = false; else if( r > *u1) *u1 = r; } else if( p > 0.0 ) { r = q / p; if( r < *u1 ) returnValue = false; else if( r < *u2 ) *u2 = r; } else if( q < 0.0 ) returnValue = false; return (returnValue); } void lineClipLiangBarsk( wcPt2D winMin, wcPt2D winMax, wcPt2D p1, wcPt2D p2) { GLfloat u1 = 0.0, u2 = 1.0, dx = p2.x - p1.x, dy; if( clipTest( -dx, p1.x - winMin.x, &u1, &u2)) { if( clipTest(dx, winMax.x - p1.x, &u1, &u2)) { dy = p2.y - p1.y; if( clipTest(-dy, p1.y - winMin.y, &u1, &u2)) { if(clipTest(dy, winMax.y - p1.y, &u1, &u2)) { if( u2 < 1.0 ) { p2.x = p1.x + u2 * dx; p2.y = p1.y + u2 * dy; } if( u1 > 0.0 ) { p1.x = p1.x + u1 * dx; p1.y = p1.y + u1 * dy; } lineBresenham( round(p1.x), round(p1.y), round(p2.x), round(p2.y)); } } } } }

你可能感兴趣的:(算法)