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

Last change on this file since 568 was 568, checked in by Sam Hocevar, 14 years ago
  • Added glue code to compile libcaca without a libc and build applications as multiboot kernels.
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1/*
2 *  libcucul      Unicode canvas library
3 *  Copyright (c) 2002-2006 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 Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
10 */
11
12/** \file conic.c
13 *  \version \$Id: conic.c 568 2006-03-09 12:47:37Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief Ellipse and circle drawing
16 *
17 *  This file contains ellipse and circle drawing functions, both filled
18 *  and outline.
19 */
20
21#include "config.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_t *, int, int, int, int, char);
31
32/**
33 * \brief Draw a circle on the screen using the given character.
34 *
35 * \param x Center X coordinate.
36 * \param y Center Y coordinate.
37 * \param r Circle radius.
38 * \param c Character to draw the circle outline with.
39 * \return void
40 */
41void cucul_draw_circle(cucul_t *qq, int x, int y, int r, char c)
42{
43    int test, dx, dy;
44
45    /* Optimized Bresenham. Kick ass. */
46    for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
47    {
48        ellipsepoints(qq, x, y, dx, dy, c);
49        ellipsepoints(qq, x, y, dy, dx, c);
50
51        test += test > 0 ? dx - dy-- : dx;
52    }
53}
54
55/**
56 * \brief Fill an ellipse on the screen using the given character.
57 *
58 * \param xo Center X coordinate.
59 * \param yo Center Y coordinate.
60 * \param a Ellipse X radius.
61 * \param b Ellipse Y radius.
62 * \param c Character to fill the ellipse with.
63 * \return void
64 */
65void cucul_fill_ellipse(cucul_t *qq, int xo, int yo, int a, int b, char c)
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, c);
82            cucul_draw_line(qq, xo - x, yo + y, xo + x, yo + y, c);
83            y--;
84        }
85        x++;
86    }
87
88    cucul_draw_line(qq, xo - x, yo - y, xo + x, yo - y, c);
89    cucul_draw_line(qq, xo - x, yo + y, xo + x, yo + y, c);
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, c);
106        cucul_draw_line(qq, xo - x, yo + y, xo + x, yo + y, c);
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, char c)
121{
122    int d2;
123    int x = 0;
124    int y = b;
125    int d1 = b*b - (a*a*b) + (a*a/4);
126
127    ellipsepoints(qq, xo, yo, x, y, c);
128
129    while(a*a*y - a*a/2 > b*b*(x+1))
130    {
131        if(d1 < 0)
132        {
133            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
134        }
135        else
136        {
137            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
138            y--;
139        }
140        x++;
141        ellipsepoints(qq, xo, yo, x, y, c);
142    }
143
144    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
145    while(y > 0)
146    {
147        if(d2 < 0)
148        {
149            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
150            x++;
151        }
152        else
153        {
154            d2 += a*a*(-2*y+3);
155        }
156
157        y--;
158        ellipsepoints(qq, xo, yo, x, y, c);
159    }
160}
161
162/**
163 * \brief Draw a thin ellipse on the screen.
164 *
165 * \param xo Center X coordinate.
166 * \param yo Center Y coordinate.
167 * \param a Ellipse X radius.
168 * \param b Ellipse Y radius.
169 * \return void
170 */
171void cucul_draw_thin_ellipse(cucul_t *qq, int xo, int yo, int a, int b)
172{
173    /* FIXME: this is not correct */
174    int d2;
175    int x = 0;
176    int y = b;
177    int d1 = b*b - (a*a*b) + (a*a/4);
178
179    ellipsepoints(qq, xo, yo, x, y, '-');
180
181    while(a*a*y - a*a/2 > b*b*(x+1))
182    {
183        if(d1 < 0)
184        {
185            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
186        }
187        else
188        {
189            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
190            y--;
191        }
192        x++;
193        ellipsepoints(qq, xo, yo, x, y, '-');
194    }
195
196    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
197    while(y > 0)
198    {
199        if(d2 < 0)
200        {
201            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
202            x++;
203        }
204        else
205        {
206            d2 += a*a*(-2*y+3);
207        }
208
209        y--;
210        ellipsepoints(qq, xo, yo, x, y, '|');
211    }
212}
213
214static void ellipsepoints(cucul_t *qq, int xo, int yo, int x, int y, char c)
215{
216    uint8_t b = 0;
217
218    if(xo + x >= 0 && xo + x < (int)qq->width)
219        b |= 0x1;
220    if(xo - x >= 0 && xo - x < (int)qq->width)
221        b |= 0x2;
222    if(yo + y >= 0 && yo + y < (int)qq->height)
223        b |= 0x4;
224    if(yo - y >= 0 && yo - y < (int)qq->height)
225        b |= 0x8;
226
227    if((b & (0x1|0x4)) == (0x1|0x4))
228        cucul_putchar(qq, xo + x, yo + y, c);
229
230    if((b & (0x2|0x4)) == (0x2|0x4))
231        cucul_putchar(qq, xo - x, yo + y, c);
232
233    if((b & (0x1|0x8)) == (0x1|0x8))
234        cucul_putchar(qq, xo + x, yo - y, c);
235
236    if((b & (0x2|0x8)) == (0x2|0x8))
237        cucul_putchar(qq, xo - x, yo - y, c);
238}
239
Note: See TracBrowser for help on using the repository browser.