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.
 
 
 
 
 

213 rivejä
4.7 KiB

  1. /*
  2. * libee ASCII-Art library
  3. * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "config.h"
  23. #ifdef USE_SLANG
  24. # include <slang.h>
  25. #elif USE_NCURSES
  26. # include <curses.h>
  27. #endif
  28. #ifdef HAVE_INTTYPES_H
  29. # include <inttypes.h>
  30. #else
  31. typedef unsigned char uint8_t;
  32. #endif
  33. #include <stdlib.h>
  34. #include "ee.h"
  35. static void ellipsepoints(int, int, int, int, char);
  36. void ee_draw_circle(int x, int y, int r, char c)
  37. {
  38. int test, dx, dy;
  39. /* Optimized Bresenham. Kick ass. */
  40. for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
  41. {
  42. ellipsepoints(x, y, dx, dy, c);
  43. ellipsepoints(x, y, dy, dx, c);
  44. test += test > 0 ? dx - dy-- : dx;
  45. }
  46. }
  47. void ee_fill_ellipse(int xo, int yo, int a, int b, char c)
  48. {
  49. int d2;
  50. int x = 0;
  51. int y = b;
  52. int d1 = b*b - (a*a*b) + (a*a/4);
  53. while( a*a*y - a*a/2 > b*b*(x+1))
  54. {
  55. if(d1 < 0)
  56. {
  57. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  58. }
  59. else
  60. {
  61. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  62. ee_draw_line(xo - x, yo - y, xo + x, yo - y, c);
  63. ee_draw_line(xo - x, yo + y, xo + x, yo + y, c);
  64. y--;
  65. }
  66. x++;
  67. }
  68. ee_draw_line(xo - x, yo - y, xo + x, yo - y, c);
  69. ee_draw_line(xo - x, yo + y, xo + x, yo + y, c);
  70. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  71. while(y > 0)
  72. {
  73. if(d2 < 0)
  74. {
  75. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  76. x++;
  77. }
  78. else
  79. {
  80. d2 += a*a*(-2*y+3);
  81. }
  82. y--;
  83. ee_draw_line(xo - x, yo - y, xo + x, yo - y, c);
  84. ee_draw_line(xo - x, yo + y, xo + x, yo + y, c);
  85. }
  86. }
  87. void ee_draw_ellipse(int xo, int yo, int a, int b, char c)
  88. {
  89. int d2;
  90. int x = 0;
  91. int y = b;
  92. int d1 = b*b - (a*a*b) + (a*a/4);
  93. ellipsepoints(xo, yo, x, y, c);
  94. while( a*a*y - a*a/2 > b*b*(x+1))
  95. {
  96. if(d1 < 0)
  97. {
  98. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  99. }
  100. else
  101. {
  102. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  103. y--;
  104. }
  105. x++;
  106. ellipsepoints(xo, yo, x, y, c);
  107. }
  108. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  109. while(y > 0)
  110. {
  111. if(d2 < 0)
  112. {
  113. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  114. x++;
  115. }
  116. else
  117. {
  118. d2 += a*a*(-2*y+3);
  119. }
  120. y--;
  121. ellipsepoints(xo, yo, x, y, c);
  122. }
  123. }
  124. void ee_draw_thin_ellipse(int xo, int yo, int a, int b)
  125. {
  126. /* FIXME: this is not correct */
  127. int d2;
  128. int x = 0;
  129. int y = b;
  130. int d1 = b*b - (a*a*b) + (a*a/4);
  131. ellipsepoints(xo, yo, x, y, '-');
  132. while( a*a*y - a*a/2 > b*b*(x+1))
  133. {
  134. if(d1 < 0)
  135. {
  136. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  137. }
  138. else
  139. {
  140. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  141. y--;
  142. }
  143. x++;
  144. ellipsepoints(xo, yo, x, y, '-');
  145. }
  146. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  147. while(y > 0)
  148. {
  149. if(d2 < 0)
  150. {
  151. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  152. x++;
  153. }
  154. else
  155. {
  156. d2 += a*a*(-2*y+3);
  157. }
  158. y--;
  159. ellipsepoints(xo, yo, x, y, '|');
  160. }
  161. }
  162. static void ellipsepoints(int xo, int yo, int x, int y, char c)
  163. {
  164. uint8_t b = 0;
  165. if(xo + x >= 0 && xo + x < ee_get_width())
  166. b |= 0x1;
  167. if(xo - x >= 0 && xo - x < ee_get_width())
  168. b |= 0x2;
  169. if(yo + y >= 0 && yo + y < ee_get_height())
  170. b |= 0x4;
  171. if(yo - y >= 0 && yo - y < ee_get_height())
  172. b |= 0x8;
  173. if((b & (0x1|0x4)) == (0x1|0x4))
  174. ee_putchar(xo + x, yo + y, c);
  175. if((b & (0x2|0x4)) == (0x2|0x4))
  176. ee_putchar(xo - x, yo + y, c);
  177. if((b & (0x1|0x8)) == (0x1|0x8))
  178. ee_putchar(xo + x, yo - y, c);
  179. if((b & (0x2|0x8)) == (0x2|0x8))
  180. ee_putchar(xo - x, yo - y, c);
  181. }