欢迎来到天天文库
浏览记录
ID:55137307
大小:12.14 KB
页数:11页
时间:2020-04-28
《计算机图形学-有效边表算法源代码.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#include#include#defineEPSILON0.//最小浮点数//点结构体structPoint{intx;//x坐标inty;//y坐标};//线结构体structLine{Pointhigh_point;//高端点Pointlow_point;//低端点intis_active;//是否为有效边,水平边(0),非水平边(1)doubleinverse_k;//斜率k的倒数};//边结点structEdgeNode{
2、doublex;//扫描线与边交点的x坐标(边的低端点的x坐标)inty_max;//边的高端点的y坐标ymaxdoubleinverse_k;//斜率k的倒数EdgeNode*next;//下一个边结点的指针};//有效边表structActiveEdgeTable{inty;//扫描线yEdgeNode*head;//边链表的头指针};//桶结点typedefstructBucket{inty;//扫描线yEdgeNode*head;//边链表的头指针Bucket*next;//下一个桶的指针}EdgeTable;intcompare(Poi
3、ntp1,Pointp2);Line*create_lines(Pointpoints[],intn);Pointget_lowest_point(Linelines[],intn);Pointget_highest_point(Linelines[],intn);voidswap(Line&l1,Line&l2);voidsort(Linelines[],intn);EdgeTable*create_edge_table(Linelines[],intn);ActiveEdgeTable*init_active_table(EdgeTable*
4、edge_table);voiddelete_edge(ActiveEdgeTable*active_table,inty_max);voidadd_edge(ActiveEdgeTable*active_table,EdgeNodeedge);ActiveEdgeTable*update_active_table(ActiveEdgeTable*active_table,EdgeTable*edge_table);voidDrawPolygon(Pointpoints,intn);voidDrawGrid(intx,inty);voidFill
5、(Pointpoints[],intn);voidInitial();voidDisplay();intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE
6、GLUT_RGB);glutInitWindowSize(400,300);glutInitWindowPosition(100,120);glutCreateWindow("PolygonFilling");glutDisplayFunc(Display);Initial();glutM
7、ainLoop();return0;}//比较2个点的高度intcompare(Pointp1,Pointp2){if(p1.y>p2.y)return1;elseif(p1.y==p2.y)return0;return-1;}//由点数组生成线段数组Line*create_lines(Pointpoints[],intn){Line*lines=(Line*)malloc(n*sizeof(Line));for(inti=0;i8、=compare(p1,p2);if(result==0)lines[i].is_active=0;elselines[i].is_active=1;lines[i].high_point=result>0?p1:p2;lines[i].low_point=result<0?p1:p2;lines[i].inverse_k=(double)(p2.x-p1.x)/(double)(p2.y-p1.y);}returnlines;}//获取线数组中最低的端点Pointget_lowest_point(Linelines[],intn){Pointl9、owest_point=lines[0].low_point;for(inti=1;i
8、=compare(p1,p2);if(result==0)lines[i].is_active=0;elselines[i].is_active=1;lines[i].high_point=result>0?p1:p2;lines[i].low_point=result<0?p1:p2;lines[i].inverse_k=(double)(p2.x-p1.x)/(double)(p2.y-p1.y);}returnlines;}//获取线数组中最低的端点Pointget_lowest_point(Linelines[],intn){Pointl
9、owest_point=lines[0].low_point;for(inti=1;i
此文档下载收益归作者所有