我只是想知道如何在矩形边界框中剪切圆圈?我目前正在使用Cohen-Sutherland算法在我的程序中进行线条裁剪,到目前为止,我设法让矩形和多边形进行剪辑。但是,对于圆形剪辑,我不知道如何实现这一点。我使用以下内容构建我的圈子:
glBegin(GL_POLYGON);
double radius = 50;
for(int angle = 0; angle <= 360; angle++ ){
float const curve = 2 * PI * (float)angle / (float)360;
glVertex2f(point.x + sin(curve) * radius, point.y + cos(curve) * radius);
}
glEnd();
我的裁剪算法与此处的裁剪算法相同:http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm。但是,它返回2个代表新行的点,以便稍后用于绘制剪切的形状。所以我基本上试图这样做:
line Lines[360] // an array with size 360 with data type line, which is a struct holding two points (x1, y1, x2, y2) of the new line returned by my clipping function.
double radius = 50;
for(int angle = 0; angle < 360; angle++){
float const currentCurve = 2 * PI * (float)angle / (float)360;
float const nextCurve = 2 * PI * (float)(angle+1) / (float)360;
int x1 = (int)(point[i].x + sin(currentCurve) * radius); // point is another struct holding only a single point.
y1 = (int)(point[i].y + cos(currentCurve) * radius);
x2 = (int)(point[i+1].x+ sin(nextCurve) * radius);
y2 = (int)(point[i+1].y + cos(nextCurve) * radius);=
// Clip the points with the clipping algorithm:
Lines[i] = Clipper(x1, y1, x2, y2);
}
// Once all lines have been clipped or not, draw:
glBegin(GL_POLYGON);
for(int i = 0; i < 360; i++){
glVertex2f(Lines[i].x1, Lines[i].y1);
glVertex2f(Lines[i].x2, Lines[i].y2);
}
glEnd();
请注意,我用鼠标在屏幕上绘制了一个圆圈,并将每个360点存储到一个名为point的结构数组中,这是一个链接列表的一部分。所以我喜欢1个节点代表屏幕上的一个圆圈。
无论如何,有了上述内容,我的圆圈没有被剪裁(或完全没有绘图),只需点击几下鼠标就会崩溃。