u8g2_polygon.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. u8g22_polygon.c
  3. */
  4. #include "u8g2.h"
  5. /*===========================================*/
  6. /* local definitions */
  7. typedef int16_t pg_word_t;
  8. struct pg_point_struct
  9. {
  10. pg_word_t x;
  11. pg_word_t y;
  12. };
  13. typedef struct _pg_struct pg_struct; /* forward declaration */
  14. struct pg_edge_struct
  15. {
  16. pg_word_t x_direction; /* 1, if x2 is greater than x1, -1 otherwise */
  17. pg_word_t height;
  18. pg_word_t current_x_offset;
  19. pg_word_t error_offset;
  20. /* --- line loop --- */
  21. pg_word_t current_y;
  22. pg_word_t max_y;
  23. pg_word_t current_x;
  24. pg_word_t error;
  25. /* --- outer loop --- */
  26. uint8_t (*next_idx_fn)(pg_struct *pg, uint8_t i);
  27. uint8_t curr_idx;
  28. };
  29. /* maximum number of points in the polygon */
  30. /* can be redefined, but highest possible value is 254 */
  31. #define PG_MAX_POINTS 6
  32. /* index numbers for the pge structures below */
  33. #define PG_LEFT 0
  34. #define PG_RIGHT 1
  35. struct _pg_struct
  36. {
  37. struct pg_point_struct list[PG_MAX_POINTS];
  38. uint8_t cnt;
  39. uint8_t is_min_y_not_flat;
  40. pg_word_t total_scan_line_cnt;
  41. struct pg_edge_struct pge[2]; /* left and right line draw structures */
  42. };
  43. /*===========================================*/
  44. /* procedures, which should not be inlined (save as much flash ROM as possible */
  45. #define PG_NOINLINE U8G2_NOINLINE
  46. static uint8_t pge_Next(struct pg_edge_struct *pge) PG_NOINLINE;
  47. static uint8_t pg_inc(pg_struct *pg, uint8_t i) PG_NOINLINE;
  48. static uint8_t pg_dec(pg_struct *pg, uint8_t i) PG_NOINLINE;
  49. static void pg_expand_min_y(pg_struct *pg, pg_word_t min_y, uint8_t pge_idx) PG_NOINLINE;
  50. static void pg_line_init(pg_struct * const pg, uint8_t pge_index) PG_NOINLINE;
  51. /*===========================================*/
  52. /* line draw algorithm */
  53. static uint8_t pge_Next(struct pg_edge_struct *pge)
  54. {
  55. if ( pge->current_y >= pge->max_y )
  56. return 0;
  57. pge->current_x += pge->current_x_offset;
  58. pge->error += pge->error_offset;
  59. if ( pge->error > 0 )
  60. {
  61. pge->current_x += pge->x_direction;
  62. pge->error -= pge->height;
  63. }
  64. pge->current_y++;
  65. return 1;
  66. }
  67. /* assumes y2 > y1 */
  68. static void pge_Init(struct pg_edge_struct *pge, pg_word_t x1, pg_word_t y1, pg_word_t x2, pg_word_t y2)
  69. {
  70. pg_word_t dx = x2 - x1;
  71. pg_word_t width;
  72. pge->height = y2 - y1;
  73. pge->max_y = y2;
  74. pge->current_y = y1;
  75. pge->current_x = x1;
  76. if ( dx >= 0 )
  77. {
  78. pge->x_direction = 1;
  79. width = dx;
  80. pge->error = 0;
  81. }
  82. else
  83. {
  84. pge->x_direction = -1;
  85. width = -dx;
  86. pge->error = 1 - pge->height;
  87. }
  88. pge->current_x_offset = dx / pge->height;
  89. pge->error_offset = width % pge->height;
  90. }
  91. /*===========================================*/
  92. /* convex polygon algorithm */
  93. static uint8_t pg_inc(pg_struct *pg, uint8_t i)
  94. {
  95. i++;
  96. if ( i >= pg->cnt )
  97. i = 0;
  98. return i;
  99. }
  100. static uint8_t pg_dec(pg_struct *pg, uint8_t i)
  101. {
  102. i--;
  103. if ( i >= pg->cnt )
  104. i = pg->cnt-1;
  105. return i;
  106. }
  107. static void pg_expand_min_y(pg_struct *pg, pg_word_t min_y, uint8_t pge_idx)
  108. {
  109. uint8_t i = pg->pge[pge_idx].curr_idx;
  110. for(;;)
  111. {
  112. i = pg->pge[pge_idx].next_idx_fn(pg, i);
  113. if ( pg->list[i].y != min_y )
  114. break;
  115. pg->pge[pge_idx].curr_idx = i;
  116. }
  117. }
  118. static uint8_t pg_prepare(pg_struct *pg)
  119. {
  120. pg_word_t max_y;
  121. pg_word_t min_y;
  122. uint8_t i;
  123. /* setup the next index procedures */
  124. pg->pge[PG_RIGHT].next_idx_fn = pg_inc;
  125. pg->pge[PG_LEFT].next_idx_fn = pg_dec;
  126. /* search for highest and lowest point */
  127. max_y = pg->list[0].y;
  128. min_y = pg->list[0].y;
  129. pg->pge[PG_LEFT].curr_idx = 0;
  130. for( i = 1; i < pg->cnt; i++ )
  131. {
  132. if ( max_y < pg->list[i].y )
  133. {
  134. max_y = pg->list[i].y;
  135. }
  136. if ( min_y > pg->list[i].y )
  137. {
  138. pg->pge[PG_LEFT].curr_idx = i;
  139. min_y = pg->list[i].y;
  140. }
  141. }
  142. /* calculate total number of scan lines */
  143. pg->total_scan_line_cnt = max_y;
  144. pg->total_scan_line_cnt -= min_y;
  145. /* exit if polygon height is zero */
  146. if ( pg->total_scan_line_cnt == 0 )
  147. return 0;
  148. /* if the minimum y side is flat, try to find the lowest and highest x points */
  149. pg->pge[PG_RIGHT].curr_idx = pg->pge[PG_LEFT].curr_idx;
  150. pg_expand_min_y(pg, min_y, PG_RIGHT);
  151. pg_expand_min_y(pg, min_y, PG_LEFT);
  152. /* check if the min side is really flat (depends on the x values) */
  153. pg->is_min_y_not_flat = 1;
  154. if ( pg->list[pg->pge[PG_LEFT].curr_idx].x != pg->list[pg->pge[PG_RIGHT].curr_idx].x )
  155. {
  156. pg->is_min_y_not_flat = 0;
  157. }
  158. else
  159. {
  160. pg->total_scan_line_cnt--;
  161. if ( pg->total_scan_line_cnt == 0 )
  162. return 0;
  163. }
  164. return 1;
  165. }
  166. static void pg_hline(pg_struct *pg, u8g2_t *u8g2)
  167. {
  168. pg_word_t x1, x2, y;
  169. x1 = pg->pge[PG_LEFT].current_x;
  170. x2 = pg->pge[PG_RIGHT].current_x;
  171. y = pg->pge[PG_RIGHT].current_y;
  172. if ( y < 0 )
  173. return;
  174. if ( y >= u8g2_GetDisplayHeight(u8g2) ) // does not work for 256x64 display???
  175. return;
  176. if ( x1 < x2 )
  177. {
  178. if ( x2 < 0 )
  179. return;
  180. if ( x1 >= u8g2_GetDisplayWidth(u8g2) )
  181. return;
  182. if ( x1 < 0 )
  183. x1 = 0;
  184. if ( x2 >= u8g2_GetDisplayWidth(u8g2) )
  185. x2 = u8g2_GetDisplayWidth(u8g2);
  186. u8g2_DrawHLine(u8g2, x1, y, x2 - x1);
  187. }
  188. else
  189. {
  190. if ( x1 < 0 )
  191. return;
  192. if ( x2 >= u8g2_GetDisplayWidth(u8g2) )
  193. return;
  194. if ( x2 < 0 )
  195. x1 = 0;
  196. if ( x1 >= u8g2_GetDisplayWidth(u8g2) )
  197. x1 = u8g2_GetDisplayWidth(u8g2);
  198. u8g2_DrawHLine(u8g2, x2, y, x1 - x2);
  199. }
  200. }
  201. static void pg_line_init(pg_struct * const pg, uint8_t pge_index)
  202. {
  203. struct pg_edge_struct *pge = pg->pge+pge_index;
  204. uint8_t idx;
  205. pg_word_t x1;
  206. pg_word_t y1;
  207. pg_word_t x2;
  208. pg_word_t y2;
  209. idx = pge->curr_idx;
  210. y1 = pg->list[idx].y;
  211. x1 = pg->list[idx].x;
  212. idx = pge->next_idx_fn(pg, idx);
  213. y2 = pg->list[idx].y;
  214. x2 = pg->list[idx].x;
  215. pge->curr_idx = idx;
  216. pge_Init(pge, x1, y1, x2, y2);
  217. }
  218. static void pg_exec(pg_struct *pg, u8g2_t *u8g2)
  219. {
  220. pg_word_t i = pg->total_scan_line_cnt;
  221. /* first line is skipped if the min y line is not flat */
  222. pg_line_init(pg, PG_LEFT);
  223. pg_line_init(pg, PG_RIGHT);
  224. if ( pg->is_min_y_not_flat != 0 )
  225. {
  226. pge_Next(&(pg->pge[PG_LEFT]));
  227. pge_Next(&(pg->pge[PG_RIGHT]));
  228. }
  229. do
  230. {
  231. pg_hline(pg, u8g2);
  232. while ( pge_Next(&(pg->pge[PG_LEFT])) == 0 )
  233. {
  234. pg_line_init(pg, PG_LEFT);
  235. }
  236. while ( pge_Next(&(pg->pge[PG_RIGHT])) == 0 )
  237. {
  238. pg_line_init(pg, PG_RIGHT);
  239. }
  240. i--;
  241. } while( i > 0 );
  242. }
  243. /*===========================================*/
  244. /* API procedures */
  245. static void pg_ClearPolygonXY(pg_struct *pg)
  246. {
  247. pg->cnt = 0;
  248. }
  249. static void pg_AddPolygonXY(pg_struct *pg, int16_t x, int16_t y)
  250. {
  251. if ( pg->cnt < PG_MAX_POINTS )
  252. {
  253. pg->list[pg->cnt].x = x;
  254. pg->list[pg->cnt].y = y;
  255. pg->cnt++;
  256. }
  257. }
  258. static void pg_DrawPolygon(pg_struct *pg, u8g2_t *u8g2)
  259. {
  260. if ( pg_prepare(pg) == 0 )
  261. return;
  262. pg_exec(pg, u8g2);
  263. }
  264. pg_struct u8g2_pg;
  265. void u8g2_ClearPolygonXY(void)
  266. {
  267. pg_ClearPolygonXY(&u8g2_pg);
  268. }
  269. void u8g2_AddPolygonXY(U8X8_UNUSED u8g2_t *u8g2, int16_t x, int16_t y)
  270. {
  271. pg_AddPolygonXY(&u8g2_pg, x, y);
  272. }
  273. void u8g2_DrawPolygon(u8g2_t *u8g2)
  274. {
  275. pg_DrawPolygon(&u8g2_pg, u8g2);
  276. }
  277. void u8g2_DrawTriangle(u8g2_t *u8g2, int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2)
  278. {
  279. u8g2_ClearPolygonXY();
  280. u8g2_AddPolygonXY(u8g2, x0, y0);
  281. u8g2_AddPolygonXY(u8g2, x1, y1);
  282. u8g2_AddPolygonXY(u8g2, x2, y2);
  283. u8g2_DrawPolygon(u8g2);
  284. }