標籤:des style blog http color io ar for strong
卿篤軍
原文地址:http://blog.csdn.net/qingdujun/article/details/40045907
本文通過一個完整的執行個體,示範橢圓的掃描轉換。
1)建立CEllipse類
標頭檔: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_)實現檔案: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函數
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)運行效果
原文地址:http://blog.csdn.net/qingdujun/article/details/40045907
參考文獻:電腦圖形學基礎教程(Visual C++版)(第2版) 孔令德 編著
電腦圖形學 橢圓的掃描轉換(3)