My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

u8g_circle.c 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. u8g_circle.c
  3. Utility to draw empty and filled circles.
  4. Universal 8bit Graphics Library
  5. Copyright (c) 2011, bjthom@gmail.com
  6. u8g_DrawCircle & u8g_DrawDisc by olikraus@gmail.com
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. * Redistributions of source code must retain the above copyright notice, this list
  11. of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above copyright notice, this
  13. list of conditions and the following disclaimer in the documentation and/or other
  14. materials provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  16. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  20. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  27. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. Addition to the U8G Library 02/25/12
  29. */
  30. #include "u8g.h"
  31. #ifdef OLD_CODE
  32. void circ_upperRight(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
  33. u8g_DrawPixel(u8g, x0 + x, y0 - y);
  34. u8g_DrawPixel(u8g, x0 + y, y0 - x);
  35. }
  36. void circ_upperLeft(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
  37. u8g_DrawPixel(u8g, x0 - x, y0 - y);
  38. u8g_DrawPixel(u8g, x0 - y, y0 - x);
  39. }
  40. void circ_lowerRight(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
  41. u8g_DrawPixel(u8g, x0 + x, y0 + y);
  42. u8g_DrawPixel(u8g, x0 + y, y0 + x);
  43. }
  44. void circ_lowerLeft(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
  45. u8g_DrawPixel(u8g, x0 - x, y0 + y);
  46. u8g_DrawPixel(u8g, x0 - y, y0 + x);
  47. }
  48. void circ_all(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
  49. circ_upperRight(u8g, x, y, x0, y0);
  50. circ_upperLeft(u8g, x, y, x0, y0);
  51. circ_lowerRight(u8g, x, y, x0, y0);
  52. circ_lowerLeft(u8g, x, y, x0, y0);
  53. }
  54. void u8g_DrawEmpCirc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
  55. {
  56. if ( u8g_IsBBXIntersection(u8g, x0-rad-1, y0-rad-1, 2*rad+1, 2*rad+1) == 0)
  57. return;
  58. int f = 1 - rad;
  59. int ddF_x = 1;
  60. int ddF_y = -2*rad;
  61. uint8_t x = 0;
  62. uint8_t y = rad;
  63. void ( *circ_util )(u8g_t *, u8g_uint_t, u8g_uint_t, u8g_uint_t, u8g_uint_t);
  64. switch (option)
  65. {
  66. case U8G_CIRC_UPPER_RIGHT:
  67. u8g_DrawPixel(u8g, x0, y0 - rad);
  68. u8g_DrawPixel(u8g, x0 + rad, y0);
  69. circ_util = circ_upperRight;
  70. break;
  71. case U8G_CIRC_UPPER_LEFT:
  72. u8g_DrawPixel(u8g, x0, y0 - rad);
  73. u8g_DrawPixel(u8g, x0 - rad, y0);
  74. circ_util = circ_upperLeft;
  75. break;
  76. case U8G_CIRC_LOWER_RIGHT:
  77. u8g_DrawPixel(u8g, x0, y0 + rad);
  78. u8g_DrawPixel(u8g, x0 + rad, y0);
  79. circ_util = circ_lowerRight;
  80. break;
  81. case U8G_CIRC_LOWER_LEFT:
  82. u8g_DrawPixel(u8g, x0, y0 + rad);
  83. u8g_DrawPixel(u8g, x0 - rad, y0);
  84. circ_util = circ_lowerLeft;
  85. break;
  86. default:
  87. case U8G_CIRC_ALL:
  88. u8g_DrawPixel(u8g, x0, y0 + rad);
  89. u8g_DrawPixel(u8g, x0, y0 - rad);
  90. u8g_DrawPixel(u8g, x0 + rad, y0);
  91. u8g_DrawPixel(u8g, x0 - rad, y0);
  92. circ_util = circ_all;
  93. break;
  94. }
  95. while( x < y )
  96. {
  97. if(f >= 0)
  98. {
  99. y--;
  100. ddF_y += 2;
  101. f += ddF_y;
  102. }
  103. x++;
  104. ddF_x += 2;
  105. f += ddF_x;
  106. circ_util(u8g, x, y, x0, y0);
  107. }
  108. }
  109. void u8g_DrawFillCirc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
  110. {
  111. if ( u8g_IsBBXIntersection(u8g, x0-rad-1, y0-rad-1, 2*rad+1, 2*rad+1) == 0)
  112. return;
  113. int f = 1 - rad;
  114. int ddF_x = 1;
  115. int ddF_y = -2*rad;
  116. uint8_t x = 0;
  117. uint8_t y = rad;
  118. // Draw vertical diameter at the horiz. center
  119. // u8g_DrawVLine(u8g, x0, y0 - rad, 2*rad+1);
  120. if (option == U8G_CIRC_UPPER_LEFT || option == U8G_CIRC_UPPER_RIGHT) {
  121. u8g_DrawVLine(u8g, x0, y0 - rad, rad+1);
  122. }
  123. else if (option == U8G_CIRC_LOWER_LEFT || option == U8G_CIRC_LOWER_RIGHT) {
  124. u8g_DrawVLine(u8g, x0, y0, rad+1);
  125. }
  126. else {
  127. u8g_DrawVLine(u8g, x0, y0 - rad, 2*rad+1);
  128. }
  129. while( x < y )
  130. {
  131. if(f >= 0)
  132. {
  133. y--;
  134. ddF_y += 2;
  135. f += ddF_y;
  136. }
  137. x++;
  138. ddF_x += 2;
  139. f += ddF_x;
  140. //Draw vertical lines from one point to another
  141. switch (option)
  142. {
  143. case U8G_CIRC_UPPER_RIGHT:
  144. u8g_DrawVLine(u8g, x0+x, y0-y, y+1);
  145. u8g_DrawVLine(u8g, x0+y, y0-x, x+1);
  146. break;
  147. case U8G_CIRC_UPPER_LEFT:
  148. u8g_DrawVLine(u8g, x0-x, y0-y, y+1);
  149. u8g_DrawVLine(u8g, x0-y, y0-x, x+1);
  150. break;
  151. case U8G_CIRC_LOWER_RIGHT:
  152. u8g_DrawVLine(u8g, x0+x, y0, y+1);
  153. u8g_DrawVLine(u8g, x0+y, y0, x+1);
  154. break;
  155. case U8G_CIRC_LOWER_LEFT:
  156. u8g_DrawVLine(u8g, x0-x, y0, y+1);
  157. u8g_DrawVLine(u8g, x0-y, y0, x+1);
  158. break;
  159. case U8G_CIRC_ALL:
  160. u8g_DrawVLine(u8g, x0+x, y0-y, 2*y+1);
  161. u8g_DrawVLine(u8g, x0-x, y0-y, 2*y+1);
  162. u8g_DrawVLine(u8g, x0+y, y0-x, 2*x+1);
  163. u8g_DrawVLine(u8g, x0-y, y0-x, 2*x+1);
  164. break;
  165. }
  166. }
  167. }
  168. #endif
  169. /*=========================================================================*/
  170. static void u8g_draw_circle_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;
  171. static void u8g_draw_circle_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option)
  172. {
  173. /* upper right */
  174. if ( option & U8G_DRAW_UPPER_RIGHT )
  175. {
  176. u8g_DrawPixel(u8g, x0 + x, y0 - y);
  177. u8g_DrawPixel(u8g, x0 + y, y0 - x);
  178. }
  179. /* upper left */
  180. if ( option & U8G_DRAW_UPPER_LEFT )
  181. {
  182. u8g_DrawPixel(u8g, x0 - x, y0 - y);
  183. u8g_DrawPixel(u8g, x0 - y, y0 - x);
  184. }
  185. /* lower right */
  186. if ( option & U8G_DRAW_LOWER_RIGHT )
  187. {
  188. u8g_DrawPixel(u8g, x0 + x, y0 + y);
  189. u8g_DrawPixel(u8g, x0 + y, y0 + x);
  190. }
  191. /* lower left */
  192. if ( option & U8G_DRAW_LOWER_LEFT )
  193. {
  194. u8g_DrawPixel(u8g, x0 - x, y0 + y);
  195. u8g_DrawPixel(u8g, x0 - y, y0 + x);
  196. }
  197. }
  198. void u8g_draw_circle(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
  199. {
  200. u8g_int_t f;
  201. u8g_int_t ddF_x;
  202. u8g_int_t ddF_y;
  203. u8g_uint_t x;
  204. u8g_uint_t y;
  205. f = 1;
  206. f -= rad;
  207. ddF_x = 1;
  208. ddF_y = 0;
  209. ddF_y -= rad;
  210. ddF_y *= 2;
  211. x = 0;
  212. y = rad;
  213. u8g_draw_circle_section(u8g, x, y, x0, y0, option);
  214. while ( x < y )
  215. {
  216. if (f >= 0)
  217. {
  218. y--;
  219. ddF_y += 2;
  220. f += ddF_y;
  221. }
  222. x++;
  223. ddF_x += 2;
  224. f += ddF_x;
  225. u8g_draw_circle_section(u8g, x, y, x0, y0, option);
  226. }
  227. }
  228. void u8g_DrawCircle(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
  229. {
  230. /* check for bounding box */
  231. {
  232. u8g_uint_t radp, radp2;
  233. radp = rad;
  234. radp++;
  235. radp2 = radp;
  236. radp2 *= 2;
  237. if ( u8g_IsBBXIntersection(u8g, x0-radp, y0-radp, radp2, radp2) == 0)
  238. return;
  239. }
  240. /* draw circle */
  241. u8g_draw_circle(u8g, x0, y0, rad, option);
  242. }
  243. static void u8g_draw_disc_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;
  244. static void u8g_draw_disc_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option)
  245. {
  246. /* upper right */
  247. if ( option & U8G_DRAW_UPPER_RIGHT )
  248. {
  249. u8g_DrawVLine(u8g, x0+x, y0-y, y+1);
  250. u8g_DrawVLine(u8g, x0+y, y0-x, x+1);
  251. }
  252. /* upper left */
  253. if ( option & U8G_DRAW_UPPER_LEFT )
  254. {
  255. u8g_DrawVLine(u8g, x0-x, y0-y, y+1);
  256. u8g_DrawVLine(u8g, x0-y, y0-x, x+1);
  257. }
  258. /* lower right */
  259. if ( option & U8G_DRAW_LOWER_RIGHT )
  260. {
  261. u8g_DrawVLine(u8g, x0+x, y0, y+1);
  262. u8g_DrawVLine(u8g, x0+y, y0, x+1);
  263. }
  264. /* lower left */
  265. if ( option & U8G_DRAW_LOWER_LEFT )
  266. {
  267. u8g_DrawVLine(u8g, x0-x, y0, y+1);
  268. u8g_DrawVLine(u8g, x0-y, y0, x+1);
  269. }
  270. }
  271. void u8g_draw_disc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
  272. {
  273. u8g_int_t f;
  274. u8g_int_t ddF_x;
  275. u8g_int_t ddF_y;
  276. u8g_uint_t x;
  277. u8g_uint_t y;
  278. f = 1;
  279. f -= rad;
  280. ddF_x = 1;
  281. ddF_y = 0;
  282. ddF_y -= rad;
  283. ddF_y *= 2;
  284. x = 0;
  285. y = rad;
  286. u8g_draw_disc_section(u8g, x, y, x0, y0, option);
  287. while ( x < y )
  288. {
  289. if (f >= 0)
  290. {
  291. y--;
  292. ddF_y += 2;
  293. f += ddF_y;
  294. }
  295. x++;
  296. ddF_x += 2;
  297. f += ddF_x;
  298. u8g_draw_disc_section(u8g, x, y, x0, y0, option);
  299. }
  300. }
  301. void u8g_DrawDisc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
  302. {
  303. /* check for bounding box */
  304. {
  305. u8g_uint_t radp, radp2;
  306. radp = rad;
  307. radp++;
  308. radp2 = radp;
  309. radp2 *= 2;
  310. if ( u8g_IsBBXIntersection(u8g, x0-radp, y0-radp, radp2, radp2) == 0)
  311. return;
  312. }
  313. /* draw disc */
  314. u8g_draw_disc(u8g, x0, y0, rad, option);
  315. }