C++实现基于EASYX库扫描线算法

时间:2022-04-26 04:51:20

本文实例为大家分享了C++实现基于EASYX库扫描线算法的具体代码,供大家参考,具体内容如下

扫描线算法的基本原理

C++实现基于EASYX库扫描线算法

* 作者在扫描线算法的基础上自己设计的更易于理解的地物填充绘制算法

流程图

C++实现基于EASYX库扫描线算法

代码

  1. #include<graphics.h>
  2. //#include<conio.h>
  3. #include<iostream>
  4. using namespace std;
  5. //-----------------------------草图形-----------------------------//
  6. void Grass(double x, double y, double hight, double width)
  7. {
  8. setlinecolor(GREEN);
  9. line(x, y, x - hight / 2, y - width);
  10. line(x, y, x, y - width);
  11. line(x, y, x + hight / 2, y - width);
  12. }
  13. //-----------------------------林图形-----------------------------//
  14. void Tree(double x, double y, double hight, double width)
  15. {
  16. setlinecolor(GREEN);
  17. line(x, y, x - hight / 2, y - width);
  18. line(x, y, x + hight / 2, y - width);
  19. line(x, y - width, x, y + width);
  20. }
  21. //------------------求解方程的k,b值---------------------//
  22. void function_line(double x1, double y1, double x2, double y2, double &k, double &b)
  23. {
  24. if (x2 - x1 == 0) {
  25. k = -9999;
  26. b = x1;
  27. return;
  28. }
  29. k = (y2 - y1) / (x2 - x1);
  30. b = y1 - k * x1;
  31. }
  32. //------------------返回直线的y----------------------//
  33. double solve_y(double k, double b, double x)
  34. {
  35. return k * x + b;
  36. }
  37. //------------------返回直线的x----------------------//
  38. double solve_x(double k, double b, double y)
  39. {
  40. if (k == -9999)
  41. return b;
  42. return (y - b) / k;
  43. }
  44. //-------------------找出扫描线与边界相交的点,并储存在suitable_x[]中,把相交的点数储存在n中--------------//
  45. //x[]-----------顶点x坐标
  46. //y[]-----------顶点y坐标
  47. //N-------------顶点的个数
  48. //now_y---------当前的扫描线y
  49. //suitable_x[]--扫描线与边界相交的点的x坐标
  50. //n-------------相交的点数
  51. void Suitable_x(double x[], double y[],int N, double now_y, double suitable_x[], int &n)
  52. {
  53. n = 0;
  54. int t = 0;
  55. double k, b;
  56. for (int i = 0; i < N; i++)
  57. {
  58. if (i == N - 1)
  59. {
  60. if (y[i] < now_y&& now_y <= y[0] || y[0] < now_y&& now_y <= y[i])//判断直线是否过最后一条直线
  61. {
  62. function_line(x[0], y[0], x[i], y[i], k, b);
  63. suitable_x[t++] = solve_x(k, b, now_y);
  64. }
  65. }
  66. else
  67. {
  68. if (y[i] < now_y&& now_y <= y[i + 1] || y[i + 1] < now_y&& now_y <= y[i]) //判断扫描线过哪条直线的范围
  69. {
  70. function_line(x[i], y[i], x[i + 1], y[i + 1], k, b);
  71. suitable_x[t++] = solve_x(k, b, now_y);//把交点的x储存在suitable_x[]中
  72. }
  73. }
  74. }
  75. n = t;
  76. }
  77. void sort_min_to_max(double suitable_x[],int n) //排序----从小到大排序
  78. {
  79. double temp;
  80. for (int i = 0; i < n - 1; i++)
  81. {
  82. for (int j = i + 1; j < n; j++) {
  83. if (suitable_x[i] > suitable_x[j]) {
  84. double temp = suitable_x[i];
  85. suitable_x[i] = suitable_x[j];
  86. suitable_x[j] = temp;
  87. }
  88. }
  89. }
  90. }
  91. double find_max(double elem[],int N) //输出最大的元素值
  92. {
  93. double max = elem[0];
  94. for (int i = 0; i < N; i++)
  95. {
  96. if (elem[i] > max)
  97. {
  98. max = elem[i];
  99. }
  100. }
  101. return max;
  102. }
  103. double find_min(double elem[],int N) //输出最小的元素值
  104. {
  105. double min = elem[0];
  106. for (int i = 0; i < N; i++)
  107. {
  108. if (elem[i] < min)
  109. {
  110. min = elem[i];
  111. }
  112. }
  113. return min;
  114. }
  115. int main()
  116. {
  117. cout << "-----------------------------------------地物填充绘制算法--------------------------------------------\n\n";
  118. cout << " 创建人:李景勃\n";
  119. cout << " 时间:2018年10月24日\n";
  120. cout << " 10月24日是全部程序员值得开心的节日,也是自己重要的日子,23号完成,祝福全部程序员们!!! \n\n\n";
  121.  
  122. int N,ch;
  123. char color;
  124. double x[50],y[50];
  125. cout << "输入填充的多边形的顶点数:";
  126. cin >> N;
  127. cout << "依次输入地物的顶点坐标:";
  128. for (int i = 0; i < N; i++)
  129. {
  130. cout << "输入第" << i+1 << "个顶点的坐标:" << endl;
  131. cout << "x,y:";
  132. scanf_s("%lf,%lf",&x[i],&y[i]);
  133. /*cout << "x = "; cin >> x[i];
  134. cout << "y = "; cin >> y[i];*/
  135. }
  136. cout << "选择您填充的地物: 整个区域单颜色充填---请按0;草地---请按 1;林地---请按 2 ; "; cin >> ch;
  137. if (ch == 0)
  138. {
  139. cout << "请选择颜色:BLACK 黑,DARKGRAY 深灰绿色 BLUE 蓝 LIGHTBLUE 亮蓝:GREEN 绿 CYAN 青 RED 红 MAGENTA 紫 BROWN 棕 YELLOW 黄 LIGHTGRAY 浅灰---注意是大写的英文\n ";
  140. cout << "如果想让颜色变亮请在颜色的前面加上LIGHT \n";
  141. cout << "请输入颜色:";
  142. cin >> color;
  143. }
  144.  
  145. initgraph(find_max(x, N)+100, find_max(y, N)+100);
  146. //int N = 9;
  147. //double x[50] = { 200,100,150,170,400,350,300,250,270 };
  148. //double y[50] = { 200,300,400,250,350,300,250,100,270 };
  149. double new_y[50] = { 0 };
  150. int n = 0;
  151. double suitable_x[10];
  152. //草的参数
  153. int hight = 12,
  154. width = 6;
  155. int space = 4; //草之间的间隔
  156.  
  157. setbkcolor(WHITE); //设置背景色
  158. cleardevice(); //用背景色填充屏幕
  159. setlinecolor(BLACK); //多边形的线条颜色
  160. for (int i = 0; i < N; i++) //逆时针顶点相连构成闭合多边形
  161. {
  162. if (i == N - 1)
  163. {
  164. line(x[i], y[i], x[0], y[0]);
  165. }
  166. else
  167. {
  168. line(x[i], y[i], x[i + 1], y[i + 1]);
  169. }
  170. }
  171.  
  172. double min_y = find_min(y,N),
  173. max_y = find_max(y,N);
  174. switch (ch)
  175. {
  176. case 0:
  177. //-------------------------像素填充面积-------------------------//
  178. for (int now_y = min_y; now_y <= max_y; now_y++)
  179. {
  180. Suitable_x(x, y, N, now_y, suitable_x, n);
  181. sort_min_to_max(suitable_x, n); //从小到大排序
  182. for (int i = 0; i < n; i += 2)
  183. {
  184. for (int now_x = suitable_x[i]; now_x < suitable_x[i + 1]; now_x++)//确定在扫描线上,要填充的范围
  185. {
  186. putpixel(now_x, now_y, color);
  187. }
  188. }
  189. }
  190. break;
  191. case 1:
  192. //-------------------------草填充-------------------------//
  193. for (int now_y = min_y + width + space; now_y <= max_y; now_y += width + space)
  194. {
  195. Suitable_x(x, y, N, now_y, suitable_x, n);
  196. sort_min_to_max(suitable_x, n); //从小到大排序
  197. for (int i = 0; i < n; i += 2)
  198. {
  199. for (int now_x = suitable_x[i] + hight / 2 + space + space; now_x < suitable_x[i + 1] - hight / 2; now_x += hight + space)//确定在扫描线上,要填充的范围
  200. {
  201. Grass(now_x, now_y, hight, width);
  202. Sleep(60);
  203. }
  204. }
  205. }
  206. break;
  207. case 2:
  208. //-------------------------树填充-------------------------//
  209. for (int now_y = min_y + width + space; now_y <= max_y; now_y += 2 * width + space)
  210. {
  211. Suitable_x(x, y, N, now_y, suitable_x, n);
  212. sort_min_to_max(suitable_x, n); //从小到大排序
  213. for (int i = 0; i < n; i += 2)
  214. {
  215. double longth = suitable_x[i + 1] - suitable_x[i]-space;
  216. int num = longth / (hight+space);
  217. //double space_tmp = longth - num * (hight + space);
  218. //double space_ = space + space / num;
  219. for (int now_x = suitable_x[i] + hight / 2 +space+space; now_x < suitable_x[i + 1] - hight / 2&&num--; now_x += hight + space)//确定在扫描线上,要填充的范围
  220. {
  221. Tree(now_x, now_y, hight, width);
  222. Sleep(60);
  223. }
  224. }
  225. }
  226. break;
  227. default:
  228. cout << "填充失败!!!" << endl;
  229. break;
  230. }
  231. //closegraph();//关闭画框
  232. system("pause");
  233. return 0;
  234. }

实现效果

C++实现基于EASYX库扫描线算法

C++实现基于EASYX库扫描线算法

C++实现基于EASYX库扫描线算法

C++实现基于EASYX库扫描线算法

谢谢阅读,欢迎来一起交流,一起学习!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/weixin_42878758/article/details/83714430