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

Last change on this file since 138 was 138, checked in by Sam Hocevar, 17 years ago
  • libee/conic.c: Clip circles and ellipses.
  • Property svn:keywords set to Id
File size: 2.8 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 138 2003-11-10 20:43:02Z 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#include <inttypes.h>
27
28#include "ee.h"
29
30static void ellipsepoints(int, int, int, int, char);
31
32void ee_draw_circle(int x, int y, int r, char c)
33{
34    int test, dx, dy;
35
36    /* Optimized Bresenham. Kick ass. */
37    for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
38    {
39        ellipsepoints(x, y, dx, dy, c);
40        ellipsepoints(x, y, dy, dx, c);
41
42        test += test > 0 ? dx - dy-- : dx;
43    }
44}
45
46void ee_draw_ellipse(int xo, int yo, int a, int b, char c)
47{
48    int d2;
49    int x = 0;
50    int y = b;
51    int d1 = b*b - (a*a*b) + (a*a/4);
52
53    ellipsepoints(xo, yo, x, y, c);
54
55    while( a*a*y - a*a/2 > b*b*(x+1))
56    {
57        if(d1 < 0)
58        {
59            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
60        }
61        else
62        {
63            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
64            y--;
65        }
66        x++;
67        ellipsepoints(xo, yo, x, y, c);
68    }
69
70    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
71    while(y > 0)
72    {
73        if(d2 < 0)
74        {
75            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
76            x++;
77        }
78        else
79        {
80            d2 += a*a*(-2*y+3);
81        }
82
83        y--;
84        ellipsepoints(xo, yo, x, y, c);
85    }
86}
87
88static void ellipsepoints(int xo, int yo, int x, int y, char c)
89{
90    uint8_t b = 0;
91
92    if(xo + x >= 0 && xo + x < ee_get_width())
93        b |= 0x1;
94    if(xo - x >= 0 && xo - x < ee_get_width())
95        b |= 0x2;
96    if(yo + y >= 0 && yo + y < ee_get_height())
97        b |= 0x4;
98    if(yo - y >= 0 && yo - y < ee_get_height())
99        b |= 0x8;
100
101    if((b & (0x1|0x4)) == (0x1|0x4))
102    {
103        ee_goto(xo + x, yo + y);
104        ee_putchar(c);
105    }
106
107    if((b & (0x2|0x4)) == (0x2|0x4))
108    {
109        ee_goto(xo - x, yo + y);
110        ee_putchar(c);
111    }
112
113    if((b & (0x1|0x8)) == (0x1|0x8))
114    {
115        ee_goto(xo + x, yo - y);
116        ee_putchar(c);
117    }
118
119    if((b & (0x2|0x8)) == (0x2|0x8))
120    {
121        ee_goto(xo - x, yo - y);
122        ee_putchar(c);
123    }
124}
125
Note: See TracBrowser for help on using the repository browser.