#include <Windows.h>
#include <gl/glut.h>
#include<stdio.h>
int leftBitCode=0x1;
const GLint rightBitCode=0x2;
const GLint buttonBitCode=0x4;
const GLint topBitCode=0x8;
GLint winWidth=640,winHeight=480;
class screenPT
{
public:
GLfloat x,y;
};
inline GLint inside(GLint code){return GLint(!code);} //判断点是否在裁剪区内
inline GLint reject(GLint code1,GLint code2){return GLint(code1&code2);} //判断线段是否完全在裁剪边框外
inline GLint accept(GLint code1,GLint code2){return GLint(!(code1 | code2));} //判断线段是否完全在裁剪边框内
inline void swapPT(screenPT& a,screenPT& b){screenPT t=a;a=b;b=t;} //交换两个点
inline void swapCode(GLubyte& a,GLubyte& b){GLubyte t=a;a=b;b=t;} //交换两个区域码
//确定一个点所在位置的区域码
GLubyte encode(const screenPT p,const screenPT winMin,const screenPT winMax)
{
GLubyte code=0x00;
if(p.x<winMin.x)
code |= leftBitCode;
if(p.x>winMax.x)
code |= rightBitCode;
if(p.y<winMin.y)
code |= buttonBitCode;
if(p.y>winMax.y)
code |= topBitCode;
return code;
}
void drawOneLine(const screenPT a,const screenPT b) //绘制(裁剪过的)线段
{
glBegin(GL_LINES);
glColor3f(0.7,0.0,1.0);//保留的线段为紫色
glVertex2f(a.x,a.y);
glVertex2f(b.x,b.y);
glEnd();
}
//线段裁剪函数
void lineClip(screenPT winMin,screenPT winMax,screenPT lineBegin,screenPT lineEnd)
{