libmonochrome/src/ML_ellipse.c

60 lines
2.0 KiB
C

/* ************************************************************************** */
/* */
/* ML_ellipse.c */
/* | Project : libmonochrome */
/* */
/* By: Pierre "PierrotLL" Le Gall <legallpierre89@gmail.com> */
/* Last updated: 2011/11/22 */
/* */
/* ************************************************************************** */
#include <monochrome/internals.h>
void ML_ellipse(int x, int y, int radius1, int radius2, ML_Color color)
{
int plot_x, plot_y;
float d1, d2;
if(radius1 < 1 || radius2 < 1) return;
plot_x = 0;
plot_y = radius2;
d1 = radius2*radius2 - radius1*radius1*radius2 + radius1*radius1/4;
ML_pixel(x, y+plot_y, color);
ML_pixel(x, y-plot_y, color);
while(radius1*radius1*(plot_y-.5) > radius2*radius2*(plot_x+1))
{
if(d1 < 0)
{
d1 += radius2*radius2*(2*plot_x+3);
plot_x++;
} else {
d1 += radius2*radius2*(2*plot_x+3) + radius1*radius1*(-2*plot_y+2);
plot_x++;
plot_y--;
}
ML_pixel(x+plot_x, y+plot_y, color);
ML_pixel(x-plot_x, y+plot_y, color);
ML_pixel(x+plot_x, y-plot_y, color);
ML_pixel(x-plot_x, y-plot_y, color);
}
d2 = radius2*radius2*(plot_x+.5)*(plot_x+.5) + radius1*radius1*(plot_y-1)*(plot_y-1) - radius1*radius1*radius2*radius2;
while(plot_y > 0)
{
if(d2 < 0)
{
d2 += radius2*radius2*(2*plot_x+2) + radius1*radius1*(-2*plot_y+3);
plot_y--;
plot_x++;
} else {
d2 += radius1*radius1*(-2*plot_y+3);
plot_y--;
}
ML_pixel(x+plot_x, y+plot_y, color);
ML_pixel(x-plot_x, y+plot_y, color);
if(plot_y > 0)
{
ML_pixel(x+plot_x, y-plot_y, color);
ML_pixel(x-plot_x, y-plot_y, color);
}
}
}