電腦圖形學 橢圓的掃描轉換(3)

來源:互聯網
上載者:User

標籤: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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.