Tubor C 直线裁剪算法 Cohen-Sutherland算法 (可运行代码)
来源:互联网 发布:绘画小区平面图软件 编辑:程序博客网 时间:2024/06/05 23:58
因为TuborC 程序的编辑页面的宽度最多是250,所以为了控制edit时候的代码宽度,所以直接全部左对齐。
(下面程序暂时不能裁剪 斜率为零 或者 斜率不存在的情况, 近期将完善更新)
#include <graphics.h> #include <stdio.h> int x_min,x_max,y_min, y_max; main() { int driver,mode; int x1,y1,x2,y2; x_min=150; y_min=100; x_max=500; y_max=300; driver=DETECT; initgraph(&driver,&mode,"c:\\tc\\bgi"); draw_win(3); x1=60;y1=120;x2=580;y2=270; line(x1,480-y1,x2,480-y2); getch(); clipline(x1,y1,x2,y2); x1=200;y1=360;x2=280;y2=250; setcolor(7); line(x1,480-y1,x2,480-y2); getch(); clipline(x1,y1,x2,y2); x1=350;y1=170;x2=550;y2=200; setcolor(7); line(x1,480-y1,x2,480-y2); getch(); clipline(x1,y1,x2,y2); x1=260;y1=150;x2=220;y2=60; setcolor(7); line(x1,480-y1,x2,480-y2); getch(); clipline(x1,y1,x2,y2); x1=50;y1=150;x2=50;y2=150; setcolor(7); line(x1,480-y1,x2,480-y2); getch(); clipline(x1,y1,x2,y2); x1=150;y1=50;x2=150;y2=400; setcolor(7); line(x1,480-y1,x2,480-y2); getch(); clipline(x1,y1,x2,y2); getch(); closegraph(); } clipline(int x1,int y1,int x2,int y2) { int i,code1[4],code2[2],display; float k; display=0; k = (float)(y2-y1)/(float)(x2-x1); while(1) { encode(x1,y1,code1); encode(x2,y2,code2); if(accept(code1,code2)) { display=1; break; } else if(reject(code1,code2)) { break; } if(swapyn(code1,code2)==1){int t;t = x1;x1 = x2;x2 = t;t = y1;y1 = y2;y2 = t;} if(code1[0]) {x1 = x1 + (y_max-y1)/k;y1 = y_max;}else if(code1[1]){x1 = x1 + (y_min-y1)/k;y1 = y_min;}else if(code1[2]){y1 = y1 + (x_max-x1)*k; x1 = x_max;}else if(code1[3]){y1 = y1 + (x_min-x1)*k;x1 = x_min;}} if(display==1) {setcolor(4); line(x1,480-y1,x2,480-y2); }} encode(int x,int y,int code[]){int i;for(i=0; i<4; i++) code[i]=0;if(y > y_max) code[0]=1;if(y < y_min) code[1]=1;if(x > x_max) code[2]=1;if(x < x_min) code[3]=1;}accept(int code1[],int code2[]) { int i,flag; flag=1; for(i=0;i<4;i++) if((code1[i]==1)||(code2[i]==1)) { flag=0; break; } return flag; } reject(int code1[],int code2[]) { int i,flag; flag=0; for(i=0;i<4;i++) if((code1[i]==1)&&(code2[i]==1)) { flag=1; break; } return flag; } swapyn(int code1[],int code2[]){int i,flag1,flag2,tmp;flag1=0;if(code1[0]+code1[1]+code1[2]+code1[3] !=0)flag1 = 1;flag2=0;if(code2[0]+code2[1]+code2[2]+code2[3] !=0)flag2 = 1;if(flag1==1 && flag2==1) return 0;else if(flag1==0 && flag2==1) { for(i=0; i<4; i++) { tmp = code1[i]; code1[i] = code2[i]; code2[i] = tmp;}return 1; } elsereturn 0;}draw_win(int c) { setcolor(c); line(x_min,480-y_min,x_max,480-y_min); line(x_max,480-y_min,x_max,480-y_max); line(x_max,480-y_max,x_min,480-y_max); line(x_min,480-y_max,x_min,480-y_min); setcolor(7); getch(); }
阅读全文
0 0
- Tubor C 直线裁剪算法 Cohen-Sutherland算法 (可运行代码)
- 计算机图形学-直线裁剪(Cohen-Sutherland编码裁剪算法)
- opengl 直线裁剪Cohen-Sutherland算法
- Cohen-Sutherland直线段的裁剪算法
- Cohen-Sutherland裁剪算法
- Cohen-Sutherland裁剪算法
- Cohen-SutherLand 裁剪算法 (vc++)
- 直线矩形视口裁剪算法 Cohen–Sutherland algorithm
- Cohen-Sutherland线段裁剪算法
- Cohen-Sutherland算法线段裁剪
- Cohen-Sutherland线段裁剪算法
- Cohen-Sutherland线段裁剪算法
- Cohen-Sutherland算法裁剪线段
- [OpenGL]计算机图形学:直线裁剪算法中Cohen-Sutherland算法和Liang-Barsky算法
- Cohen Sutherland线段裁剪算法(C#实现)
- Cohen-Sutherland线段裁剪算法
- Cohen-Sutherland 窗口裁剪线段算法
- 计算机图形学 - 线段裁剪 - Cohen Sutherland算法
- 2017年薪酬最高的15门编程语言,GO夺冠
- 大数据早报:日本三菱公司再曝数据造假丑闻 Splunk机器学习帮助制药公司治疗遗传疾病(11.28)
- 数字图像处理 | 一些成像方式
- python操作redis
- python学习总结---python注释方法
- Tubor C 直线裁剪算法 Cohen-Sutherland算法 (可运行代码)
- Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
- Eureka单节点及高可用(详细配置)
- 通过Function Score Query优化Elasticsearch搜索结果(综合排序)
- 实习总结三:文件迁移
- 个人订阅号认证步骤,个人公众号如何认证看这里巧妙绕过腾讯规则2天搞定认证
- 项目和中的前端页面
- 【Java】2.垃圾回收机制 读书笔记
- 通威太阳能打造全球最大太阳能电池“智慧工厂”