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

Last change on this file since 1347 was 1347, checked in by Sam Hocevar, 14 years ago
  • Rename cucul_put*() into cucul_put_*(). Updated all code to reflect that.
  • Property svn:keywords set to Id
File size: 6.2 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 1347 2006-11-11 16:29:39Z 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#include "common.h"
21
22#if !defined(__KERNEL__)
23#   include <stdlib.h>
24#endif
25
26#include "cucul.h"
27#include "cucul_internals.h"
28
29static void ellipsepoints(cucul_canvas_t *, int, int, int, int, uint32_t);
30
31/** \brief Draw a circle on the canvas using the given character.
32 *
33 *  This function never fails.
34 *
35 *  \param cv The handle to the libcucul canvas.
36 *  \param x Center X coordinate.
37 *  \param y Center Y coordinate.
38 *  \param r Circle radius.
39 *  \param ch UTF-32 character to be used to draw the circle outline.
40 *  \return This function always returns 0.
41 */
42int cucul_draw_circle(cucul_canvas_t *cv, int x, int y, int r,
43                      unsigned long int ch)
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(cv, x, y, dx, dy, ch);
51        ellipsepoints(cv, x, y, dy, dx, ch);
52
53        test += test > 0 ? dx - dy-- : dx;
54    }
55
56    return 0;
57}
58
59/** \brief Fill an ellipse on the canvas using the given character.
60 *
61 *  This function never fails.
62 *
63 *  \param cv The handle to the libcucul canvas.
64 *  \param xo Center X coordinate.
65 *  \param yo Center Y coordinate.
66 *  \param a Ellipse X radius.
67 *  \param b Ellipse Y radius.
68 *  \param ch UTF-32 character to be used to fill the ellipse.
69 *  \return This function always returns 0.
70 */
71int cucul_fill_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b,
72                       unsigned long int ch)
73{
74    int d2;
75    int x = 0;
76    int y = b;
77    int d1 = b*b - (a*a*b) + (a*a/4);
78
79    while(a*a*y - a*a/2 > b*b*(x+1))
80    {
81        if(d1 < 0)
82        {
83            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
84        }
85        else
86        {
87            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
88            cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch);
89            cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch);
90            y--;
91        }
92        x++;
93    }
94
95    cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch);
96    cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch);
97
98    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
99    while(y > 0)
100    {
101        if(d2 < 0)
102        {
103            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
104            x++;
105        }
106        else
107        {
108            d2 += a*a*(-2*y+3);
109        }
110
111        y--;
112        cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch);
113        cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch);
114    }
115
116    return 0;
117}
118
119/** \brief Draw an ellipse on the canvas using the given character.
120 *
121 *  This function never fails.
122 *
123 *  \param cv The handle to the libcucul canvas.
124 *  \param xo Center X coordinate.
125 *  \param yo Center Y coordinate.
126 *  \param a Ellipse X radius.
127 *  \param b Ellipse Y radius.
128 *  \param ch UTF-32 character to be used to draw the ellipse outline.
129 *  \return This function always returns 0.
130 */
131int cucul_draw_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b,
132                       unsigned long int ch)
133{
134    int d2;
135    int x = 0;
136    int y = b;
137    int d1 = b*b - (a*a*b) + (a*a/4);
138
139    ellipsepoints(cv, xo, yo, x, y, ch);
140
141    while(a*a*y - a*a/2 > b*b*(x+1))
142    {
143        if(d1 < 0)
144        {
145            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
146        }
147        else
148        {
149            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
150            y--;
151        }
152        x++;
153        ellipsepoints(cv, xo, yo, x, y, ch);
154    }
155
156    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
157    while(y > 0)
158    {
159        if(d2 < 0)
160        {
161            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
162            x++;
163        }
164        else
165        {
166            d2 += a*a*(-2*y+3);
167        }
168
169        y--;
170        ellipsepoints(cv, xo, yo, x, y, ch);
171    }
172
173    return 0;
174}
175
176/** \brief Draw a thin ellipse on the canvas.
177 *
178 *  This function never fails.
179 *
180 *  \param cv The handle to the libcucul canvas.
181 *  \param xo Center X coordinate.
182 *  \param yo Center Y coordinate.
183 *  \param a Ellipse X radius.
184 *  \param b Ellipse Y radius.
185 *  \return This function always returns 0.
186 */
187int cucul_draw_thin_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b)
188{
189    /* FIXME: this is not correct */
190    int d2;
191    int x = 0;
192    int y = b;
193    int d1 = b*b - (a*a*b) + (a*a/4);
194
195    ellipsepoints(cv, xo, yo, x, y, '-');
196
197    while(a*a*y - a*a/2 > b*b*(x+1))
198    {
199        if(d1 < 0)
200        {
201            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
202        }
203        else
204        {
205            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
206            y--;
207        }
208        x++;
209        ellipsepoints(cv, xo, yo, x, y, '-');
210    }
211
212    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
213    while(y > 0)
214    {
215        if(d2 < 0)
216        {
217            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
218            x++;
219        }
220        else
221        {
222            d2 += a*a*(-2*y+3);
223        }
224
225        y--;
226        ellipsepoints(cv, xo, yo, x, y, '|');
227    }
228
229    return 0;
230}
231
232static void ellipsepoints(cucul_canvas_t *cv, int xo, int yo, int x, int y,
233                          uint32_t ch)
234{
235    uint8_t b = 0;
236
237    if(xo + x >= 0 && xo + x < (int)cv->width)
238        b |= 0x1;
239    if(xo - x >= 0 && xo - x < (int)cv->width)
240        b |= 0x2;
241    if(yo + y >= 0 && yo + y < (int)cv->height)
242        b |= 0x4;
243    if(yo - y >= 0 && yo - y < (int)cv->height)
244        b |= 0x8;
245
246    if((b & (0x1|0x4)) == (0x1|0x4))
247        cucul_put_char(cv, xo + x, yo + y, ch);
248
249    if((b & (0x2|0x4)) == (0x2|0x4))
250        cucul_put_char(cv, xo - x, yo + y, ch);
251
252    if((b & (0x1|0x8)) == (0x1|0x8))
253        cucul_put_char(cv, xo + x, yo - y, ch);
254
255    if((b & (0x2|0x8)) == (0x2|0x8))
256        cucul_put_char(cv, xo - x, yo - y, ch);
257}
258
Note: See TracBrowser for help on using the repository browser.