Mail Archives: djgpp/1997/04/16/13:19:49
I was looking (not too hard) for ellipse algorithmus for long time,
finally found one and because there was question about it some time ago
I post this code. It's from jlib graphics library and it 100% works. I
post only one general version of it changed a bit to use Allegro
function names to make my post shorter. I think it can be of some
interest for Shawn too.
--> CUT HERE <--
/*
** Copyright (C) 1995 Jonathan Paul Griffiths. All rights reserved.
**
** You may do anything with this code you wish EXCEPT sell it. You
may sell
** any software you create using this code, but you MUST NOT charge
for
** the code itself. See the file "jlib.doc" for detailed terms.
*/
#include <jlib.h>
#define FAST_2_MUL(x) ((x)<<1)
#define FAST_3_MUL(x) (((x)<<1)+x)
#define FAST_4_DIV(x) ((x)>>2)
void buff_draw_ellipse(BITMAP * buff, int x0, int y0, int a, int b, int
c)
{
int x, y, y1, aa, bb, d, g, h;
aa = a * a;
bb = b * b;
h = (FAST_4_DIV (aa)) - b * aa + bb;
g = (FAST_4_DIV (9 * aa)) - (FAST_3_MUL (b * aa)) + bb;
x = 0;
y = b;
while (g < 0) {
putpixel(buff, x0 + x, y0 + y, c);
putpixel(buff, x0 - x, y0 + y, c);
putpixel(buff, x0 - x, y0 - y, c);
putpixel(buff, x0 + x, y0 - y, c);
if (h < 0) {
d = ((FAST_2_MUL (x)) + 3) * bb;
g += d;
}
else {
d = ((FAST_2_MUL (x)) + 3) * bb - FAST_2_MUL ((y - 1) * aa);
g += (d + (FAST_2_MUL (aa)));
--y;
}
h += d;
++x;
}
y1 = y;
h = (FAST_4_DIV (bb)) - a * bb + aa;
x = a;
y = 0;
while (y <= y1) {
putpixel(buff, x0 + x, y0 + y, c);
putpixel(buff, x0 - x, y0 + y, c);
putpixel(buff, x0 - x, y0 - y, c);
putpixel(buff, x0 + x, y0 - y, c);
if (h < 0) {
h += ((FAST_2_MUL (y)) + 3) * aa;
}
else {
h += (((FAST_2_MUL (y) + 3) * aa) - (FAST_2_MUL (x - 1) * bb));
--x;
}
++y;
}
}
--> CUT HERE <--
I hope someone find it as usefull as I did.
--
//\\ //\\ ..
// \\// \\
// \/ \\ || |/\/\ /---\
// \\ || | | | |___'
// \\ || | | | \___.
E-Mail: MiMe AT EUnet DOT cz
- Raw text -