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

Last change on this file since 1148 was 982, checked in by Sam Hocevar, 15 years ago
  • Have cucul_utf8_to_utf32 write how many bytes it read.
  • Use that in the SLang driver so that it supports UTF-8 input.
  • Property svn:keywords set to Id
File size: 6.4 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 982 2006-05-25 20:01:10Z 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 str UTF-8 string representing the character that should be used
40 *         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, char const *str)
44{
45    int test, dx, dy;
46    uint32_t ch = cucul_utf8_to_utf32(str, NULL);
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 str UTF-8 string representing the character that should be used
70 *         to fill the ellipse.
71 *  \return This function always returns 0.
72 */
73int cucul_fill_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b,
74                       char const *str)
75{
76    int d2;
77    int x = 0;
78    int y = b;
79    int d1 = b*b - (a*a*b) + (a*a/4);
80
81    while(a*a*y - a*a/2 > b*b*(x+1))
82    {
83        if(d1 < 0)
84        {
85            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
86        }
87        else
88        {
89            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
90            cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, str);
91            cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, str);
92            y--;
93        }
94        x++;
95    }
96
97    cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, str);
98    cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, str);
99
100    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
101    while(y > 0)
102    {
103        if(d2 < 0)
104        {
105            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
106            x++;
107        }
108        else
109        {
110            d2 += a*a*(-2*y+3);
111        }
112
113        y--;
114        cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, str);
115        cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, str);
116    }
117
118    return 0;
119}
120
121/** \brief Draw an ellipse on the canvas using the given character.
122 *
123 *  This function never fails.
124 *
125 *  \param cv The handle to the libcucul canvas.
126 *  \param xo Center X coordinate.
127 *  \param yo Center Y coordinate.
128 *  \param a Ellipse X radius.
129 *  \param b Ellipse Y radius.
130 *  \param str UTF-8 string representing the character that should be used
131 *         to draw the ellipse outline.
132 *  \return This function always returns 0.
133 */
134int cucul_draw_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b,
135                       char const *str)
136{
137    int d2;
138    int x = 0;
139    int y = b;
140    int d1 = b*b - (a*a*b) + (a*a/4);
141    uint32_t ch = cucul_utf8_to_utf32(str, NULL);
142
143    ellipsepoints(cv, xo, yo, x, y, ch);
144
145    while(a*a*y - a*a/2 > b*b*(x+1))
146    {
147        if(d1 < 0)
148        {
149            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
150        }
151        else
152        {
153            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
154            y--;
155        }
156        x++;
157        ellipsepoints(cv, xo, yo, x, y, ch);
158    }
159
160    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
161    while(y > 0)
162    {
163        if(d2 < 0)
164        {
165            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
166            x++;
167        }
168        else
169        {
170            d2 += a*a*(-2*y+3);
171        }
172
173        y--;
174        ellipsepoints(cv, xo, yo, x, y, ch);
175    }
176
177    return 0;
178}
179
180/** \brief Draw a thin ellipse on the canvas.
181 *
182 *  This function never fails.
183 *
184 *  \param cv The handle to the libcucul canvas.
185 *  \param xo Center X coordinate.
186 *  \param yo Center Y coordinate.
187 *  \param a Ellipse X radius.
188 *  \param b Ellipse Y radius.
189 *  \return This function always returns 0.
190 */
191int cucul_draw_thin_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b)
192{
193    /* FIXME: this is not correct */
194    int d2;
195    int x = 0;
196    int y = b;
197    int d1 = b*b - (a*a*b) + (a*a/4);
198
199    ellipsepoints(cv, xo, yo, x, y, '-');
200
201    while(a*a*y - a*a/2 > b*b*(x+1))
202    {
203        if(d1 < 0)
204        {
205            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
206        }
207        else
208        {
209            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
210            y--;
211        }
212        x++;
213        ellipsepoints(cv, xo, yo, x, y, '-');
214    }
215
216    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
217    while(y > 0)
218    {
219        if(d2 < 0)
220        {
221            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
222            x++;
223        }
224        else
225        {
226            d2 += a*a*(-2*y+3);
227        }
228
229        y--;
230        ellipsepoints(cv, xo, yo, x, y, '|');
231    }
232
233    return 0;
234}
235
236static void ellipsepoints(cucul_canvas_t *cv, int xo, int yo, int x, int y,
237                          uint32_t ch)
238{
239    uint8_t b = 0;
240
241    if(xo + x >= 0 && xo + x < (int)cv->width)
242        b |= 0x1;
243    if(xo - x >= 0 && xo - x < (int)cv->width)
244        b |= 0x2;
245    if(yo + y >= 0 && yo + y < (int)cv->height)
246        b |= 0x4;
247    if(yo - y >= 0 && yo - y < (int)cv->height)
248        b |= 0x8;
249
250    if((b & (0x1|0x4)) == (0x1|0x4))
251        cucul_putchar(cv, xo + x, yo + y, ch);
252
253    if((b & (0x2|0x4)) == (0x2|0x4))
254        cucul_putchar(cv, xo - x, yo + y, ch);
255
256    if((b & (0x1|0x8)) == (0x1|0x8))
257        cucul_putchar(cv, xo + x, yo - y, ch);
258
259    if((b & (0x2|0x8)) == (0x2|0x8))
260        cucul_putchar(cv, xo - x, yo - y, ch);
261}
262
Note: See TracBrowser for help on using the repository browser.