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

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