ucg_polygon.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. ucg_polygon.c
  3. Universal uC Color Graphics Library
  4. Copyright (c) 2014, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "ucg.h"
  28. /*===========================================*/
  29. /* procedures, which should not be inlined (save as much flash ROM as possible) */
  30. static uint8_t pge_Next(struct pg_edge_struct *pge) PG_NOINLINE;
  31. static uint8_t pg_inc(pg_struct *pg, uint8_t i) PG_NOINLINE;
  32. static uint8_t pg_dec(pg_struct *pg, uint8_t i) PG_NOINLINE;
  33. static void pg_expand_min_y(pg_struct *pg, pg_word_t min_y, uint8_t pge_idx) PG_NOINLINE;
  34. static void pg_line_init(pg_struct * const pg, uint8_t pge_index) PG_NOINLINE;
  35. /*===========================================*/
  36. /* line draw algorithm */
  37. static uint8_t pge_Next(struct pg_edge_struct *pge)
  38. {
  39. if ( pge->current_y >= pge->max_y )
  40. return 0;
  41. pge->current_x += pge->current_x_offset;
  42. pge->error += pge->error_offset;
  43. if ( pge->error > 0 )
  44. {
  45. pge->current_x += pge->x_direction;
  46. pge->error -= pge->height;
  47. }
  48. pge->current_y++;
  49. return 1;
  50. }
  51. /* assumes y2 > y1 */
  52. 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)
  53. {
  54. pg_word_t dx = x2 - x1;
  55. pg_word_t width;
  56. pge->height = y2 - y1;
  57. pge->max_y = y2;
  58. pge->current_y = y1;
  59. pge->current_x = x1;
  60. if ( dx >= 0 )
  61. {
  62. pge->x_direction = 1;
  63. width = dx;
  64. pge->error = 0;
  65. }
  66. else
  67. {
  68. pge->x_direction = -1;
  69. width = -dx;
  70. pge->error = 1 - pge->height;
  71. }
  72. pge->current_x_offset = dx / pge->height;
  73. pge->error_offset = width % pge->height;
  74. }
  75. /*===========================================*/
  76. /* convex polygon algorithm */
  77. static uint8_t pg_inc(pg_struct *pg, uint8_t i)
  78. {
  79. i++;
  80. if ( i >= pg->cnt )
  81. i = 0;
  82. return i;
  83. }
  84. static uint8_t pg_dec(pg_struct *pg, uint8_t i)
  85. {
  86. i--;
  87. if ( i >= pg->cnt )
  88. i = pg->cnt-1;
  89. return i;
  90. }
  91. static void pg_expand_min_y(pg_struct *pg, pg_word_t min_y, uint8_t pge_idx)
  92. {
  93. uint8_t i = pg->pge[pge_idx].curr_idx;
  94. for(;;)
  95. {
  96. i = pg->pge[pge_idx].next_idx_fn(pg, i);
  97. if ( pg->list[i].y != min_y )
  98. break;
  99. pg->pge[pge_idx].curr_idx = i;
  100. }
  101. }
  102. static uint8_t pg_prepare(pg_struct *pg)
  103. {
  104. pg_word_t max_y;
  105. pg_word_t min_y;
  106. uint8_t i;
  107. /* setup the next index procedures */
  108. pg->pge[PG_RIGHT].next_idx_fn = pg_inc;
  109. pg->pge[PG_LEFT].next_idx_fn = pg_dec;
  110. /* search for highest and lowest point */
  111. max_y = pg->list[0].y;
  112. min_y = pg->list[0].y;
  113. pg->pge[PG_LEFT].curr_idx = 0;
  114. for( i = 1; i < pg->cnt; i++ )
  115. {
  116. if ( max_y < pg->list[i].y )
  117. {
  118. max_y = pg->list[i].y;
  119. }
  120. if ( min_y > pg->list[i].y )
  121. {
  122. pg->pge[PG_LEFT].curr_idx = i;
  123. min_y = pg->list[i].y;
  124. }
  125. }
  126. /* calculate total number of scan lines */
  127. pg->total_scan_line_cnt = max_y;
  128. pg->total_scan_line_cnt -= min_y;
  129. /* exit if polygon height is zero */
  130. if ( pg->total_scan_line_cnt == 0 )
  131. return 0;
  132. /* if the minimum y side is flat, try to find the lowest and highest x points */
  133. pg->pge[PG_RIGHT].curr_idx = pg->pge[PG_LEFT].curr_idx;
  134. pg_expand_min_y(pg, min_y, PG_RIGHT);
  135. pg_expand_min_y(pg, min_y, PG_LEFT);
  136. /* check if the min side is really flat (depends on the x values) */
  137. pg->is_min_y_not_flat = 1;
  138. if ( pg->list[pg->pge[PG_LEFT].curr_idx].x != pg->list[pg->pge[PG_RIGHT].curr_idx].x )
  139. {
  140. pg->is_min_y_not_flat = 0;
  141. }
  142. else
  143. {
  144. pg->total_scan_line_cnt--;
  145. if ( pg->total_scan_line_cnt == 0 )
  146. return 0;
  147. }
  148. return 1;
  149. }
  150. static void pg_hline(pg_struct *pg, ucg_t *ucg)
  151. {
  152. pg_word_t x1, x2, y;
  153. x1 = pg->pge[PG_LEFT].current_x;
  154. x2 = pg->pge[PG_RIGHT].current_x;
  155. y = pg->pge[PG_RIGHT].current_y;
  156. if ( y < 0 )
  157. return;
  158. if ( y >= ucg_GetHeight(ucg) )
  159. return;
  160. if ( x1 < x2 )
  161. {
  162. if ( x2 < 0 )
  163. return;
  164. if ( x1 >= ucg_GetWidth(ucg) )
  165. return;
  166. if ( x1 < 0 )
  167. x1 = 0;
  168. if ( x2 >= ucg_GetWidth(ucg) )
  169. x2 = ucg_GetWidth(ucg);
  170. ucg_DrawHLine(ucg, x1, y, x2 - x1);
  171. }
  172. else
  173. {
  174. if ( x1 < 0 )
  175. return;
  176. if ( x2 >= ucg_GetWidth(ucg) )
  177. return;
  178. if ( x2 < 0 )
  179. x1 = 0;
  180. if ( x1 >= ucg_GetWidth(ucg) )
  181. x1 = ucg_GetWidth(ucg);
  182. ucg_DrawHLine(ucg, x2, y, x1 - x2);
  183. }
  184. }
  185. static void pg_line_init(pg_struct * pg, uint8_t pge_index)
  186. {
  187. struct pg_edge_struct *pge = pg->pge+pge_index;
  188. uint8_t idx;
  189. pg_word_t x1;
  190. pg_word_t y1;
  191. pg_word_t x2;
  192. pg_word_t y2;
  193. idx = pge->curr_idx;
  194. y1 = pg->list[idx].y;
  195. x1 = pg->list[idx].x;
  196. idx = pge->next_idx_fn(pg, idx);
  197. y2 = pg->list[idx].y;
  198. x2 = pg->list[idx].x;
  199. pge->curr_idx = idx;
  200. pge_Init(pge, x1, y1, x2, y2);
  201. }
  202. static void pg_exec(pg_struct *pg, ucg_t *ucg)
  203. {
  204. pg_word_t i = pg->total_scan_line_cnt;
  205. /* first line is skipped if the min y line is not flat */
  206. pg_line_init(pg, PG_LEFT);
  207. pg_line_init(pg, PG_RIGHT);
  208. if ( pg->is_min_y_not_flat != 0 )
  209. {
  210. pge_Next(&(pg->pge[PG_LEFT]));
  211. pge_Next(&(pg->pge[PG_RIGHT]));
  212. }
  213. do
  214. {
  215. pg_hline(pg, ucg);
  216. while ( pge_Next(&(pg->pge[PG_LEFT])) == 0 )
  217. {
  218. pg_line_init(pg, PG_LEFT);
  219. }
  220. while ( pge_Next(&(pg->pge[PG_RIGHT])) == 0 )
  221. {
  222. pg_line_init(pg, PG_RIGHT);
  223. }
  224. i--;
  225. } while( i > 0 );
  226. }
  227. /*===========================================*/
  228. /* API procedures */
  229. void ucg_pg_ClearPolygonXY(pg_struct *pg)
  230. {
  231. pg->cnt = 0;
  232. }
  233. void ucg_pg_AddPolygonXY(pg_struct *pg, ucg_t *ucg, int16_t x, int16_t y)
  234. {
  235. if ( pg->cnt < PG_MAX_POINTS )
  236. {
  237. pg->list[pg->cnt].x = x;
  238. pg->list[pg->cnt].y = y;
  239. pg->cnt++;
  240. }
  241. }
  242. void ucg_pg_DrawPolygon(pg_struct *pg, ucg_t *ucg)
  243. {
  244. if ( pg_prepare(pg) == 0 )
  245. return;
  246. pg_exec(pg, ucg);
  247. }
  248. pg_struct ucg_pg;
  249. void ucg_ClearPolygonXY(void)
  250. {
  251. ucg_pg_ClearPolygonXY(&ucg_pg);
  252. }
  253. void ucg_AddPolygonXY(ucg_t *ucg, int16_t x, int16_t y)
  254. {
  255. ucg_pg_AddPolygonXY(&ucg_pg, ucg, x, y);
  256. }
  257. void ucg_DrawPolygon(ucg_t *ucg)
  258. {
  259. ucg_pg_DrawPolygon(&ucg_pg, ucg);
  260. }
  261. void ucg_DrawTriangle(ucg_t *ucg, int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2)
  262. {
  263. ucg_ClearPolygonXY();
  264. ucg_AddPolygonXY(ucg, x0, y0);
  265. ucg_AddPolygonXY(ucg, x1, y1);
  266. ucg_AddPolygonXY(ucg, x2, y2);
  267. ucg_DrawPolygon(ucg);
  268. }
  269. void ucg_DrawTetragon(ucg_t *ucg, int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3)
  270. {
  271. ucg_ClearPolygonXY();
  272. ucg_AddPolygonXY(ucg, x0, y0);
  273. ucg_AddPolygonXY(ucg, x1, y1);
  274. ucg_AddPolygonXY(ucg, x2, y2);
  275. ucg_AddPolygonXY(ucg, x3, y3);
  276. ucg_DrawPolygon(ucg);
  277. }