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.
 
 
 
 
 

126 lines
2.8 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. #include <stdlib.h>
  24. #include <inttypes.h>
  25. #include "ee.h"
  26. static void ellipsepoints(int, int, int, int, char);
  27. void ee_draw_circle(int x, int y, int r, char c)
  28. {
  29. int test, dx, dy;
  30. /* Optimized Bresenham. Kick ass. */
  31. for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
  32. {
  33. ellipsepoints(x, y, dx, dy, c);
  34. ellipsepoints(x, y, dy, dx, c);
  35. test += test > 0 ? dx - dy-- : dx;
  36. }
  37. }
  38. void ee_draw_ellipse(int xo, int yo, int a, int b, char c)
  39. {
  40. int d2;
  41. int x = 0;
  42. int y = b;
  43. int d1 = b*b - (a*a*b) + (a*a/4);
  44. ellipsepoints(xo, yo, x, y, c);
  45. while( a*a*y - a*a/2 > b*b*(x+1))
  46. {
  47. if(d1 < 0)
  48. {
  49. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  50. }
  51. else
  52. {
  53. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  54. y--;
  55. }
  56. x++;
  57. ellipsepoints(xo, yo, x, y, c);
  58. }
  59. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  60. while(y > 0)
  61. {
  62. if(d2 < 0)
  63. {
  64. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  65. x++;
  66. }
  67. else
  68. {
  69. d2 += a*a*(-2*y+3);
  70. }
  71. y--;
  72. ellipsepoints(xo, yo, x, y, c);
  73. }
  74. }
  75. static void ellipsepoints(int xo, int yo, int x, int y, char c)
  76. {
  77. uint8_t b = 0;
  78. if(xo + x >= 0 && xo + x < ee_get_width())
  79. b |= 0x1;
  80. if(xo - x >= 0 && xo - x < ee_get_width())
  81. b |= 0x2;
  82. if(yo + y >= 0 && yo + y < ee_get_height())
  83. b |= 0x4;
  84. if(yo - y >= 0 && yo - y < ee_get_height())
  85. b |= 0x8;
  86. if((b & (0x1|0x4)) == (0x1|0x4))
  87. {
  88. ee_goto(xo + x, yo + y);
  89. ee_putchar(c);
  90. }
  91. if((b & (0x2|0x4)) == (0x2|0x4))
  92. {
  93. ee_goto(xo - x, yo + y);
  94. ee_putchar(c);
  95. }
  96. if((b & (0x1|0x8)) == (0x1|0x8))
  97. {
  98. ee_goto(xo + x, yo - y);
  99. ee_putchar(c);
  100. }
  101. if((b & (0x2|0x8)) == (0x2|0x8))
  102. {
  103. ee_goto(xo - x, yo - y);
  104. ee_putchar(c);
  105. }
  106. }