已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#include #include #include #include #define EPSILON 0. /最小浮点数/点结构体struct Point int x; /x坐标 int y; /y坐标;/线结构体struct Line Point high_point; /高端点 Point low_point; /低端点 int is_active; /是否为有效边,水平边(0),非水平边(1) double inverse_k; /斜率k的倒数;/边结点struct EdgeNode double x; /扫描线与边交点的x坐标(边的低端点的x坐标) int y_max; /边的高端点的y坐标ymax double inverse_k; /斜率k的倒数 EdgeNode *next; /下一个边结点的指针;/有效边表struct ActiveEdgeTable int y; /扫描线y EdgeNode *head; /边链表的头指针;/桶结点typedef struct Bucket int y; /扫描线y EdgeNode *head; /边链表的头指针 Bucket *next; /下一个桶的指针 EdgeTable;int compare(Point p1, Point p2);Line* create_lines(Point points, int n);Point get_lowest_point(Line lines, int n);Point get_highest_point(Line lines, int n);void swap(Line &l1, Line &l2);void sort(Line lines, int n);EdgeTable* create_edge_table(Line lines, int n);ActiveEdgeTable* init_active_table(EdgeTable *edge_table);void delete_edge(ActiveEdgeTable *active_table, int y_max);void add_edge(ActiveEdgeTable *active_table, EdgeNode edge);ActiveEdgeTable* update_active_table(ActiveEdgeTable *active_table, EdgeTable *edge_table);void DrawPolygon(Point points, int n);void DrawGrid(int x, int y);void Fill(Point points, int n);void Initial();void Display();int main(int argc, char* argv)glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(400, 300);glutInitWindowPosition(100, 120);glutCreateWindow(Polygon Filling);glutDisplayFunc(Display);Initial();glutMainLoop(); return 0;/比较2个点的高度int compare(Point p1, Point p2) if (p1.y p2.y) return 1; else if (p1.y = p2.y) return 0; return -1;/由点数组生成线段数组Line* create_lines(Point points, int n)Line *lines = (Line*)malloc(n * sizeof(Line);for (int i = 0; i 0 ? p1 : p2;linesi.low_point = result 0 ? p1 : p2;linesi.inverse_k = (double)(p2.x - p1.x) / (double)(p2.y - p1.y);return lines;/获取线数组中最低的端点Point get_lowest_point(Line lines, int n) Point lowest_point = lines0.low_point; for (int i = 1; i 0) lowest_point = low_point; return lowest_point;/获取线数组中最高的端点Point get_highest_point(Line lines, int n) Point highest_point = lines0.high_point; for (int i = 1; i n; +i) Point high_point = linesi.high_point; if (compare(highest_point, high_point) 0) highest_point = high_point; return highest_point;/交换2个Line对象void swap(Line &l1, Line &l2) Line temp = l1; l1 = l2; l2 = temp;/对线数组进行排序void sort(Line lines, int n)/先按低端点的y坐标进行升序排序 for (int i = 0; i n; +i) int min_index = i; for (int j = i + 1; j n; +j) if (linesj.low_point.y linesmin_index.low_point.y) min_index = j; swap(linesi, linesmin_index); /再将有序数组按低端点的x坐标升序排列,若x坐标相等,按inverse_k升序 for (i = 0; i n; +i) int min_index = i; for (int j = i + 1; linesj.low_point.y = linesi.low_point.y; +j) if (linesj.low_point.x 0 & linesi.low_point.x = linesi - 1.low_point.x) if (linesi.is_active = 1 & linesi - 1.is_active = 1) if (linesi.inverse_k head = NULL;edge_table-next = NULL;sort(lines, n);Point lowest_point = get_lowest_point(lines, n);Point highest_point = get_highest_point(lines, n);EdgeTable *s = edge_table;for (int i = lowest_point.y; i y = i;bucket-next = NULL;bucket-head = (EdgeNode*)malloc(sizeof(EdgeNode);bucket-head-next = NULL;EdgeNode *p = bucket-head;for (int j = 0; j x = linesj.low_point.x;q-y_max = linesj.high_point.y;q-inverse_k = linesj.inverse_k;q-next = NULL;p-next = q;p = q;s-next = bucket;s = bucket;return edge_table;/从边表中取出第一个不为空的桶初始化有效边表ActiveEdgeTable* init_active_table(EdgeTable *edge_table) ActiveEdgeTable *active_table = (ActiveEdgeTable*)malloc(sizeof(ActiveEdgeTable); active_table-y = edge_table-next-y; active_table-head = (EdgeNode*)malloc(sizeof(EdgeNode); active_table-head-next = NULL; EdgeNode *p = edge_table-next-head; EdgeNode *q = active_table-head; while (p-next != NULL) EdgeNode *s = (EdgeNode*)malloc(sizeof(EdgeNode); s-x = p-next-x; s-y_max = p-next-y_max; s-inverse_k = p-next-inverse_k; s-next = NULL; q-next = s; q = s; p = p-next; return active_table;/从有效边表中删除指定y_max的边结点void delete_edge(ActiveEdgeTable *active_table, int y_max) EdgeNode *p = active_table-head; while (p-next != NULL) EdgeNode *q = p-next; if (q-y_max = y_max) p-next = q-next; free(q); else p = p-next; /将一个边结点按次序添加到有效边表中void add_edge(ActiveEdgeTable *active_table, EdgeNode edge) EdgeNode *t = (EdgeNode*)malloc(sizeof(EdgeNode); t-x = edge.x; t-y_max = edge.y_max; t-inverse_k = edge.inverse_k; t-next = NULL; EdgeNode *p = active_table-head; while (p-next != NULL) EdgeNode *q = p-next; if (edge.x x) | (edge.x = q-x & edge.inverse_k inverse_k) p-next = t; t-next = q; return; p = p-next; p-next = t;/更新有效边表,并与边表中对应的桶合并ActiveEdgeTable* update_active_table(ActiveEdgeTable *active_table, EdgeTable *edge_table)/更新扫描线y +active_table-y; /删除y=ymax的边 delete_edge(active_table, active_table-y);/更新边结点的数据 EdgeNode *p = active_table-head-next; while (p != NULL) p-x += p-inverse_k; p = p-next; /找到边表中对应的桶 EdgeTable *q = edge_table; while (q = q-next) != NULL & q-y != active_table-y);/如果找到,则进行合并 if (q != NULL) EdgeNode *s = q-head; while (s = s-next) != NULL) add_edge(active_table, *s); return active_table;/画出多边形的边框void DrawPolygon(Point points, int n)glBegin(GL_LINE_LOOP);for (int i = 0; i n; +i)glVertex2i(pointsi.x, pointsi.y);glEnd();/画出x * y的网格void DrawGrid(int x, int y)glBegin(GL_LINES);/横线for (int i = 0; i = y; +i)glVertex2d(0, i);glVertex2d(x, i);/竖线for (i = 0; i head-next != NULL) EdgeNode *p = active_table-head;int b = -1; while (p-next != NULL) if (b 0)int left = p-x;int right = p-next-x;/如果不是局部最低点,则进行边界处理if (!(p-x - p-next-x = -EPSILON & p-x - p-next-x x - left = -EPSILON & p-x - left next-x - right = -EPSILON & p-next-x - right = EPSILON)right -= 1;for (int i = left; i y);glEnd();glFlush();Sleep(50);p =
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 流域智慧水务平台方案
- 2026中国卫星网络集团有限公司校园招聘备考题库附答案详解(培优)
- 2026年安徽大学专职辅导员招聘16人备考题库及答案详解(名师系列)
- 2026中国生物纪检巡察岗位社会招聘备考题库附答案详解(达标题)
- 2026广西桂林理工大学资产经营有限公司招聘备考题库附答案详解
- 人防施工组织方案
- 桥梁架梁施工方案
- 2026年福建省南安市丰州中心幼儿园招聘幼儿教师备考题库及参考答案详解1套
- 2026浙江温州市瑞安市南滨街道办事处招聘劳务派遣人员1人备考题库及1套完整答案详解
- 公益性公墓生态修复方案
- 第2课《生涯规划 筑梦未来》第1框《认识职业生涯》(课件+视频)中职思想政治《心理健康与职业生涯》(高教版2023·基础模块)
- SYT 6688-2013 时频电磁法勘探技术规程
- 桥式起重机定期检查记录表
- 雷蒙磨培训课件
- (0~1 500)℃钨铼热电偶校准规范
- 生产日报表模板
- 消防维保方案(消防维保服务)(技术标)
- GB/T 43084.2-2023塑料含氟聚合物分散体、模塑和挤出材料第2部分:试样制备和性能测定
- GB/T 713.1-2023承压设备用钢板和钢带第1部分:一般要求
- 新松agc小车控制台tc操作手册
- 退保证金说明转账方式提供退保证金说明
评论
0/150
提交评论