实验六:(2学时)
一、 实验目的:
理解Cohen-Sutherland裁剪算法
二、 实验内容:
实现Cohen-Sutherland编码线段裁剪算法,能看到裁剪前后的屏幕显示效果。
三、 实现效果及步骤(或流程)
源码如下:
// test6.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#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]