source: libcaca/trunk/src/conic.c @ 192

Last change on this file since 192 was 192, checked in by Sam Hocevar, 19 years ago
  • Changed copyleft to LGPL.
  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/*
2 *   libcaca       ASCII-Art library
3 *   Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: conic.c 192 2003-11-16 12:28:29Z sam $
7 *
8 *   This library is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU Lesser General Public
10 *   License as published by the Free Software Foundation; either
11 *   version 2 of the License, or (at your option) any later version.
12 *
13 *   This library 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 GNU
16 *   Lesser General Public License for more details.
17 *
18 *   You should have received a copy of the GNU Lesser General Public
19 *   License along with this library; if not, write to the Free Software
20 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 *   02111-1307  USA
22 */
23
24#include "config.h"
25
26#ifdef HAVE_INTTYPES_H
27#   include <inttypes.h>
28#else
29typedef unsigned char uint8_t;
30#endif
31
32#include <stdlib.h>
33
34#include "caca.h"
35#include "caca_internals.h"
36
37static void ellipsepoints(int, int, int, int, char);
38
39void caca_draw_circle(int x, int y, int r, char c)
40{
41    int test, dx, dy;
42
43    /* Optimized Bresenham. Kick ass. */
44    for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
45    {
46        ellipsepoints(x, y, dx, dy, c);
47        ellipsepoints(x, y, dy, dx, c);
48
49        test += test > 0 ? dx - dy-- : dx;
50    }
51}
52
53void caca_fill_ellipse(int xo, int yo, int a, int b, char c)
54{
55    int d2;
56    int x = 0;
57    int y = b;
58    int d1 = b*b - (a*a*b) + (a*a/4);
59
60    while(a*a*y - a*a/2 > b*b*(x+1))
61    {
62        if(d1 < 0)
63        {
64            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
65        }
66        else
67        {
68            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
69            caca_draw_line(xo - x, yo - y, xo + x, yo - y, c);
70            caca_draw_line(xo - x, yo + y, xo + x, yo + y, c);
71            y--;
72        }
73        x++;
74    }
75
76    caca_draw_line(xo - x, yo - y, xo + x, yo - y, c);
77    caca_draw_line(xo - x, yo + y, xo + x, yo + y, c);
78
79    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
80    while(y > 0)
81    {
82        if(d2 < 0)
83        {
84            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
85            x++;
86        }
87        else
88        {
89            d2 += a*a*(-2*y+3);
90        }
91
92        y--;
93        caca_draw_line(xo - x, yo - y, xo + x, yo - y, c);
94        caca_draw_line(xo - x, yo + y, xo + x, yo + y, c);
95    }
96}
97
98void caca_draw_ellipse(int xo, int yo, int a, int b, char c)
99{
100    int d2;
101    int x = 0;
102    int y = b;
103    int d1 = b*b - (a*a*b) + (a*a/4);
104
105    ellipsepoints(xo, yo, x, y, c);
106
107    while(a*a*y - a*a/2 > b*b*(x+1))
108    {
109        if(d1 < 0)
110        {
111            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
112        }
113        else
114        {
115            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
116            y--;
117        }
118        x++;
119        ellipsepoints(xo, yo, x, y, c);
120    }
121
122    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
123    while(y > 0)
124    {
125        if(d2 < 0)
126        {
127            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
128            x++;
129        }
130        else
131        {
132            d2 += a*a*(-2*y+3);
133        }
134
135        y--;
136        ellipsepoints(xo, yo, x, y, c);
137    }
138}
139
140void caca_draw_thin_ellipse(int xo, int yo, int a, int b)
141{
142    /* FIXME: this is not correct */
143    int d2;
144    int x = 0;
145    int y = b;
146    int d1 = b*b - (a*a*b) + (a*a/4);
147
148    ellipsepoints(xo, yo, x, y, '-');
149
150    while(a*a*y - a*a/2 > b*b*(x+1))
151    {
152        if(d1 < 0)
153        {
154            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
155        }
156        else
157        {
158            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
159            y--;
160        }
161        x++;
162        ellipsepoints(xo, yo, x, y, '-');
163    }
164
165    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
166    while(y > 0)
167    {
168        if(d2 < 0)
169        {
170            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
171            x++;
172        }
173        else
174        {
175            d2 += a*a*(-2*y+3);
176        }
177
178        y--;
179        ellipsepoints(xo, yo, x, y, '|');
180    }
181}
182
183static void ellipsepoints(int xo, int yo, int x, int y, char c)
184{
185    uint8_t b = 0;
186
187    if(xo + x >= 0 && xo + x < (int)caca_get_width())
188        b |= 0x1;
189    if(xo - x >= 0 && xo - x < (int)caca_get_width())
190        b |= 0x2;
191    if(yo + y >= 0 && yo + y < (int)caca_get_height())
192        b |= 0x4;
193    if(yo - y >= 0 && yo - y < (int)caca_get_height())
194        b |= 0x8;
195
196    if((b & (0x1|0x4)) == (0x1|0x4))
197        caca_putchar(xo + x, yo + y, c);
198
199    if((b & (0x2|0x4)) == (0x2|0x4))
200        caca_putchar(xo - x, yo + y, c);
201
202    if((b & (0x1|0x8)) == (0x1|0x8))
203        caca_putchar(xo + x, yo - y, c);
204
205    if((b & (0x2|0x8)) == (0x2|0x8))
206        caca_putchar(xo - x, yo - y, c);
207}
208
Note: See TracBrowser for help on using the repository browser.