用void MidPointEllipse(int x0,int y0,int x1,int y1,void (*SetPixel)(int x,int y))写中点画椭圆的代码
时间: 2023-10-13 19:05:27 浏览: 90
void MidPointEllipse(int x0, int y0, int x1, int y1, void (*SetPixel)(int x, int y)) {
int a = abs(x1 - x0);
int b = abs(y1 - y0);
int b2 = b * b;
int a2 = a * a;
int d = a2 + b2 * (-a + 1) / 4 + b2 / 4;
int x = 0;
int y = b;
while (a2 * (y - 0.5) > b2 * (x + 1)) {
if (d < 0) {
d += a2 * (2 * y + 3);
} else {
d += a2 * (2 * y + 3) + b2 * (-2 * x + 2);
x++;
}
y--;
SetPixel(x0 + x, y0 + y);
SetPixel(x0 + x, y0 - y);
SetPixel(x0 - x, y0 + y);
SetPixel(x0 - x, y0 - y);
}
d = a2 * (y * y - y) + b2 * (x * x + x - a2 * b2);
while (y >= 0) {
SetPixel(x0 + x, y0 + y);
SetPixel(x0 + x, y0 - y);
SetPixel(x0 - x, y0 + y);
SetPixel(x0 - x, y0 - y);
if (d < 0) {
d += a2 * (2 * y - 1) + b2 * (2 * x + 2);
x++;
} else {
d += b2 * (2 * x + 2);
}
y--;
}
}
阅读全文