Guest User

Untitled

a guest
Aug 15th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 KB | None | 0 0
  1. typedef struct point {
  2. double x,y;
  3. } POINT;
  4.  
  5. typedef struct ray {
  6. POINT p,v;
  7. } RAY;
  8.  
  9. typedef struct polygon {
  10. int n;
  11. POINT *v;
  12. } POLYGON;
  13.  
  14. /*
  15. * This function implements the Jordan-curve test.
  16. * It returns 1 if the point is inside the polygon,
  17. * and 0 if it is outside.
  18. */
  19. static int pointIsInside(POINT *pt, POLYGON *poly) {
  20. int i, j, c = 0;
  21. int n = poly->n;
  22. POINT *v = poly->v;
  23. double x = pt->x;
  24. double y = pt->y;
  25.  
  26. for (i = 0, j = n-1; i < n; j = i++) {
  27. /*
  28. * Flip a bit for each edge that:
  29. * straddles the x-axis (going up or going down), and
  30. * intersects the x-axis at a positive value of x.
  31. */
  32. if ((((v[i].y <= y) && (y < v[j].y)) ||
  33. ((v[j].y <= y) && (y < v[i].y))) &&
  34. (x < (v[j].x - v[i].x) * (y - v[i].y) / (v[j].y - v[i].y) + v[i].x))
  35.  
  36. c = !c;
  37. }
  38. return c;
  39. }
  40.  
  41. static void one_drawer(unsigned char *buf, unsigned x, unsigned y, unsigned w, unsigned h) {
  42. POINT a[6] = {
  43. {.x = 1, .y = 1},
  44. {.x = 2, .y = 0},
  45. {.x = 8, .y = 0},
  46. {.x = 9, .y = 1},
  47. {.x = 8, .y = 2},
  48. {.x = 2, .y = 2}
  49. };
  50.  
  51. POLYGON polygon_a = {
  52. .n = 6,
  53. .v = a
  54. };
  55.  
  56.  
  57. POINT b[6] = {
  58. {.x = 9, .y = 1},
  59. {.x = 10,.y = 2},
  60. {.x = 10,.y = 8},
  61. {.x = 9, .y = 9},
  62. {.x = 8, .y = 8},
  63. {.x = 8, .y = 2}
  64. };
  65.  
  66. POLYGON polygon_b = {
  67. .n = 6,
  68. .v = b
  69. };
  70.  
  71.  
  72. POINT c[6] = {
  73. {.x = 9, .y = 9},
  74. {.x = 10,.y = 10},
  75. {.x = 10,.y = 16},
  76. {.x = 9, .y = 17},
  77. {.x = 8, .y = 16},
  78. {.x = 8, .y = 10}
  79. };
  80.  
  81. POLYGON polygon_c = {
  82. .n = 6,
  83. .v = c
  84. };
  85.  
  86. POINT d[6] = {
  87. {.x = 9, .y = 17},
  88. {.x = 8, .y = 18},
  89. {.x = 2, .y = 18},
  90. {.x = 1, .y = 17},
  91. {.x = 2, .y = 16},
  92. {.x = 8, .y = 16}
  93. };
  94.  
  95. POLYGON polygon_d = {
  96. .n = 6,
  97. .v = d
  98. };
  99.  
  100. POINT e[6] = {
  101. {.x = 1, .y = 17},
  102. {.x = 0, .y = 16},
  103. {.x = 0, .y = 10},
  104. {.x = 1, .y = 9},
  105. {.x = 2, .y = 10},
  106. {.x = 2, .y = 16}
  107. };
  108.  
  109. POLYGON polygon_e = {
  110. .n = 6,
  111. .v = e
  112. };
  113.  
  114.  
  115. POINT f[6] = {
  116. {.x = 1, .y = 9},
  117. {.x = 0, .y = 8},
  118. {.x = 0, .y = 2},
  119. {.x = 1, .y = 1},
  120. {.x = 2, .y = 2},
  121. {.x = 2, .y = 8}
  122. };
  123.  
  124. POLYGON polygon_f = {
  125. .n = 6,
  126. .v = f
  127. };
  128.  
  129.  
  130. POINT g[6] = {
  131. {.x = 1, .y = 9},
  132. {.x = 2, .y = 8},
  133. {.x = 8, .y = 8},
  134. {.x = 9, .y = 9},
  135. {.x = 8, .y = 10},
  136. {.x = 2, .y = 10}
  137. };
  138.  
  139. POLYGON polygon_g = {
  140. .n = 6,
  141. .v = g
  142. };
  143.  
  144. POINT z[6] = {
  145. {.x = -1,.y = -1},
  146. {.x = -1,.y = -1},
  147. {.x = -1,.y = -1},
  148. {.x = -1,.y = -1},
  149. {.x = -1,.y = -1},
  150. {.x = -1,.y = -1}
  151. };
  152.  
  153. POLYGON polygon_z = {
  154. .n = 6,
  155. .v = z
  156. };
  157.  
  158.  
  159. POLYGON polygons[7] = {0};
  160. POINT p = {.x = x, .y = y};
  161. int inside = 0;
  162.  
  163. POINT temp_point[6] = {0};
  164.  
  165. POLYGON temp_polygon = {
  166. .n = 6,
  167. .v = temp_point
  168. };
  169.  
  170. char str[] = "20180909120909";
  171. // char str[] = "8";
  172. unsigned x_offset_base = 10;
  173. unsigned y_offset_base = 50;
  174. for (unsigned str_index = 0; str_index<strlen(str); ++str_index) {
  175. unsigned index = 0;
  176. switch (str[str_index]) {
  177. case '0':
  178. polygons[index++] = polygon_a;
  179. polygons[index++] = polygon_b;
  180. polygons[index++] = polygon_c;
  181. polygons[index++] = polygon_d;
  182. polygons[index++] = polygon_e;
  183. polygons[index++] = polygon_f;
  184. polygons[index++] = polygon_z;
  185. break;
  186. case '1':
  187. polygons[index++] = polygon_z;
  188. polygons[index++] = polygon_b;
  189. polygons[index++] = polygon_c;
  190. break;
  191. case '2':
  192. polygons[index++] = polygon_a;
  193. polygons[index++] = polygon_b;
  194. polygons[index++] = polygon_z;
  195. polygons[index++] = polygon_d;
  196. polygons[index++] = polygon_e;
  197. polygons[index++] = polygon_z;
  198. polygons[index++] = polygon_g;
  199. break;
  200. case '3':
  201. polygons[index++] = polygon_a;
  202. polygons[index++] = polygon_b;
  203. polygons[index++] = polygon_c;
  204. polygons[index++] = polygon_d;
  205. polygons[index++] = polygon_z;
  206. polygons[index++] = polygon_z;
  207. polygons[index++] = polygon_g;
  208. break;
  209. case '4':
  210. polygons[index++] = polygon_z;
  211. polygons[index++] = polygon_b;
  212. polygons[index++] = polygon_c;
  213. polygons[index++] = polygon_z;
  214. polygons[index++] = polygon_z;
  215. polygons[index++] = polygon_f;
  216. polygons[index++] = polygon_g;
  217. break;
  218. case '5':
  219. polygons[index++] = polygon_a;
  220. polygons[index++] = polygon_z;
  221. polygons[index++] = polygon_c;
  222. polygons[index++] = polygon_d;
  223. polygons[index++] = polygon_z;
  224. polygons[index++] = polygon_f;
  225. polygons[index++] = polygon_g;
  226. break;
  227. case '6':
  228. polygons[index++] = polygon_a;
  229. polygons[index++] = polygon_z;
  230. polygons[index++] = polygon_c;
  231. polygons[index++] = polygon_d;
  232. polygons[index++] = polygon_e;
  233. polygons[index++] = polygon_f;
  234. polygons[index++] = polygon_g;
  235. break;
  236. case '7':
  237. polygons[index++] = polygon_a;
  238. polygons[index++] = polygon_b;
  239. polygons[index++] = polygon_c;
  240. break;
  241. case '8':
  242. polygons[index++] = polygon_a;
  243. polygons[index++] = polygon_b;
  244. polygons[index++] = polygon_c;
  245. polygons[index++] = polygon_d;
  246. polygons[index++] = polygon_e;
  247. polygons[index++] = polygon_f;
  248. polygons[index++] = polygon_g;
  249. break;
  250. case '9':
  251. polygons[index++] = polygon_a;
  252. polygons[index++] = polygon_b;
  253. polygons[index++] = polygon_c;
  254. polygons[index++] = polygon_d;
  255. polygons[index++] = polygon_z;
  256. polygons[index++] = polygon_f;
  257. polygons[index++] = polygon_g;
  258. break;
  259. default:
  260. break;
  261. }
  262. for (unsigned i = 0; i < index; ++i) {
  263. for (unsigned z = 0; z < temp_polygon.n; ++z) {
  264. temp_point[z] = polygons[i].v[z];
  265. }
  266. for (unsigned j = 0; j < temp_polygon.n; ++j) {
  267. temp_polygon.v[j].x *= 2;
  268. temp_polygon.v[j].y *= 2;
  269.  
  270. temp_polygon.v[j].x += x_offset_base + str_index * 30;
  271. temp_polygon.v[j].y += y_offset_base;
  272.  
  273. if (i == 0 || i == 3 || i == 6) {
  274. temp_polygon.v[j].x += 1;
  275. }
  276. if (i == 1 || i == 2) {
  277. temp_polygon.v[j].x += 1 * 2;
  278. }
  279. if (i == 1 || i == 5) {
  280. temp_polygon.v[j].y += 1;
  281. }
  282. if (i == 6) {
  283. temp_polygon.v[j].y += 1 * 2;
  284. }
  285. if (i == 2 || i == 4) {
  286. temp_polygon.v[j].y += 1 * 3;
  287. }
  288. if (i == 3) {
  289. temp_polygon.v[j].y += 1 * 4;
  290. }
  291. }
  292.  
  293. inside = pointIsInside(&p, &temp_polygon);
  294. if (inside > 0) {
  295. break;
  296. }
  297.  
  298. }
  299. if (inside > 0) {
  300. break;
  301. }
  302. }
  303. if (inside > 0) {
  304. fill_color(buf, 0xFF, 0xFF, 0xFF);
  305. } else {
  306. fill_color(buf, 0, 0, 0);
  307. }
  308.  
  309. }
Add Comment
Please, Sign In to add comment