u8g_ellipse.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. u8g_ellipse.c
  3. Utility to draw empty and filled ellipses.
  4. Universal 8bit Graphics Library
  5. Copyright (c) 2011, bjthom@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. Addition to the U8G Library as of 02/29/12
  28. Adapted from Bresenham's Algorithm and the following websites:
  29. http://free.pages.at/easyfilter/bresenham.html
  30. http://homepage.smc.edu/kennedy_john/belipse.pdf
  31. */
  32. #include "u8g.h"
  33. #ifdef WORK_IN_PROGRESS
  34. void u8g_DrawEllipseRect(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t x1, u8g_uint_t y1)
  35. {
  36. int a = abs(x1 - x0);
  37. int b = abs(y1 - y0); //get diameters
  38. int b1 = b&1;
  39. long dx = 4*(1-a)*b*b;
  40. long dy = 4*(b1+1)*a*a;
  41. long err = dx+dy+b1*a*a;
  42. long e2;
  43. if (x0 > x1) { x0 = x1; x1 += a; }
  44. if (y0 > y1) { y0 = y1; }
  45. y0 += (b+1)/2;
  46. y1 = y0-b1;
  47. a *= 8*a;
  48. b1 = 8*b*b;
  49. do {
  50. u8g_DrawPixel(u8g, x1, y0);
  51. u8g_DrawPixel(u8g, x0, y0);
  52. u8g_DrawPixel(u8g, x0, y1);
  53. u8g_DrawPixel(u8g, x1, y1);
  54. e2 = 2*err;
  55. if (e2 >= dx) {
  56. x0++;
  57. x1--;
  58. err += dx += b1;
  59. }
  60. if (e2 <= dy) {
  61. y0++;
  62. y1--;
  63. err += dy += a;
  64. }
  65. } while (x0 <= x1);
  66. while (y0-y1 < b) {
  67. u8g_DrawPixel(u8g, x0-1, y0);
  68. u8g_DrawPixel(u8g, x1+1, y0++);
  69. u8g_DrawPixel(u8g, x0-1, y1);
  70. u8g_DrawPixel(u8g, x1+1, y1--);
  71. }
  72. }
  73. void u8g_DrawEllipse(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t xr, u8g_uint_t yr)
  74. {
  75. u8g_DrawPixel(u8g, x0, y0+yr);
  76. u8g_DrawPixel(u8g, x0, y0-yr);
  77. u8g_DrawPixel(u8g, x0+xr, y0);
  78. u8g_DrawPixel(u8g, x0-xr, y0);
  79. }
  80. #endif
  81. #if defined(U8G_16BIT)
  82. typedef int32_t u8g_long_t;
  83. #else
  84. typedef int16_t u8g_long_t;
  85. #endif
  86. /*
  87. Source:
  88. ftp://pc.fk0.name/pub/books/programming/bezier-ellipse.pdf
  89. Foley, Computer Graphics, p 90
  90. */
  91. static void u8g_draw_ellipse_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option) U8G_NOINLINE;
  92. static void u8g_draw_ellipse_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option)
  93. {
  94. /* upper right */
  95. if ( option & U8G_DRAW_UPPER_RIGHT )
  96. {
  97. u8g_DrawPixel(u8g, x0 + x, y0 - y);
  98. }
  99. /* upper left */
  100. if ( option & U8G_DRAW_UPPER_LEFT )
  101. {
  102. u8g_DrawPixel(u8g, x0 - x, y0 - y);
  103. }
  104. /* lower right */
  105. if ( option & U8G_DRAW_LOWER_RIGHT )
  106. {
  107. u8g_DrawPixel(u8g, x0 + x, y0 + y);
  108. }
  109. /* lower left */
  110. if ( option & U8G_DRAW_LOWER_LEFT )
  111. {
  112. u8g_DrawPixel(u8g, x0 - x, y0 + y);
  113. }
  114. }
  115. void u8g_draw_ellipse(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rx, u8g_uint_t ry, uint8_t option)
  116. {
  117. u8g_uint_t x, y;
  118. u8g_long_t xchg, ychg;
  119. u8g_long_t err;
  120. u8g_long_t rxrx2;
  121. u8g_long_t ryry2;
  122. u8g_long_t stopx, stopy;
  123. rxrx2 = rx;
  124. rxrx2 *= rx;
  125. rxrx2 *= 2;
  126. ryry2 = ry;
  127. ryry2 *= ry;
  128. ryry2 *= 2;
  129. x = rx;
  130. y = 0;
  131. xchg = 1;
  132. xchg -= rx;
  133. xchg -= rx;
  134. xchg *= ry;
  135. xchg *= ry;
  136. ychg = rx;
  137. ychg *= rx;
  138. err = 0;
  139. stopx = ryry2;
  140. stopx *= rx;
  141. stopy = 0;
  142. while( stopx >= stopy )
  143. {
  144. u8g_draw_ellipse_section(u8g, x, y, x0, y0, option);
  145. y++;
  146. stopy += rxrx2;
  147. err += ychg;
  148. ychg += rxrx2;
  149. if ( 2*err+xchg > 0 )
  150. {
  151. x--;
  152. stopx -= ryry2;
  153. err += xchg;
  154. xchg += ryry2;
  155. }
  156. }
  157. x = 0;
  158. y = ry;
  159. xchg = ry;
  160. xchg *= ry;
  161. ychg = 1;
  162. ychg -= ry;
  163. ychg -= ry;
  164. ychg *= rx;
  165. ychg *= rx;
  166. err = 0;
  167. stopx = 0;
  168. stopy = rxrx2;
  169. stopy *= ry;
  170. while( stopx <= stopy )
  171. {
  172. u8g_draw_ellipse_section(u8g, x, y, x0, y0, option);
  173. x++;
  174. stopx += ryry2;
  175. err += xchg;
  176. xchg += ryry2;
  177. if ( 2*err+ychg > 0 )
  178. {
  179. y--;
  180. stopy -= rxrx2;
  181. err += ychg;
  182. ychg += rxrx2;
  183. }
  184. }
  185. }
  186. void u8g_DrawEllipse(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rx, u8g_uint_t ry, uint8_t option)
  187. {
  188. /* check for bounding box */
  189. {
  190. u8g_uint_t rxp, rxp2;
  191. u8g_uint_t ryp, ryp2;
  192. rxp = rx;
  193. rxp++;
  194. rxp2 = rxp;
  195. rxp2 *= 2;
  196. ryp = ry;
  197. ryp++;
  198. ryp2 = ryp;
  199. ryp2 *= 2;
  200. if ( u8g_IsBBXIntersection(u8g, x0-rxp, y0-ryp, rxp2, ryp2) == 0)
  201. return;
  202. }
  203. u8g_draw_ellipse(u8g, x0, y0, rx, ry, option);
  204. }
  205. static void u8g_draw_filled_ellipse_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option) U8G_NOINLINE;
  206. static void u8g_draw_filled_ellipse_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option)
  207. {
  208. /* upper right */
  209. if ( option & U8G_DRAW_UPPER_RIGHT )
  210. {
  211. u8g_DrawVLine(u8g, x0+x, y0-y, y+1);
  212. }
  213. /* upper left */
  214. if ( option & U8G_DRAW_UPPER_LEFT )
  215. {
  216. u8g_DrawVLine(u8g, x0-x, y0-y, y+1);
  217. }
  218. /* lower right */
  219. if ( option & U8G_DRAW_LOWER_RIGHT )
  220. {
  221. u8g_DrawVLine(u8g, x0+x, y0, y+1);
  222. }
  223. /* lower left */
  224. if ( option & U8G_DRAW_LOWER_LEFT )
  225. {
  226. u8g_DrawVLine(u8g, x0-x, y0, y+1);
  227. }
  228. }
  229. void u8g_draw_filled_ellipse(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rx, u8g_uint_t ry, uint8_t option)
  230. {
  231. u8g_uint_t x, y;
  232. u8g_long_t xchg, ychg;
  233. u8g_long_t err;
  234. u8g_long_t rxrx2;
  235. u8g_long_t ryry2;
  236. u8g_long_t stopx, stopy;
  237. rxrx2 = rx;
  238. rxrx2 *= rx;
  239. rxrx2 *= 2;
  240. ryry2 = ry;
  241. ryry2 *= ry;
  242. ryry2 *= 2;
  243. x = rx;
  244. y = 0;
  245. xchg = 1;
  246. xchg -= rx;
  247. xchg -= rx;
  248. xchg *= ry;
  249. xchg *= ry;
  250. ychg = rx;
  251. ychg *= rx;
  252. err = 0;
  253. stopx = ryry2;
  254. stopx *= rx;
  255. stopy = 0;
  256. while( stopx >= stopy )
  257. {
  258. u8g_draw_filled_ellipse_section(u8g, x, y, x0, y0, option);
  259. y++;
  260. stopy += rxrx2;
  261. err += ychg;
  262. ychg += rxrx2;
  263. if ( 2*err+xchg > 0 )
  264. {
  265. x--;
  266. stopx -= ryry2;
  267. err += xchg;
  268. xchg += ryry2;
  269. }
  270. }
  271. x = 0;
  272. y = ry;
  273. xchg = ry;
  274. xchg *= ry;
  275. ychg = 1;
  276. ychg -= ry;
  277. ychg -= ry;
  278. ychg *= rx;
  279. ychg *= rx;
  280. err = 0;
  281. stopx = 0;
  282. stopy = rxrx2;
  283. stopy *= ry;
  284. while( stopx <= stopy )
  285. {
  286. u8g_draw_filled_ellipse_section(u8g, x, y, x0, y0, option);
  287. x++;
  288. stopx += ryry2;
  289. err += xchg;
  290. xchg += ryry2;
  291. if ( 2*err+ychg > 0 )
  292. {
  293. y--;
  294. stopy -= rxrx2;
  295. err += ychg;
  296. ychg += rxrx2;
  297. }
  298. }
  299. }
  300. void u8g_DrawFilledEllipse(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rx, u8g_uint_t ry, uint8_t option)
  301. {
  302. /* check for bounding box */
  303. {
  304. u8g_uint_t rxp, rxp2;
  305. u8g_uint_t ryp, ryp2;
  306. rxp = rx;
  307. rxp++;
  308. rxp2 = rxp;
  309. rxp2 *= 2;
  310. ryp = ry;
  311. ryp++;
  312. ryp2 = ryp;
  313. ryp2 *= 2;
  314. if ( u8g_IsBBXIntersection(u8g, x0-rxp, y0-ryp, rxp2, ryp2) == 0)
  315. return;
  316. }
  317. u8g_draw_filled_ellipse(u8g, x0, y0, rx, ry, option);
  318. }