Qing Dujun
Address: http://blog.csdn.net/qingdujun/article/details/40042591
This article uses a complete example to demonstrate the scanning and conversion of circles.
1) Create CP2 class
Header file: p2.h
// P2.h: interface for the CP2 class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_P2_H__709052D1_45DA_4DF0_B5F2_15AC2B45687A__INCLUDED_)#define AFX_P2_H__709052D1_45DA_4DF0_B5F2_15AC2B45687A__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000//二维点类class CP2 {public:CP2();CP2(double x,double y);virtual ~CP2();public: //方便访问,直接定义为共有double x;double y;};#endif // !defined(AFX_P2_H__709052D1_45DA_4DF0_B5F2_15AC2B45687A__INCLUDED_)
Implementation file: cp2.cpp
// P2.cpp: implementation of the CP2 class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "DrawCircle.h"#include "P2.h"#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CP2::CP2(){}CP2::~CP2(){}
2) create a ccircle class
Header file circle. h
// Circle.h: interface for the CCircle class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_CIRCLE_H__AFAF8AB9_D976_4D82_8750_DB3E7F64F45C__INCLUDED_)#define AFX_CIRCLE_H__AFAF8AB9_D976_4D82_8750_DB3E7F64F45C__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include "P2.h"class CCircle {public:CCircle();virtual ~CCircle();void OneEight(double R,CDC *pDC); //绘制1/8圆void SymmetryFill(double x, double y,CDC *pDC); //绘制,同时根据对称性填充其他的7/8private:CP2 Center; //圆心};#endif // !defined(AFX_CIRCLE_H__AFAF8AB9_D976_4D82_8750_DB3E7F64F45C__INCLUDED_)
Implementation file: circle. cpp
// Circle.cpp: implementation of the CCircle class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "DrawCircle.h"#include "Circle.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//////////////////////////////////////////////////////////////////////CCircle::CCircle(){//初始化圆心坐标Center.x = 200;Center.y = 200;}CCircle::~CCircle(){}void CCircle::OneEight(double R,CDC *pDC) //绘制1/8圆{double x,y,d,xMax; d=1.25-R; //第一个中点是(1,R-0.5),代入隐函数得d(R为半径)x=0; y=R; //从像素点(0,R)开始填充(圆最上方顶点处)xMax = R*cos(3.14/4); // x属于(0 ,R*(sqrt(2)/2) )范围,即cos(45°)for(x=0;x<=xMax;x++) //(主方向为x){SymmetryFill(x,y,pDC);//绘制,同时根据对称性填充其他的7/8if (d<0)d=d+2*x+3; //当d(i)<0时,递推公式d(i+1)=d(i)+2x(i)+3else{d=d+2*(x-y)+5;//递推公式d(i+1)=d(i)+2[x(i)-y(i)]+5y--;} }}void CCircle::SymmetryFill(double x, double y,CDC *pDC) //绘制,同时根据对称性填充其他的7/8{ //定义圆的边界颜色 COLORREF clr=RGB(255,0,0);//绘制,填充该点pDC->SetPixelV(Round(x+Center.x),Round(y+Center.y),clr); //x,y//同时根据对称性填充其他的7/8像素点pDC->SetPixelV(Round(y+Center.x),Round(x+Center.y),clr); //y,xpDC->SetPixelV(Round(y+Center.x),Round(-x+Center.y),clr); //y,-xpDC->SetPixelV(Round(x+Center.x),Round(-y+Center.y),clr); //x,-ypDC->SetPixelV(Round(-x+Center.x),Round(-y+Center.y),clr);//-x,-ypDC->SetPixelV(Round(-y+Center.x),Round(-x+Center.y),clr);//-y,-xpDC->SetPixelV(Round(-y+Center.x),Round(x+Center.y),clr); //-y,xpDC->SetPixelV(Round(-x+Center.x),Round(y+Center.y),clr); //-x,y}
3) ondraw Function
void CDrawCircleView::OnDraw(CDC* pDC){CDrawCircleDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data hereCCircle *pCircle = new CCircle;pCircle->OneEight(100,pDC);}
4) Running Effect
Address: http://blog.csdn.net/qingdujun/article/details/40042591
References: tutorial on basic computer graphics (Visual C ++) (2nd) by Kong lingde
Scanning and conversion of computer graphics circles (2)