u8g2_circle.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. u8g2_circle.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2016, 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 "u8g2.h"
  28. /*==============================================*/
  29. /* Circle */
  30. static void u8g2_draw_circle_section(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t x0, u8g2_uint_t y0, uint8_t option) U8G2_NOINLINE;
  31. static void u8g2_draw_circle_section(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t x0, u8g2_uint_t y0, uint8_t option)
  32. {
  33. /* upper right */
  34. if ( option & U8G2_DRAW_UPPER_RIGHT )
  35. {
  36. u8g2_DrawPixel(u8g2, x0 + x, y0 - y);
  37. u8g2_DrawPixel(u8g2, x0 + y, y0 - x);
  38. }
  39. /* upper left */
  40. if ( option & U8G2_DRAW_UPPER_LEFT )
  41. {
  42. u8g2_DrawPixel(u8g2, x0 - x, y0 - y);
  43. u8g2_DrawPixel(u8g2, x0 - y, y0 - x);
  44. }
  45. /* lower right */
  46. if ( option & U8G2_DRAW_LOWER_RIGHT )
  47. {
  48. u8g2_DrawPixel(u8g2, x0 + x, y0 + y);
  49. u8g2_DrawPixel(u8g2, x0 + y, y0 + x);
  50. }
  51. /* lower left */
  52. if ( option & U8G2_DRAW_LOWER_LEFT )
  53. {
  54. u8g2_DrawPixel(u8g2, x0 - x, y0 + y);
  55. u8g2_DrawPixel(u8g2, x0 - y, y0 + x);
  56. }
  57. }
  58. static void u8g2_draw_circle(u8g2_t *u8g2, u8g2_uint_t x0, u8g2_uint_t y0, u8g2_uint_t rad, uint8_t option)
  59. {
  60. u8g2_int_t f;
  61. u8g2_int_t ddF_x;
  62. u8g2_int_t ddF_y;
  63. u8g2_uint_t x;
  64. u8g2_uint_t y;
  65. f = 1;
  66. f -= rad;
  67. ddF_x = 1;
  68. ddF_y = 0;
  69. ddF_y -= rad;
  70. ddF_y *= 2;
  71. x = 0;
  72. y = rad;
  73. u8g2_draw_circle_section(u8g2, x, y, x0, y0, option);
  74. while ( x < y )
  75. {
  76. if (f >= 0)
  77. {
  78. y--;
  79. ddF_y += 2;
  80. f += ddF_y;
  81. }
  82. x++;
  83. ddF_x += 2;
  84. f += ddF_x;
  85. u8g2_draw_circle_section(u8g2, x, y, x0, y0, option);
  86. }
  87. }
  88. void u8g2_DrawCircle(u8g2_t *u8g2, u8g2_uint_t x0, u8g2_uint_t y0, u8g2_uint_t rad, uint8_t option)
  89. {
  90. /* check for bounding box */
  91. #ifdef U8G2_WITH_INTERSECTION
  92. {
  93. if ( u8g2_IsIntersection(u8g2, x0-rad, y0-rad, x0+rad+1, y0+rad+1) == 0 )
  94. return;
  95. }
  96. #endif /* U8G2_WITH_INTERSECTION */
  97. /* draw circle */
  98. u8g2_draw_circle(u8g2, x0, y0, rad, option);
  99. }
  100. /*==============================================*/
  101. /* Disk */
  102. static void u8g2_draw_disc_section(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t x0, u8g2_uint_t y0, uint8_t option) U8G2_NOINLINE;
  103. static void u8g2_draw_disc_section(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t x0, u8g2_uint_t y0, uint8_t option)
  104. {
  105. /* upper right */
  106. if ( option & U8G2_DRAW_UPPER_RIGHT )
  107. {
  108. u8g2_DrawVLine(u8g2, x0+x, y0-y, y+1);
  109. u8g2_DrawVLine(u8g2, x0+y, y0-x, x+1);
  110. }
  111. /* upper left */
  112. if ( option & U8G2_DRAW_UPPER_LEFT )
  113. {
  114. u8g2_DrawVLine(u8g2, x0-x, y0-y, y+1);
  115. u8g2_DrawVLine(u8g2, x0-y, y0-x, x+1);
  116. }
  117. /* lower right */
  118. if ( option & U8G2_DRAW_LOWER_RIGHT )
  119. {
  120. u8g2_DrawVLine(u8g2, x0+x, y0, y+1);
  121. u8g2_DrawVLine(u8g2, x0+y, y0, x+1);
  122. }
  123. /* lower left */
  124. if ( option & U8G2_DRAW_LOWER_LEFT )
  125. {
  126. u8g2_DrawVLine(u8g2, x0-x, y0, y+1);
  127. u8g2_DrawVLine(u8g2, x0-y, y0, x+1);
  128. }
  129. }
  130. static void u8g2_draw_disc(u8g2_t *u8g2, u8g2_uint_t x0, u8g2_uint_t y0, u8g2_uint_t rad, uint8_t option)
  131. {
  132. u8g2_int_t f;
  133. u8g2_int_t ddF_x;
  134. u8g2_int_t ddF_y;
  135. u8g2_uint_t x;
  136. u8g2_uint_t y;
  137. f = 1;
  138. f -= rad;
  139. ddF_x = 1;
  140. ddF_y = 0;
  141. ddF_y -= rad;
  142. ddF_y *= 2;
  143. x = 0;
  144. y = rad;
  145. u8g2_draw_disc_section(u8g2, x, y, x0, y0, option);
  146. while ( x < y )
  147. {
  148. if (f >= 0)
  149. {
  150. y--;
  151. ddF_y += 2;
  152. f += ddF_y;
  153. }
  154. x++;
  155. ddF_x += 2;
  156. f += ddF_x;
  157. u8g2_draw_disc_section(u8g2, x, y, x0, y0, option);
  158. }
  159. }
  160. void u8g2_DrawDisc(u8g2_t *u8g2, u8g2_uint_t x0, u8g2_uint_t y0, u8g2_uint_t rad, uint8_t option)
  161. {
  162. /* check for bounding box */
  163. #ifdef U8G2_WITH_INTERSECTION
  164. {
  165. if ( u8g2_IsIntersection(u8g2, x0-rad, y0-rad, x0+rad+1, y0+rad+1) == 0 )
  166. return;
  167. }
  168. #endif /* U8G2_WITH_INTERSECTION */
  169. /* draw disc */
  170. u8g2_draw_disc(u8g2, x0, y0, rad, option);
  171. }
  172. /*==============================================*/
  173. /* Ellipse */
  174. /*
  175. Source:
  176. Foley, Computer Graphics, p 90
  177. */
  178. static void u8g2_draw_ellipse_section(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t x0, u8g2_uint_t y0, uint8_t option) U8G2_NOINLINE;
  179. static void u8g2_draw_ellipse_section(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t x0, u8g2_uint_t y0, uint8_t option)
  180. {
  181. /* upper right */
  182. if ( option & U8G2_DRAW_UPPER_RIGHT )
  183. {
  184. u8g2_DrawPixel(u8g2, x0 + x, y0 - y);
  185. }
  186. /* upper left */
  187. if ( option & U8G2_DRAW_UPPER_LEFT )
  188. {
  189. u8g2_DrawPixel(u8g2, x0 - x, y0 - y);
  190. }
  191. /* lower right */
  192. if ( option & U8G2_DRAW_LOWER_RIGHT )
  193. {
  194. u8g2_DrawPixel(u8g2, x0 + x, y0 + y);
  195. }
  196. /* lower left */
  197. if ( option & U8G2_DRAW_LOWER_LEFT )
  198. {
  199. u8g2_DrawPixel(u8g2, x0 - x, y0 + y);
  200. }
  201. }
  202. static void u8g2_draw_ellipse(u8g2_t *u8g2, u8g2_uint_t x0, u8g2_uint_t y0, u8g2_uint_t rx, u8g2_uint_t ry, uint8_t option)
  203. {
  204. u8g2_uint_t x, y;
  205. u8g2_long_t xchg, ychg;
  206. u8g2_long_t err;
  207. u8g2_long_t rxrx2;
  208. u8g2_long_t ryry2;
  209. u8g2_long_t stopx, stopy;
  210. rxrx2 = rx;
  211. rxrx2 *= rx;
  212. rxrx2 *= 2;
  213. ryry2 = ry;
  214. ryry2 *= ry;
  215. ryry2 *= 2;
  216. x = rx;
  217. y = 0;
  218. xchg = 1;
  219. xchg -= rx;
  220. xchg -= rx;
  221. xchg *= ry;
  222. xchg *= ry;
  223. ychg = rx;
  224. ychg *= rx;
  225. err = 0;
  226. stopx = ryry2;
  227. stopx *= rx;
  228. stopy = 0;
  229. while( stopx >= stopy )
  230. {
  231. u8g2_draw_ellipse_section(u8g2, x, y, x0, y0, option);
  232. y++;
  233. stopy += rxrx2;
  234. err += ychg;
  235. ychg += rxrx2;
  236. if ( 2*err+xchg > 0 )
  237. {
  238. x--;
  239. stopx -= ryry2;
  240. err += xchg;
  241. xchg += ryry2;
  242. }
  243. }
  244. x = 0;
  245. y = ry;
  246. xchg = ry;
  247. xchg *= ry;
  248. ychg = 1;
  249. ychg -= ry;
  250. ychg -= ry;
  251. ychg *= rx;
  252. ychg *= rx;
  253. err = 0;
  254. stopx = 0;
  255. stopy = rxrx2;
  256. stopy *= ry;
  257. while( stopx <= stopy )
  258. {
  259. u8g2_draw_ellipse_section(u8g2, x, y, x0, y0, option);
  260. x++;
  261. stopx += ryry2;
  262. err += xchg;
  263. xchg += ryry2;
  264. if ( 2*err+ychg > 0 )
  265. {
  266. y--;
  267. stopy -= rxrx2;
  268. err += ychg;
  269. ychg += rxrx2;
  270. }
  271. }
  272. }
  273. void u8g2_DrawEllipse(u8g2_t *u8g2, u8g2_uint_t x0, u8g2_uint_t y0, u8g2_uint_t rx, u8g2_uint_t ry, uint8_t option)
  274. {
  275. /* check for bounding box */
  276. #ifdef U8G2_WITH_INTERSECTION
  277. {
  278. if ( u8g2_IsIntersection(u8g2, x0-rx, y0-ry, x0+rx+1, y0+ry+1) == 0 )
  279. return;
  280. }
  281. #endif /* U8G2_WITH_INTERSECTION */
  282. u8g2_draw_ellipse(u8g2, x0, y0, rx, ry, option);
  283. }
  284. /*==============================================*/
  285. /* Filled Ellipse */
  286. static void u8g2_draw_filled_ellipse_section(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t x0, u8g2_uint_t y0, uint8_t option) U8G2_NOINLINE;
  287. static void u8g2_draw_filled_ellipse_section(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t x0, u8g2_uint_t y0, uint8_t option)
  288. {
  289. /* upper right */
  290. if ( option & U8G2_DRAW_UPPER_RIGHT )
  291. {
  292. u8g2_DrawVLine(u8g2, x0+x, y0-y, y+1);
  293. }
  294. /* upper left */
  295. if ( option & U8G2_DRAW_UPPER_LEFT )
  296. {
  297. u8g2_DrawVLine(u8g2, x0-x, y0-y, y+1);
  298. }
  299. /* lower right */
  300. if ( option & U8G2_DRAW_LOWER_RIGHT )
  301. {
  302. u8g2_DrawVLine(u8g2, x0+x, y0, y+1);
  303. }
  304. /* lower left */
  305. if ( option & U8G2_DRAW_LOWER_LEFT )
  306. {
  307. u8g2_DrawVLine(u8g2, x0-x, y0, y+1);
  308. }
  309. }
  310. static void u8g2_draw_filled_ellipse(u8g2_t *u8g2, u8g2_uint_t x0, u8g2_uint_t y0, u8g2_uint_t rx, u8g2_uint_t ry, uint8_t option)
  311. {
  312. u8g2_uint_t x, y;
  313. u8g2_long_t xchg, ychg;
  314. u8g2_long_t err;
  315. u8g2_long_t rxrx2;
  316. u8g2_long_t ryry2;
  317. u8g2_long_t stopx, stopy;
  318. rxrx2 = rx;
  319. rxrx2 *= rx;
  320. rxrx2 *= 2;
  321. ryry2 = ry;
  322. ryry2 *= ry;
  323. ryry2 *= 2;
  324. x = rx;
  325. y = 0;
  326. xchg = 1;
  327. xchg -= rx;
  328. xchg -= rx;
  329. xchg *= ry;
  330. xchg *= ry;
  331. ychg = rx;
  332. ychg *= rx;
  333. err = 0;
  334. stopx = ryry2;
  335. stopx *= rx;
  336. stopy = 0;
  337. while( stopx >= stopy )
  338. {
  339. u8g2_draw_filled_ellipse_section(u8g2, x, y, x0, y0, option);
  340. y++;
  341. stopy += rxrx2;
  342. err += ychg;
  343. ychg += rxrx2;
  344. if ( 2*err+xchg > 0 )
  345. {
  346. x--;
  347. stopx -= ryry2;
  348. err += xchg;
  349. xchg += ryry2;
  350. }
  351. }
  352. x = 0;
  353. y = ry;
  354. xchg = ry;
  355. xchg *= ry;
  356. ychg = 1;
  357. ychg -= ry;
  358. ychg -= ry;
  359. ychg *= rx;
  360. ychg *= rx;
  361. err = 0;
  362. stopx = 0;
  363. stopy = rxrx2;
  364. stopy *= ry;
  365. while( stopx <= stopy )
  366. {
  367. u8g2_draw_filled_ellipse_section(u8g2, x, y, x0, y0, option);
  368. x++;
  369. stopx += ryry2;
  370. err += xchg;
  371. xchg += ryry2;
  372. if ( 2*err+ychg > 0 )
  373. {
  374. y--;
  375. stopy -= rxrx2;
  376. err += ychg;
  377. ychg += rxrx2;
  378. }
  379. }
  380. }
  381. void u8g2_DrawFilledEllipse(u8g2_t *u8g2, u8g2_uint_t x0, u8g2_uint_t y0, u8g2_uint_t rx, u8g2_uint_t ry, uint8_t option)
  382. {
  383. /* check for bounding box */
  384. #ifdef U8G2_WITH_INTERSECTION
  385. {
  386. if ( u8g2_IsIntersection(u8g2, x0-rx, y0-ry, x0+rx+1, y0+ry+1) == 0 )
  387. return;
  388. }
  389. #endif /* U8G2_WITH_INTERSECTION */
  390. u8g2_draw_filled_ellipse(u8g2, x0, y0, rx, ry, option);
  391. }