직사각형 경계 상자에서 원을 자르는 방법에 대해 궁금한가요? 현재 내 프로그램에서 라인 클리핑에 Cohen-Sutherland 알고리즘을 사용하고 있으며 지금까지 사각형과 폴리곤을 클립으로 처리했습니다. 그러나 원 클리핑의 경우이를 수행하는 방법을 알지 못합니다. 나는 나의 원을 구성하려면 다음을 사용하고 있습니다 : http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm :C++/OpenGL - 2D - 사각형 경계 상자에서 원을 자르는 방법
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();
내 클리핑 알고리즘이 여기 것과 동일합니다. 그러나 새 선을 나타내는 2 점을 반환하여 나중에 클리핑 된 모양을 그리는 데 사용됩니다. 그래서 기본적으로 내가이 일을 시도했다 :, 나는의 간격입니다 포인트라는 구조체 배열에 각 360 점을 마우스와 함께 화면에 원을 그려 저장 한
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();
주 연결된 목록. 그래서 하나의 원을 화면에 나타내는 노드를 가지고 있습니다.
어쨌든, 위와 같이, 내 원은 그리기 (또는 그 문제에 대해 전혀 그림이 그리지 않음)이며 마우스를 몇 번 클릭하면 응용 프로그램이 충돌합니다.
http://www.opengl.org/sdk/docs/man/xhtml/glScissor.xml 당신의 색인 배열 라인을 i 변수를 사용하여, 실제로 나는 루프 위, 당신은 변수 "각도"를 이용해야한다고 생각 :) (glScissor에 읽을 - – Robinson