source: libcaca/trunk/cucul/conic.c @ 769

Last change on this file since 769 was 769, checked in by Sam Hocevar, 14 years ago
  • Removed \file directives from all files except caca.h and cucul.h, to remove redundencies in the Doxygen documentation.
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: conic.c 769 2006-04-14 07:30:53Z sam $
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 *  This file contains ellipse and circle drawing functions, both filled
16 *  and outline.
17 */
18
19#include "config.h"
20
21#if !defined(__KERNEL__)
22#   include <stdlib.h>
23#endif
24
25#include "cucul.h"
26#include "cucul_internals.h"
27
28static void ellipsepoints(cucul_t *, int, int, int, int, uint32_t);
29
30/**
31 * \brief Draw a circle on the screen using the given character.
32 *
33 * \param x Center X coordinate.
34 * \param y Center Y coordinate.
35 * \param r Circle radius.
36 * \param c Character to draw the circle outline with.
37 * \return void
38 */
39void cucul_draw_circle(cucul_t *qq, int x, int y, int r, char const *str)
40{
41    int test, dx, dy;
42    uint32_t c = _cucul_utf8_to_utf32(str);
43
44    /* Optimized Bresenham. Kick ass. */
45    for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
46    {
47        ellipsepoints(qq, x, y, dx, dy, c);
48        ellipsepoints(qq, x, y, dy, dx, c);
49
50        test += test > 0 ? dx - dy-- : dx;
51    }
52}
53
54/**
55 * \brief Fill an ellipse on the screen using the given character.
56 *
57 * \param xo Center X coordinate.
58 * \param yo Center Y coordinate.
59 * \param a Ellipse X radius.
60 * \param b Ellipse Y radius.
61 * \param c Character to fill the ellipse with.
62 * \return void
63 */
64void cucul_fill_ellipse(cucul_t *qq, int xo, int yo, int a, int b,
65                        char const *str)
66{
67    int d2;
68    int x = 0;
69    int y = b;
70    int d1 = b*b - (a*a*b) + (a*a/4);
71
72    while(a*a*y - a*a/2 > b*b*(x+1))
73    {
74        if(d1 < 0)
75        {
76            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
77        }
78        else
79        {
80            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
81            cucul_draw_line(qq, xo - x, yo - y, xo + x, yo - y, str);
82            cucul_draw_line(qq, xo - x, yo + y, xo + x, yo + y, str);
83            y--;
84        }
85        x++;
86    }
87
88    cucul_draw_line(qq, xo - x, yo - y, xo + x, yo - y, str);
89    cucul_draw_line(qq, xo - x, yo + y, xo + x, yo + y, str);
90
91    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
92    while(y > 0)
93    {
94        if(d2 < 0)
95        {
96            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
97            x++;
98        }
99        else
100        {
101            d2 += a*a*(-2*y+3);
102        }
103
104        y--;
105        cucul_draw_line(qq, xo - x, yo - y, xo + x, yo - y, str);
106        cucul_draw_line(qq, xo - x, yo + y, xo + x, yo + y, str);
107    }
108}
109
110/**
111 * \brief Draw an ellipse on the screen using the given character.
112 *
113 * \param xo Center X coordinate.
114 * \param yo Center Y coordinate.
115 * \param a Ellipse X radius.
116 * \param b Ellipse Y radius.
117 * \param c Character to draw the ellipse outline with.
118 * \return void
119 */
120void cucul_draw_ellipse(cucul_t *qq, int xo, int yo, int a, int b,
121                        char const *str)
122{
123    int d2;
124    int x = 0;
125    int y = b;
126    int d1 = b*b - (a*a*b) + (a*a/4);
127    uint32_t c = _cucul_utf8_to_utf32(str);
128
129    ellipsepoints(qq, xo, yo, x, y, c);
130
131    while(a*a*y - a*a/2 > b*b*(x+1))
132    {
133        if(d1 < 0)
134        {
135            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
136        }
137        else
138        {
139            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
140            y--;
141        }
142        x++;
143        ellipsepoints(qq, xo, yo, x, y, c);
144    }
145
146    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
147    while(y > 0)
148    {
149        if(d2 < 0)
150        {
151            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
152            x++;
153        }
154        else
155        {
156            d2 += a*a*(-2*y+3);
157        }
158
159        y--;
160        ellipsepoints(qq, xo, yo, x, y, c);
161    }
162}
163
164/**
165 * \brief Draw a thin ellipse on the screen.
166 *
167 * \param xo Center X coordinate.
168 * \param yo Center Y coordinate.
169 * \param a Ellipse X radius.
170 * \param b Ellipse Y radius.
171 * \return void
172 */
173void cucul_draw_thin_ellipse(cucul_t *qq, int xo, int yo, int a, int b)
174{
175    /* FIXME: this is not correct */
176    int d2;
177    int x = 0;
178    int y = b;
179    int d1 = b*b - (a*a*b) + (a*a/4);
180
181    ellipsepoints(qq, xo, yo, x, y, '-');
182
183    while(a*a*y - a*a/2 > b*b*(x+1))
184    {
185        if(d1 < 0)
186        {
187            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
188        }
189        else
190        {
191            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
192            y--;
193        }
194        x++;
195        ellipsepoints(qq, xo, yo, x, y, '-');
196    }
197
198    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
199    while(y > 0)
200    {
201        if(d2 < 0)
202        {
203            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
204            x++;
205        }
206        else
207        {
208            d2 += a*a*(-2*y+3);
209        }
210
211        y--;
212        ellipsepoints(qq, xo, yo, x, y, '|');
213    }
214}
215
216static void ellipsepoints(cucul_t *qq, int xo, int yo, int x, int y, uint32_t c)
217{
218    uint8_t b = 0;
219
220    if(xo + x >= 0 && xo + x < (int)qq->width)
221        b |= 0x1;
222    if(xo - x >= 0 && xo - x < (int)qq->width)
223        b |= 0x2;
224    if(yo + y >= 0 && yo + y < (int)qq->height)
225        b |= 0x4;
226    if(yo - y >= 0 && yo - y < (int)qq->height)
227        b |= 0x8;
228
229    if((b & (0x1|0x4)) == (0x1|0x4))
230        _cucul_putchar32(qq, xo + x, yo + y, c);
231
232    if((b & (0x2|0x4)) == (0x2|0x4))
233        _cucul_putchar32(qq, xo - x, yo + y, c);
234
235    if((b & (0x1|0x8)) == (0x1|0x8))
236        _cucul_putchar32(qq, xo + x, yo - y, c);
237
238    if((b & (0x2|0x8)) == (0x2|0x8))
239        _cucul_putchar32(qq, xo - x, yo - y, c);
240}
241
Note: See TracBrowser for help on using the repository browser.