source: ttyvaders/trunk/libee/conic.c @ 156

Last change on this file since 156 was 156, checked in by Sam Hocevar, 16 years ago
  • MS-DOS port of libee, using <conio.h>.
  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
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 156 2003-11-12 16:23:18Z 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#ifdef HAVE_INTTYPES_H
32#   include <inttypes.h>
33#else
34typedef unsigned char uint8_t;
35#endif
36
37#include <stdlib.h>
38
39#include "ee.h"
40
41static void ellipsepoints(int, int, int, int, char);
42
43void ee_draw_circle(int x, int y, int r, char c)
44{
45    int test, dx, dy;
46
47    /* Optimized Bresenham. Kick ass. */
48    for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
49    {
50        ellipsepoints(x, y, dx, dy, c);
51        ellipsepoints(x, y, dy, dx, c);
52
53        test += test > 0 ? dx - dy-- : dx;
54    }
55}
56
57void ee_fill_ellipse(int xo, int yo, int a, int b, char c)
58{
59    int d2;
60    int x = 0;
61    int y = b;
62    int d1 = b*b - (a*a*b) + (a*a/4);
63
64    while( a*a*y - a*a/2 > b*b*(x+1))
65    {
66        if(d1 < 0)
67        {
68            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
69        }
70        else
71        {
72            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
73            ee_draw_line(xo - x, yo - y, xo + x, yo - y, c);
74            ee_draw_line(xo - x, yo + y, xo + x, yo + y, c);
75            y--;
76        }
77        x++;
78    }
79
80    ee_draw_line(xo - x, yo - y, xo + x, yo - y, c);
81    ee_draw_line(xo - x, yo + y, xo + x, yo + y, c);
82
83    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
84    while(y > 0)
85    {
86        if(d2 < 0)
87        {
88            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
89            x++;
90        }
91        else
92        {
93            d2 += a*a*(-2*y+3);
94        }
95
96        y--;
97        ee_draw_line(xo - x, yo - y, xo + x, yo - y, c);
98        ee_draw_line(xo - x, yo + y, xo + x, yo + y, c);
99    }
100}
101
102void ee_draw_ellipse(int xo, int yo, int a, int b, char c)
103{
104    int d2;
105    int x = 0;
106    int y = b;
107    int d1 = b*b - (a*a*b) + (a*a/4);
108
109    ellipsepoints(xo, yo, x, y, c);
110
111    while( a*a*y - a*a/2 > b*b*(x+1))
112    {
113        if(d1 < 0)
114        {
115            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
116        }
117        else
118        {
119            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
120            y--;
121        }
122        x++;
123        ellipsepoints(xo, yo, x, y, c);
124    }
125
126    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
127    while(y > 0)
128    {
129        if(d2 < 0)
130        {
131            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
132            x++;
133        }
134        else
135        {
136            d2 += a*a*(-2*y+3);
137        }
138
139        y--;
140        ellipsepoints(xo, yo, x, y, c);
141    }
142}
143
144void ee_draw_thin_ellipse(int xo, int yo, int a, int b)
145{
146    /* FIXME: this is not correct */
147    int d2;
148    int x = 0;
149    int y = b;
150    int d1 = b*b - (a*a*b) + (a*a/4);
151
152    ellipsepoints(xo, yo, x, y, '-');
153
154    while( a*a*y - a*a/2 > b*b*(x+1))
155    {
156        if(d1 < 0)
157        {
158            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
159        }
160        else
161        {
162            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
163            y--;
164        }
165        x++;
166        ellipsepoints(xo, yo, x, y, '-');
167    }
168
169    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
170    while(y > 0)
171    {
172        if(d2 < 0)
173        {
174            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
175            x++;
176        }
177        else
178        {
179            d2 += a*a*(-2*y+3);
180        }
181
182        y--;
183        ellipsepoints(xo, yo, x, y, '|');
184    }
185}
186
187static void ellipsepoints(int xo, int yo, int x, int y, char c)
188{
189    uint8_t b = 0;
190
191    if(xo + x >= 0 && xo + x < ee_get_width())
192        b |= 0x1;
193    if(xo - x >= 0 && xo - x < ee_get_width())
194        b |= 0x2;
195    if(yo + y >= 0 && yo + y < ee_get_height())
196        b |= 0x4;
197    if(yo - y >= 0 && yo - y < ee_get_height())
198        b |= 0x8;
199
200    if((b & (0x1|0x4)) == (0x1|0x4))
201        ee_putchar(xo + x, yo + y, c);
202
203    if((b & (0x2|0x4)) == (0x2|0x4))
204        ee_putchar(xo - x, yo + y, c);
205
206    if((b & (0x1|0x8)) == (0x1|0x8))
207        ee_putchar(xo + x, yo - y, c);
208
209    if((b & (0x2|0x8)) == (0x2|0x8))
210        ee_putchar(xo - x, yo - y, c);
211}
212
Note: See TracBrowser for help on using the repository browser.