一、实验目的和要求
熟悉光栅图形学中的相关直线段裁剪算法、理解Cohen-Sutherland裁剪算法。
二、实验内容
实现Cohen-Sutherland编码线段裁剪算法,能看到裁剪前后的屏幕显示效果。
(裁剪效果显示窗口,放大或最大化即可显示)
三、实验过程及代码
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
class Point
{
public:
float x, y;
};
bool hasLine = true, cut = false;
float offset = 10;
int winWidth = 400, winHeight = 300;
Point clippingWindow[4], line[2], bound[4][2], inside[2];// 用于记录线段line位于裁剪窗口内的两个端点
// 初始化裁剪窗口的四个顶点坐标
void initClippingWindow(void)
{
// 左上角
clippingWindow[0].x = -100;
clippingWindow[0].y = 100;
// 左下角
clippingWindow[1].x = -100;
clippingWindow[1].y = -100;
// 右下角
clippingWindow[2].x = 100;
clippingWindow[2].y = -100;
// 右上角
clippingWindow[3].x = 100;
clippingWindow[3].y = 100;
}
// 绘制裁剪窗口
void drawClippingWindow(void)
{
glColor3f(0, 0, 0);
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 4; i++)
glVertex2f(clippingWindow[i].x, clippingWindow[i].y);
glEnd();
}
// 更新边界
void updateBound(void)
{
// 由裁剪窗口的左上角顶点和左下角定点组成的线,即左边界
bound[0][0] = clippingWindow[0];
bound[0][1] = clippingWindow[1];
// 右边界
bound[1][0] = clippingWindow[2];
bound[1][1] = clippingWindow[3];
// 下边界
bound[2][0] = clippingWindow[1];
bound[2][1] = clippingWindow[2];
// 上边界
bound[3][0] = clippingWindow[0];
bound[3][1] = clippingWindow[3];
}
// 初始化line
void initLine(void)
{
line[0].x = 0;
line[0].y = 0;
line[1].x = 0;
line[1]