25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

conic.c 4.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include <stdlib.h>
  29. #include <inttypes.h>
  30. #include "ee.h"
  31. static void ellipsepoints(int, int, int, int, char);
  32. void ee_draw_circle(int x, int y, int r, char c)
  33. {
  34. int test, dx, dy;
  35. /* Optimized Bresenham. Kick ass. */
  36. for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
  37. {
  38. ellipsepoints(x, y, dx, dy, c);
  39. ellipsepoints(x, y, dy, dx, c);
  40. test += test > 0 ? dx - dy-- : dx;
  41. }
  42. }
  43. void ee_fill_ellipse(int xo, int yo, int a, int b, char c)
  44. {
  45. int d2;
  46. int x = 0;
  47. int y = b;
  48. int d1 = b*b - (a*a*b) + (a*a/4);
  49. while( a*a*y - a*a/2 > b*b*(x+1))
  50. {
  51. if(d1 < 0)
  52. {
  53. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  54. }
  55. else
  56. {
  57. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  58. ee_draw_line(xo - x, yo - y, xo + x, yo - y, c);
  59. ee_draw_line(xo - x, yo + y, xo + x, yo + y, c);
  60. y--;
  61. }
  62. x++;
  63. }
  64. ee_draw_line(xo - x, yo - y, xo + x, yo - y, c);
  65. ee_draw_line(xo - x, yo + y, xo + x, yo + y, c);
  66. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  67. while(y > 0)
  68. {
  69. if(d2 < 0)
  70. {
  71. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  72. x++;
  73. }
  74. else
  75. {
  76. d2 += a*a*(-2*y+3);
  77. }
  78. y--;
  79. ee_draw_line(xo - x, yo - y, xo + x, yo - y, c);
  80. ee_draw_line(xo - x, yo + y, xo + x, yo + y, c);
  81. }
  82. }
  83. void ee_draw_ellipse(int xo, int yo, int a, int b, char c)
  84. {
  85. int d2;
  86. int x = 0;
  87. int y = b;
  88. int d1 = b*b - (a*a*b) + (a*a/4);
  89. ellipsepoints(xo, yo, x, y, c);
  90. while( a*a*y - a*a/2 > b*b*(x+1))
  91. {
  92. if(d1 < 0)
  93. {
  94. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  95. }
  96. else
  97. {
  98. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  99. y--;
  100. }
  101. x++;
  102. ellipsepoints(xo, yo, x, y, c);
  103. }
  104. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  105. while(y > 0)
  106. {
  107. if(d2 < 0)
  108. {
  109. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  110. x++;
  111. }
  112. else
  113. {
  114. d2 += a*a*(-2*y+3);
  115. }
  116. y--;
  117. ellipsepoints(xo, yo, x, y, c);
  118. }
  119. }
  120. void ee_draw_thin_ellipse(int xo, int yo, int a, int b)
  121. {
  122. /* FIXME: this is not correct */
  123. int d2;
  124. int x = 0;
  125. int y = b;
  126. int d1 = b*b - (a*a*b) + (a*a/4);
  127. ellipsepoints(xo, yo, x, y, '-');
  128. while( a*a*y - a*a/2 > b*b*(x+1))
  129. {
  130. if(d1 < 0)
  131. {
  132. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  133. }
  134. else
  135. {
  136. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  137. y--;
  138. }
  139. x++;
  140. ellipsepoints(xo, yo, x, y, '-');
  141. }
  142. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  143. while(y > 0)
  144. {
  145. if(d2 < 0)
  146. {
  147. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  148. x++;
  149. }
  150. else
  151. {
  152. d2 += a*a*(-2*y+3);
  153. }
  154. y--;
  155. ellipsepoints(xo, yo, x, y, '|');
  156. }
  157. }
  158. static void ellipsepoints(int xo, int yo, int x, int y, char c)
  159. {
  160. uint8_t b = 0;
  161. if(xo + x >= 0 && xo + x < ee_get_width())
  162. b |= 0x1;
  163. if(xo - x >= 0 && xo - x < ee_get_width())
  164. b |= 0x2;
  165. if(yo + y >= 0 && yo + y < ee_get_height())
  166. b |= 0x4;
  167. if(yo - y >= 0 && yo - y < ee_get_height())
  168. b |= 0x8;
  169. if((b & (0x1|0x4)) == (0x1|0x4))
  170. ee_putchar(xo + x, yo + y, c);
  171. if((b & (0x2|0x4)) == (0x2|0x4))
  172. ee_putchar(xo - x, yo + y, c);
  173. if((b & (0x1|0x8)) == (0x1|0x8))
  174. ee_putchar(xo + x, yo - y, c);
  175. if((b & (0x2|0x8)) == (0x2|0x8))
  176. ee_putchar(xo - x, yo - y, c);
  177. }