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.
 
 
 
 
 
 

120 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. #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_draw_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. ellipsepoints(xo, yo, x, y, c);
  50. while( a*a*y - a*a/2 > b*b*(x+1))
  51. {
  52. if(d1 < 0)
  53. {
  54. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  55. }
  56. else
  57. {
  58. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  59. y--;
  60. }
  61. x++;
  62. ellipsepoints(xo, yo, x, y, c);
  63. }
  64. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  65. while(y > 0)
  66. {
  67. if(d2 < 0)
  68. {
  69. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  70. x++;
  71. }
  72. else
  73. {
  74. d2 += a*a*(-2*y+3);
  75. }
  76. y--;
  77. ellipsepoints(xo, yo, x, y, c);
  78. }
  79. }
  80. static void ellipsepoints(int xo, int yo, int x, int y, char c)
  81. {
  82. uint8_t b = 0;
  83. if(xo + x >= 0 && xo + x < ee_get_width())
  84. b |= 0x1;
  85. if(xo - x >= 0 && xo - x < ee_get_width())
  86. b |= 0x2;
  87. if(yo + y >= 0 && yo + y < ee_get_height())
  88. b |= 0x4;
  89. if(yo - y >= 0 && yo - y < ee_get_height())
  90. b |= 0x8;
  91. if((b & (0x1|0x4)) == (0x1|0x4))
  92. ee_putchar(xo + x, yo + y, c);
  93. if((b & (0x2|0x4)) == (0x2|0x4))
  94. ee_putchar(xo - x, yo + y, c);
  95. if((b & (0x1|0x8)) == (0x1|0x8))
  96. ee_putchar(xo + x, yo - y, c);
  97. if((b & (0x2|0x8)) == (0x2|0x8))
  98. ee_putchar(xo - x, yo - y, c);
  99. }