代码实现:
中心点画圆法
//画圆函数实现
void CCGDrawingView::MidpointCircle(int x1, int y1, int R, CDC* pDC)
{
int x, y;
double d;
x = 0; y = R; d = 1 - R;
pDC->SetPixel(x1 + x, y1 + y, RGB(255, 0, 0));
//绘制对称点
pDC->SetPixel(x1 + x, y1 - y, RGB(255, 0, 0));
while (x < y) {
if (d < 0) {
d += 2 * x + 3;
x++;
}
else {
d += 2 * (x - y) + 5;
x++;
y--;
}
//绘制对称点
pDC->SetPixel(x1 + x, y1 + y, RGB(255, 0, 0));
pDC->SetPixel(x1 + y, y1 + x, RGB(255, 0, 0));
pDC->SetPixel(x1 + y, y1 - x, RGB(255, 0, 0));
pDC->SetPixel(x1 + x, y1 - y, RGB(255, 0, 0));
pDC->SetPixel(x1 - x, y1 - y, RGB(255, 0, 0));
pDC->SetPixel(x1 - y, y1 - x, RGB(255, 0, 0));
pDC->SetPixel(x1 - y, y1 + x, RGB(255, 0, 0));
pDC->SetPixel(x1 - x, y1 + y, RGB(255, 0, 0));
}
}
Bresenham画圆法
void CCGDrawingView::BresenhamCircle(int x1, int y1, int R, CDC* pDC) {
int x=0, y, p;
y = 0;
y = R;
p = 3 - 2 * R;
for (; x <= y; x++) {
pDC->SetPixel( x, y, RGB(255, 255, 255));
if (p >= 0) {
p += 4 * (x - y) + 10;
y--;
}
else {
p += 4 * x + 6;
}
//绘制对称点
pDC->SetPixel(x1 + x, y1 + y, RGB(255, 255, 255));
pDC->SetPixel(x1 + y, y1 + x, RGB(255, 255, 255));
pDC->SetPixel(x1 + y, y1 - x, RGB(255, 255, 255));
pDC->SetPixel(x1 + x, y1 - y, RGB(255, 255, 255));
pDC->SetPixel(x1 - x, y1 - y, RGB(255, 255, 255));
pDC->SetPixel(x1 - y, y1 - x, RGB(255, 255, 255));
pDC->SetPixel(x1 - y, y1 + x, RGB(255, 255, 255));
pDC->SetPixel(x1 - x, y1 + y, RGB(255, 255, 255));
}
}