中点画椭圆

来源:互联网 发布:2016十大网络用语作文 编辑:程序博客网 时间:2024/05/16 00:34

void drawPix(CDC *pDC, int CentX, int CentY, int x, int y, long color){pDC->SetPixel(CentX+x, CentY+y, color);pDC->SetPixel(CentX+x, CentY-y, color);pDC->SetPixel(CentX-x, CentY+y, color);pDC->SetPixel(CentX-x, CentY-y, color);}void ellipse(CDC *pDC, int CentX, int CentY, int a, int b, long color){int a2 = a * a;int b2 = b * b;int x = 0;int y = b;int d = b2 + a2 * (0.25 - b);drawPix(pDC, CentX, CentY, x, y, color);//上半部分    while(b2 * (x + 1) < a2 * (y - 0.5)){if(d >= 0){d += a2 * (2 - 2 * y);y--;}x++;d += b2 * (3 + 2 * x);drawPix(pDC, CentX, CentY, x, y, color);}//下半部分d = b2 * (x + 0.5) * (x + 0.5) + a2 * (y - 1) * (y - 1) - a2 * b2;while(y > 0){if(d < 0){d += b2 * (2 + 2 * x);x++;}d += a2 * (3 - 2 * y);y--;drawPix(pDC, CentX, CentY, x, y, color);}}void CEllipseView::OnDraw(CDC* pDC){CEllipseDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);ellipse(pDC, 300, 300, 200, 100, RGB(255, 0, 0));ellipse(pDC, 300, 300, 100, 200, RGB(0, 255, 0));// TODO: add draw code for native data here}


精度要求不高的情况下,可作如下化简:

void ellipse(CDC *pDC, int CentX, int CentY, int a, int b, long color){int a2 = a * a;int b2 = b * b;int x = 0;int y = b;int d = b2 + a2 * (0.25 - b) ;drawPix(pDC, CentX, CentY, x, y, color);//上半部分    while(b2 * (x + 1) < a2 * (y - 0.5)){if(d >= 0){d += a2 * (1 - y);y--;}x++;d += b2 * (1 + x);drawPix(pDC, CentX, CentY, x, y, color);}//下半部分d = b2 * (x + 0.5) * (x + 0.5) + a2 * (y - 1) * (y - 1) - a2 * b2;while(y > 0){if(d < 0){d += b2 * (1 + x);x++;}d += a2 * (1 - y);y--;drawPix(pDC, CentX, CentY, x, y, color);}}


0 0
原创粉丝点击