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

Last change on this file since 246 was 246, checked in by Sam Hocevar, 18 years ago
  • src/graphics.c: + Internally cache screen width and height.
  • src/bitmap.c: + Added alpha support to caca_draw_bitmap(). For now, we only treat 0%

alpha as fully transparent, and any other value as fully opaque.

+ Slightly tuned colour weights in the renderer.
+ caca_set_bitmap_palette() takes unsigned ints.

  • examples/demo.c: + Added a crap render demo.
  • examples/aafire.c: + Set a 20ms delay. + Added alpha values to the palette.
  • examples/view.c: + Draw a gray checkered grid below the image so that transparent images

look a lot nicer.

+ 'f' toggles "fullscreen" mode.

  • Property svn:keywords set to Id
File size: 4.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 246 2003-12-11 16:31:49Z sam $
24 *   \author Sam Hocevar <sam@zoy.org>
25 *   \brief Ellipse and circle drawing functions
26 *
27 *   This file contains ellipse and circle drawing functions, both filled
28 *   and outline.
29 */
30
31#include "config.h"
32
33#ifdef HAVE_INTTYPES_H
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
46void caca_draw_circle(int x, int y, int r, char c)
47{
48    int test, dx, dy;
49
50    /* Optimized Bresenham. Kick ass. */
51    for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
52    {
53        ellipsepoints(x, y, dx, dy, c);
54        ellipsepoints(x, y, dy, dx, c);
55
56        test += test > 0 ? dx - dy-- : dx;
57    }
58}
59
60void caca_fill_ellipse(int xo, int yo, int a, int b, char c)
61{
62    int d2;
63    int x = 0;
64    int y = b;
65    int d1 = b*b - (a*a*b) + (a*a/4);
66
67    while(a*a*y - a*a/2 > b*b*(x+1))
68    {
69        if(d1 < 0)
70        {
71            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
72        }
73        else
74        {
75            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
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            y--;
79        }
80        x++;
81    }
82
83    caca_draw_line(xo - x, yo - y, xo + x, yo - y, c);
84    caca_draw_line(xo - x, yo + y, xo + x, yo + y, c);
85
86    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
87    while(y > 0)
88    {
89        if(d2 < 0)
90        {
91            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
92            x++;
93        }
94        else
95        {
96            d2 += a*a*(-2*y+3);
97        }
98
99        y--;
100        caca_draw_line(xo - x, yo - y, xo + x, yo - y, c);
101        caca_draw_line(xo - x, yo + y, xo + x, yo + y, c);
102    }
103}
104
105void caca_draw_ellipse(int xo, int yo, int a, int b, char c)
106{
107    int d2;
108    int x = 0;
109    int y = b;
110    int d1 = b*b - (a*a*b) + (a*a/4);
111
112    ellipsepoints(xo, yo, x, y, c);
113
114    while(a*a*y - a*a/2 > b*b*(x+1))
115    {
116        if(d1 < 0)
117        {
118            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
119        }
120        else
121        {
122            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
123            y--;
124        }
125        x++;
126        ellipsepoints(xo, yo, x, y, c);
127    }
128
129    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
130    while(y > 0)
131    {
132        if(d2 < 0)
133        {
134            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
135            x++;
136        }
137        else
138        {
139            d2 += a*a*(-2*y+3);
140        }
141
142        y--;
143        ellipsepoints(xo, yo, x, y, c);
144    }
145}
146
147void caca_draw_thin_ellipse(int xo, int yo, int a, int b)
148{
149    /* FIXME: this is not correct */
150    int d2;
151    int x = 0;
152    int y = b;
153    int d1 = b*b - (a*a*b) + (a*a/4);
154
155    ellipsepoints(xo, yo, x, y, '-');
156
157    while(a*a*y - a*a/2 > b*b*(x+1))
158    {
159        if(d1 < 0)
160        {
161            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
162        }
163        else
164        {
165            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
166            y--;
167        }
168        x++;
169        ellipsepoints(xo, yo, x, y, '-');
170    }
171
172    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
173    while(y > 0)
174    {
175        if(d2 < 0)
176        {
177            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
178            x++;
179        }
180        else
181        {
182            d2 += a*a*(-2*y+3);
183        }
184
185        y--;
186        ellipsepoints(xo, yo, x, y, '|');
187    }
188}
189
190static void ellipsepoints(int xo, int yo, int x, int y, char c)
191{
192    uint8_t b = 0;
193
194    if(xo + x >= 0 && xo + x < (int)_caca_width)
195        b |= 0x1;
196    if(xo - x >= 0 && xo - x < (int)_caca_width)
197        b |= 0x2;
198    if(yo + y >= 0 && yo + y < (int)_caca_height)
199        b |= 0x4;
200    if(yo - y >= 0 && yo - y < (int)_caca_height)
201        b |= 0x8;
202
203    if((b & (0x1|0x4)) == (0x1|0x4))
204        caca_putchar(xo + x, yo + y, c);
205
206    if((b & (0x2|0x4)) == (0x2|0x4))
207        caca_putchar(xo - x, yo + y, c);
208
209    if((b & (0x1|0x8)) == (0x1|0x8))
210        caca_putchar(xo + x, yo - y, c);
211
212    if((b & (0x2|0x8)) == (0x2|0x8))
213        caca_putchar(xo - x, yo - y, c);
214}
215
Note: See TracBrowser for help on using the repository browser.