1 | /* |
---|
2 | * libee ASCII-Art library |
---|
3 | * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: conic.c 148 2003-11-10 23:38:50Z sam $ |
---|
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 | |
---|
23 | #include "config.h" |
---|
24 | |
---|
25 | #ifdef USE_SLANG |
---|
26 | # include <slang.h> |
---|
27 | #elif USE_NCURSES |
---|
28 | # include <curses.h> |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <stdlib.h> |
---|
32 | #include <inttypes.h> |
---|
33 | |
---|
34 | #include "ee.h" |
---|
35 | |
---|
36 | static void ellipsepoints(int, int, int, int, char); |
---|
37 | |
---|
38 | void ee_draw_circle(int x, int y, int r, char c) |
---|
39 | { |
---|
40 | int test, dx, dy; |
---|
41 | |
---|
42 | /* Optimized Bresenham. Kick ass. */ |
---|
43 | for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++) |
---|
44 | { |
---|
45 | ellipsepoints(x, y, dx, dy, c); |
---|
46 | ellipsepoints(x, y, dy, dx, c); |
---|
47 | |
---|
48 | test += test > 0 ? dx - dy-- : dx; |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | void ee_draw_ellipse(int xo, int yo, int a, int b, char c) |
---|
53 | { |
---|
54 | int d2; |
---|
55 | int x = 0; |
---|
56 | int y = b; |
---|
57 | int d1 = b*b - (a*a*b) + (a*a/4); |
---|
58 | |
---|
59 | ellipsepoints(xo, yo, x, y, c); |
---|
60 | |
---|
61 | while( a*a*y - a*a/2 > b*b*(x+1)) |
---|
62 | { |
---|
63 | if(d1 < 0) |
---|
64 | { |
---|
65 | d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ |
---|
66 | } |
---|
67 | else |
---|
68 | { |
---|
69 | d1 += b*b*(2*x*1) + a*a*(-2*y+2); |
---|
70 | y--; |
---|
71 | } |
---|
72 | x++; |
---|
73 | ellipsepoints(xo, yo, x, y, c); |
---|
74 | } |
---|
75 | |
---|
76 | d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; |
---|
77 | while(y > 0) |
---|
78 | { |
---|
79 | if(d2 < 0) |
---|
80 | { |
---|
81 | d2 += b*b*(2*x+2) + a*a*(-2*y+3); |
---|
82 | x++; |
---|
83 | } |
---|
84 | else |
---|
85 | { |
---|
86 | d2 += a*a*(-2*y+3); |
---|
87 | } |
---|
88 | |
---|
89 | y--; |
---|
90 | ellipsepoints(xo, yo, x, y, c); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | static void ellipsepoints(int xo, int yo, int x, int y, char c) |
---|
95 | { |
---|
96 | uint8_t b = 0; |
---|
97 | |
---|
98 | if(xo + x >= 0 && xo + x < ee_get_width()) |
---|
99 | b |= 0x1; |
---|
100 | if(xo - x >= 0 && xo - x < ee_get_width()) |
---|
101 | b |= 0x2; |
---|
102 | if(yo + y >= 0 && yo + y < ee_get_height()) |
---|
103 | b |= 0x4; |
---|
104 | if(yo - y >= 0 && yo - y < ee_get_height()) |
---|
105 | b |= 0x8; |
---|
106 | |
---|
107 | if((b & (0x1|0x4)) == (0x1|0x4)) |
---|
108 | ee_putchar(xo + x, yo + y, c); |
---|
109 | |
---|
110 | if((b & (0x2|0x4)) == (0x2|0x4)) |
---|
111 | ee_putchar(xo - x, yo + y, c); |
---|
112 | |
---|
113 | if((b & (0x1|0x8)) == (0x1|0x8)) |
---|
114 | ee_putchar(xo + x, yo - y, c); |
---|
115 | |
---|
116 | if((b & (0x2|0x8)) == (0x2|0x8)) |
---|
117 | ee_putchar(xo - x, yo - y, c); |
---|
118 | } |
---|
119 | |
---|