source: ttyvaders/trunk/libee/conic.c @ 136

Last change on this file since 136 was 136, checked in by Sam Hocevar, 17 years ago
  • libee/sprite.c: + Added sanity checks in ee_sprite_* functions.
  • libee/conic.c: + Use ellipsepoints() in ee_draw_circle().
  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1/*
2 *   libee         ASCII-Art library
3 *   Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: conic.c 136 2003-11-10 18:29:04Z sam $
7 *
8 *   This program is free software; you can redistribute it and/or modify
9 *   it under the terms of the GNU General Public License as published by
10 *   the Free Software Foundation; either version 2 of the License, or
11 *   (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include "config.h"
24
25#include <stdlib.h>
26
27#include "ee.h"
28
29static void ellipsepoints(int, int, int, int, char);
30
31void ee_draw_circle(int x, int y, int r, char c)
32{
33    int test, dx, dy;
34
35    /* Optimized Bresenham. Kick ass. */
36    for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
37    {
38        ellipsepoints(x, y, dx, dy, c);
39        ellipsepoints(x, y, dy, dx, c);
40
41        test += test > 0 ? dx - dy-- : dx;
42    }
43}
44
45void ee_draw_ellipse(int xo, int yo, int a, int b, char c)
46{
47    int d2;
48    int x = 0;
49    int y = b;
50    int d1 = b*b - (a*a*b) + (a*a/4);
51
52    ellipsepoints(xo, yo, x, y, c);
53
54    while( a*a*y - a*a/2 > b*b*(x+1))
55    {
56        if(d1 < 0)
57        {
58            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
59        }
60        else
61        {
62            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
63            y--;
64        }
65        x++;
66        ellipsepoints(xo, yo, x, y, c);
67    }
68
69    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
70    while(y > 0)
71    {
72        if(d2 < 0)
73        {
74            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
75            x++;
76        }
77        else
78        {
79            d2 += a*a*(-2*y+3);
80        }
81
82        y--;
83        ellipsepoints(xo, yo, x, y, c);
84    }
85}
86
87static void ellipsepoints(int xo, int yo, int x, int y, char c)
88{
89    ee_putcharTO(xo + x, yo + y, c);
90    ee_putcharTO(xo - x, yo + y, c);
91    ee_putcharTO(xo + x, yo - y, c);
92    ee_putcharTO(xo - x, yo - y, c);
93}
94
Note: See TracBrowser for help on using the repository browser.