#include <windows.h>
#include <GL/glut.h>
#include <iostream>
#include <cmath>
using namespace std;
struct Line
{
double x,y;
int code;
} firstLine[10],afterLine[10],polygon[4];
int afternum=0,firstnum=10;
//编码
int encode(double x,double y)
{
// 0 0 0 0
// 上 下 右 左
int code=0;
if(x<polygon[0].x)//xmin
code+=1;
if(x>polygon[2].x)//xmax
code+=2;
if(y<polygon[0].y)//ymin
code+=4;
if(y>polygon[2].y)//ymax
code+=8;
return code;
}
void init()
{
glClearColor(0,0,0,0.0);//清除当前所有颜色
glMatrixMode(GL_PROJECTION);//投影模型
gluOrtho2D(0,500,0,500);//二维投影,参数越大,直线越小
polygon[0].x=200,polygon[0].y=200;//裁剪窗口
polygon[1].x=300,polygon[1].y=200;
polygon[2].x=300,polygon[2].y=300;
polygon[3].x=200,polygon[3].y=300;
firstLine[0].x=100,firstLine[0].y=250;//线段
firstLine[1].x=250,firstLine[1].y=450;
firstLine[2].x=240,firstLine[2].y=280;
firstLine[3].x=280,firstLine[3].y=240;
firstLine[4].x=150,firstLine[4].y=230;
firstLine[5].x=330,firstLine[5].y=230;
firstLine[6].x=290,firstLine[6].y=170;
firstLine[7].x=290,firstLine[7].y=340;
firstLine[8].x=170,firstLine[8].y=400;
firstLine[9].x=260,firstLine[9].y=180;
int i;
for(i=0;i<firstnum;i++)
{
firstLine[i].code=encode(firstLine[i].x,firstLine[i].y);
}
}
//绘制裁剪窗口多边形
void drawPolygon()
{
int i;
glBegin(GL_LINE_LOOP);
for(i=0; i<4; i++)
{
glVertex2i(polygon[i].x,polygon[i].y);
}
glEnd();
glFlush();//刷新缓冲
Sleep(1000);
}
//绘制裁剪前线段
void drawFirstLine()
{
int i;
glBegin(GL_LINES);
for(i=0; i<firstnum; i++)
{
glVertex2i(firstLine[i].x,firstLine[i].y);
}
glEnd();
glFlush();//刷新缓冲
Sleep(1000);
}
//绘制裁剪后线段
void drawAfterLine()
{
int i;
glBegin(GL_LINES);
for(i=0; i<afternum; i++)
{
glVertex2i(afterLine[i].x,afterLine[i].y);
}
glEnd();
glFlush();//刷新缓冲
}
//裁剪算法
void cohen_Sutherland()
{
int i,code1,code2;
double x1,x2,y1,y2;
double k,xmin,xmax,ymin,ymax;
xmin=polygon[0].x,ymin=polygon[0].y;
xmax=polygon[2].x,ymax=polygon[2].y;
for(i=0;i<firstnum;i+=2)
{
x1=firstLine[i].x,y1=firstLine[i].y;
x2=firstLine[i+1].x,y2=firstLine[i+1].y;
code1=firstLine[i].code;
code2=firstLine[i+1].code;
if(code1&code2)//在裁剪窗口外
{
continue;
}
else if(code1==0&&code2==0)//在裁剪窗口内
{
afterLine[afternum++]=firstLine[i];
afterLine[afternum++]=firstLine[i+1];
}
else//需要分情况讨论
{
k=(y1-y2)*1.0/(x1-x2);//斜率
if(code1&1)//左边界
{
y1=y1+k*(xmin-x1);
x1=xmin;
code1=encode(x1,y1);
}
if(code2&1)
{
y2=y2+k*(xmin-x2);
x2=xmin;
code2=encode(x2,y2);
}
if(code1&2)//右边界
{
y1=y1+k*(xmax-x1);
x1=xmax;
code1=encode(x1,y1);
}
if(code2&2)
{
y2=y2+k*(xmax-x2);
x2=xmax;
code2=encode(x2,y2);
}
if(code1&4)//下边界
{
x1=(ymin-y1)/k+x1;
y1=ymin;
code1=encode(x1,y1);
}
if(code2&4)
{
x2=(ymin-y2)/k+x2;
y2=ymin;
code2=encode(x2,y2);
}
if(code1&8)//上边界
{
x1=(ymax-y1)/k+x1;
y1=ymax;
code1=encode(x1,y1);
}
if(code2&8)
{
x2=(ymax-y2)/k+x2;
y2=ymax;
code2=encode(x2,y2);
}
afterLine[afternum].x=x1;
afterLine[afternum++].y=y1;
afterLine[afternum].x=x2;
afterLine[afternum++].y=y2;
}
}
}
void display()
{
/*
* glClearColor表示清除颜色设为黑色
* glclear表示实际完成了把整个窗口清除为黑色的任务
*/
glClear(GL_COLOR_BUFFER_BIT);//清除缓冲器颜色
glColor3f(1.0,1,1);//设置画笔颜色
drawFirstLine();
drawPolygon();
glClear(GL_COLOR_BUFFER_BIT);//清除缓冲器颜色
drawPolygon();
cohen_Sutherland();
drawAfterLine();
glFlush();//刷新缓冲
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);//从main函数传递参数
//设置显示模式,单缓冲,RGB
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(0,0);//窗口位置
glutInitWindowSize(500,500);//窗口大小
glutCreateWindow("Cohen_Sutherland");//窗口标题
init();//自定义初始化函数
glutDisplayFunc(display);//渲染函数
glutMainLoop();//循环消息机制
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Cohen_Sutherland.rar (6个子文件)
Cohen_Sutherland
bin
Debug
Cohen_Sutherland.exe 953KB
Cohen_Sutherland.cbp 2KB
obj
Debug
main.o 17KB
Cohen_Sutherland.layout 504B
main.cpp 5KB
Cohen_Sutherland.depend 6KB
共 6 条
- 1
资源评论
- heyguy0072015-05-27很容易看懂,如果随意绘制直线也可以裁剪的话最好了
- beaterjy2017-05-18感觉还行吧
八月号
- 粉丝: 9
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功