该算法是通过对直线两顶点的编码来判断直线段与裁剪矩形窗口的位置。
对于每一条直线段分为以下三种情况处理:
1.直线段完全在窗口之内,则显示该线段;
2.线段完全在窗口之外,则丢弃该直线段;
3.若线段一部分在窗口内,一部分在窗口外,将线段分割为两段,完全在窗口外的线段丢弃,然后继续重复判断剩下的直线段与矩形裁剪窗口的位置。
问题1:怎么对直线段顶点进行编码呢?
问题2:怎样判断裁剪窗口和直线段的位置呢?
上代码:
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#define OneLine 0
#define Crop 1
int mode = OneLine;//默认为重置直线
int c = 0;
#define LEFT_EDGE 1
#define RIGHT_EDGE 2
#define BOTTOM_EDGE 4
#define TOP_EDGE 8
void LineGL(int x0, int y0, int x1, int y1)
{
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(x0, y0);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(x1, y1);
glEnd();
}
struct MyRect
{
float xmin, xmax, ymin, ymax;
};
MyRect rect;
int x0, y0, x1, y1;
void drawPaint(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(rect.xmin, rect.ymin);
glVertex2f(rect.xmin, rect.ymax);
glVertex2f(rect.xmax, rect.ymax);
glVertex2f(rect.xmax, rect.ymin);
glEnd();
glFlush();
}
int getCode(int x, int y, MyRect rect)
{
int code = 0x00;
if (y < rect.ymin)
code = code | 4;
if (y > rect.ymax)
code = code | 8;
if (x > rect.xmax)
code = code | 2;
if (x < rect.xmin)
code = code | 1;
return code;
}
int cohenSutherlandLine(MyRect rect, int& x0, int& y0, int& x1, int& y1)
{
int accept, done;
float x, y;
accept = 0;
done = 0;
int code1, code2, codeout;
code1 = getCode(x0, y0, rect);
code2 = getCode(x1, y1, rect);
do {
if (!(code1 | code2))
{
accept = 1;
done = 1;
}
else if (code1 & code2) {
done = 1;
x1 = 0;y1 = 0;x0 = 0;y0 = 0;
return accept;
}
else
{
if (code1 != 0)
codeout = code1;
else
codeout = code2;
if (codeout & LEFT_EDGE) {
y = y0 + (y1 - y0) * (rect.xmin - x0) / (x1 - x0);
x = (float)rect.xmin;
}
else if (codeout & RIGHT_EDGE) {
y = y0 + (y1 - y0) * (rect.xmax - x0) / (x1 - x0);
x = (float)rect.xmax;
}
else if (codeout & BOTTOM_EDGE) {
x = x0 + (x1 - x0) * (rect.ymin - y0) / (y1 - y0);
y = (float)rect.ymin;
}
else if (codeout & TOP_EDGE) {
x = x0 + (x1 - x0) * (rect.ymax - y0) / (y1 - y0);
y = (float)rect.ymax;
}
if (codeout == code1)
{
x0 = x; y0 = y;
code1 = getCode(x0, y0, rect);
}
else
{
x1 = x; y1 = y;
code2 = getCode(x1, y1, rect);
}
}
} while (!done);
if (accept)
LineGL(x0, y0, x1, y1);
return accept;
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
drawPaint();//绘制矩形
LineGL(x0, y0, x1, y1);
glFlush();
}
void Init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
rect.xmin = 100;
rect.xmax = 300;
rect.ymin = 100;
rect.ymax = 300;
}
void myReshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}
void myKeyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'c':
cohenSutherlandLine(rect, x0, y0, x1, y1);
glutPostRedisplay();
break;
case 'x':
exit(0);
break;
default:
break;
}
}
//输入两个点的坐标
void input(GLint button, GLint state, GLint x, GLint y) {
if (c == 0) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {//左键按下
x0 = x;
y0 = y;
c = 1;
}
}
if (c == 1) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {//左键按下
x1 = x;
y1 = y;
}
}
}
//鼠标拖动后根据选择做相应的移动
void Drawmouse(GLint button, GLint state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
switch (mode)
{
case OneLine:
//1.获取两个点,
glutMouseFunc(input);
glutMouseFunc(input);
break;
case Crop:
break;
default:
break;
}
}
}
void MYmouse(int x, int y) {
switch (mode)
{
case OneLine: //2. 绘制一条直线
LineGL(x0, y0, x1, y1);
break;
case Crop: //2.对直线相对于矩形的位置进行判断并进行裁剪
//1.得到直线两个顶点的编码
cohenSutherlandLine(rect, x0, y0, x1, y1);
glutPostRedisplay();
break;
default:
break;
}
}
void mymenu(int id) {
if (id == 0)
mode = 0;
else if (id == 1)
mode = 1;//进行裁剪;
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("实验5");
int id = glutCreateMenu(mymenu);//添加菜单
//以下为菜单添加条目
glutAddMenuEntry("OneLine", 0);
glutAddMenuEntry("Crop", 1);//进行裁剪
glutAttachMenu(GLUT_RIGHT_BUTTON);//把菜单和鼠标右键关联起来:菜单的显示方式:鼠标右键
Init();
glutDisplayFunc(myDisplay);
glutReshapeFunc(myReshape);
glutMouseFunc(Drawmouse);//鼠标监听回调函数
glutMotionFunc(MYmouse);//鼠标拖动
glutKeyboardFunc(myKeyboard);
glutMainLoop();
return 0;
}
运行测试:
点击右键选择oneline画一条直线:
点击右键选择crop进行直线段的裁剪: