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

Last change on this file since 341 was 322, checked in by Sam Hocevar, 16 years ago
  • libcaca.spec debian/control: + Updated the package descriptions to include cacaball.
  • doc/cacademo.1 doc/Makefile.am: + Use the cacademo manpage as the cacaball manpage.
  • src/: + Added #ifdef _DOXYGEN_SKIP_ME here and there to prvent Doxygen from

documenting bizarre stuff.

  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1/*
2 *  libcaca       ASCII-Art library
3 *  Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the GNU Lesser General Public
8 *  License as published by the Free Software Foundation; either
9 *  version 2 of the License, or (at your option) any later version.
10 *
11 *  This library is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 *  Lesser General Public License for more details.
15 *
16 *  You should have received a copy of the GNU Lesser General Public
17 *  License along with this library; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 *  02111-1307  USA
20 */
21
22/** \file conic.c
23 *  \version \$Id: conic.c 322 2004-01-07 12:45:02Z sam $
24 *  \author Sam Hocevar <sam@zoy.org>
25 *  \brief Ellipse and circle drawing
26 *
27 *  This file contains ellipse and circle drawing functions, both filled
28 *  and outline.
29 */
30
31#include "config.h"
32
33#if defined(HAVE_INTTYPES_H) || defined(_DOXYGEN_SKIP_ME)
34#   include <inttypes.h>
35#else
36typedef unsigned char uint8_t;
37#endif
38
39#include <stdlib.h>
40
41#include "caca.h"
42#include "caca_internals.h"
43
44static void ellipsepoints(int, int, int, int, char);
45
46/**
47 * \brief Draw a circle on the screen using the given character.
48 *
49 * \param x Center X coordinate.
50 * \param y Center Y coordinate.
51 * \param r Circle radius.
52 * \param c Character to draw the circle outline with.
53 * \return void
54 */
55void caca_draw_circle(int x, int y, int r, char c)
56{
57    int test, dx, dy;
58
59    /* Optimized Bresenham. Kick ass. */
60    for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
61    {
62        ellipsepoints(x, y, dx, dy, c);
63        ellipsepoints(x, y, dy, dx, c);
64
65        test += test > 0 ? dx - dy-- : dx;
66    }
67}
68
69/**
70 * \brief Fill an ellipse on the screen using the given character.
71 *
72 * \param xo Center X coordinate.
73 * \param yo Center Y coordinate.
74 * \param a Ellipse X radius.
75 * \param b Ellipse Y radius.
76 * \param c Character to fill the ellipse with.
77 * \return void
78 */
79void caca_fill_ellipse(int xo, int yo, int a, int b, char c)
80{
81    int d2;
82    int x = 0;
83    int y = b;
84    int d1 = b*b - (a*a*b) + (a*a/4);
85
86    while(a*a*y - a*a/2 > b*b*(x+1))
87    {
88        if(d1 < 0)
89        {
90            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
91        }
92        else
93        {
94            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
95            caca_draw_line(xo - x, yo - y, xo + x, yo - y, c);
96            caca_draw_line(xo - x, yo + y, xo + x, yo + y, c);
97            y--;
98        }
99        x++;
100    }
101
102    caca_draw_line(xo - x, yo - y, xo + x, yo - y, c);
103    caca_draw_line(xo - x, yo + y, xo + x, yo + y, c);
104
105    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
106    while(y > 0)
107    {
108        if(d2 < 0)
109        {
110            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
111            x++;
112        }
113        else
114        {
115            d2 += a*a*(-2*y+3);
116        }
117
118        y--;
119        caca_draw_line(xo - x, yo - y, xo + x, yo - y, c);
120        caca_draw_line(xo - x, yo + y, xo + x, yo + y, c);
121    }
122}
123
124/**
125 * \brief Draw an ellipse on the screen using the given character.
126 *
127 * \param xo Center X coordinate.
128 * \param yo Center Y coordinate.
129 * \param a Ellipse X radius.
130 * \param b Ellipse Y radius.
131 * \param c Character to draw the ellipse outline with.
132 * \return void
133 */
134void caca_draw_ellipse(int xo, int yo, int a, int b, char c)
135{
136    int d2;
137    int x = 0;
138    int y = b;
139    int d1 = b*b - (a*a*b) + (a*a/4);
140
141    ellipsepoints(xo, yo, x, y, c);
142
143    while(a*a*y - a*a/2 > b*b*(x+1))
144    {
145        if(d1 < 0)
146        {
147            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
148        }
149        else
150        {
151            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
152            y--;
153        }
154        x++;
155        ellipsepoints(xo, yo, x, y, c);
156    }
157
158    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
159    while(y > 0)
160    {
161        if(d2 < 0)
162        {
163            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
164            x++;
165        }
166        else
167        {
168            d2 += a*a*(-2*y+3);
169        }
170
171        y--;
172        ellipsepoints(xo, yo, x, y, c);
173    }
174}
175
176/**
177 * \brief Draw a thin ellipse on the screen.
178 *
179 * \param xo Center X coordinate.
180 * \param yo Center Y coordinate.
181 * \param a Ellipse X radius.
182 * \param b Ellipse Y radius.
183 * \return void
184 */
185void caca_draw_thin_ellipse(int xo, int yo, int a, int b)
186{
187    /* FIXME: this is not correct */
188    int d2;
189    int x = 0;
190    int y = b;
191    int d1 = b*b - (a*a*b) + (a*a/4);
192
193    ellipsepoints(xo, yo, x, y, '-');
194
195    while(a*a*y - a*a/2 > b*b*(x+1))
196    {
197        if(d1 < 0)
198        {
199            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
200        }
201        else
202        {
203            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
204            y--;
205        }
206        x++;
207        ellipsepoints(xo, yo, x, y, '-');
208    }
209
210    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
211    while(y > 0)
212    {
213        if(d2 < 0)
214        {
215            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
216            x++;
217        }
218        else
219        {
220            d2 += a*a*(-2*y+3);
221        }
222
223        y--;
224        ellipsepoints(xo, yo, x, y, '|');
225    }
226}
227
228static void ellipsepoints(int xo, int yo, int x, int y, char c)
229{
230    uint8_t b = 0;
231
232    if(xo + x >= 0 && xo + x < (int)_caca_width)
233        b |= 0x1;
234    if(xo - x >= 0 && xo - x < (int)_caca_width)
235        b |= 0x2;
236    if(yo + y >= 0 && yo + y < (int)_caca_height)
237        b |= 0x4;
238    if(yo - y >= 0 && yo - y < (int)_caca_height)
239        b |= 0x8;
240
241    if((b & (0x1|0x4)) == (0x1|0x4))
242        caca_putchar(xo + x, yo + y, c);
243
244    if((b & (0x2|0x4)) == (0x2|0x4))
245        caca_putchar(xo - x, yo + y, c);
246
247    if((b & (0x1|0x8)) == (0x1|0x8))
248        caca_putchar(xo + x, yo - y, c);
249
250    if((b & (0x2|0x8)) == (0x2|0x8))
251        caca_putchar(xo - x, yo - y, c);
252}
253
Note: See TracBrowser for help on using the repository browser.