Qing Dujun
Address: http://blog.csdn.net/qingdujun/article/details/40045907
This article uses a complete example to demonstrate the scanning and conversion of an ellipse.
1) create a cellipse class
Header file: ellipse. h
// Ellipse.h: interface for the CEllipse class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_ELLIPSE_H__DBDD57D1_3A14_4067_93E9_B40218C54601__INCLUDED_)#define AFX_ELLIPSE_H__DBDD57D1_3A14_4067_93E9_B40218C54601__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000class CEllipse {public:CEllipse();virtual ~CEllipse();void SymmetryFill(double x, double y,CDC *pDC); //绘制,同时根据对称性填充剩下的3/4区域void OneFour(CDC *pDC); //绘制1/4椭圆private:CPoint Center; //椭圆中点double a,b; //椭圆长半轴、短半轴};#endif // !defined(AFX_ELLIPSE_H__DBDD57D1_3A14_4067_93E9_B40218C54601__INCLUDED_)Implementation file: ellipse. cpp
// Ellipse.cpp: implementation of the CEllipse class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "DrawEllipse.h"#include "Ellipse.h"#include <math.h>#define Round(d) int(floor(d+0.5))//四舍五入宏定义#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CEllipse::CEllipse(){//设定椭圆的中点Center.x = 300;Center.y = 300;//初始化长短半轴a = 200.0;b = 100.0;}CEllipse::~CEllipse(){}void CEllipse::SymmetryFill(double x, double y,CDC *pDC) //绘制,同时根据对称性填充剩下的3/4区域{//定义椭圆的颜色COLORREF clr=RGB(0,0,255); //绘制pDC->SetPixelV(Round(x+Center.x),Round(y+Center.y),clr);//同时根据对称性填充剩下的3/4区域pDC->SetPixelV(Round(-x+Center.x),Round(y+Center.y),clr);pDC->SetPixelV(Round(x+Center.x),Round(-y+Center.y),clr);pDC->SetPixelV(Round(-x+Center.x),Round(-y+Center.y),clr);}void CEllipse::OneFour(CDC *pDC) //绘制1/4椭圆{double x,y,d1,d2;x=0;y=b; //从像素点(0,b)开始填充(椭圆最上方顶点处)d1=b*b+a*a*(-b+0.25); //中点误差项初始值d(10)=b^2+a^2*(-b+0.25)//椭圆AC弧段(x为主方向)while(b*b*(x+1)<a*a*(y-0.5)) //法矢量两分量相等处(b^2*(x+1)==a^2*(y-0.5)){if (d1<0){d1=d1+b*b*(2*x+3); //当d1(i)<0时,递推公式:d1(i+1)=d1(i)+b^2*( 2x(i)+3 )}else{d1=d1+b*b*(2*x+3)+a*a*(-2*y+2);//递推公式:d1(i+1)=d1(i)+b^2*( 2x(i)+3 )+a^2*( -2y(i)+2 )y--;}x++;SymmetryFill(x,y,pDC); //执行绘制}//中点误差项初始值d(10)=b^2+a^2*(y-1)^2-a^2*b^2d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;//椭圆CB弧段(y为主方向)while(y>0){if (d2<0) {d2=d2+b*b*(2*x+2)+a*a*(-2*y+3);//当d2(i)<0时,递推公式:d2(i+1)=d2(i)+b^2*( 2x(i)+2 )+a^2*(-2y+3)x++;}else{d2=d2+a*a*(-2*y+3); //递推公式:d2(i+1)=d2(i)+a^2*(-2y+3)}y--;SymmetryFill(x,y,pDC); //执行绘制}}2) ondraw Function
void CDrawEllipseView::OnDraw(CDC* pDC){CDrawEllipseDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data hereCEllipse *pEllipse = new CEllipse;pEllipse->OneFour(pDC);}3) Running Effect
Address: http://blog.csdn.net/qingdujun/article/details/40045907
References: tutorial on basic computer graphics (Visual C ++) (2nd) by Kong lingde
Scanning and conversion of computer graphics ovans (3)