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

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   for   

卿篤軍

原文地址:http://blog.csdn.net/qingdujun/article/details/40042591


本文通過一個完整的執行個體,示範圓的掃描轉換。

1)建立CP2類

標頭檔: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_)
實現檔案: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)建立CCircle類

標頭檔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_)
實現檔案: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函數

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)運行效果



原文地址:http://blog.csdn.net/qingdujun/article/details/40042591

參考文獻:電腦圖形學基礎教程(Visual C++版)(第2版) 孔令德 編著

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.