標籤:des style blog http color io ar for strong
卿篤軍
原文地址:http://blog.csdn.net/qingdujun/article/details/40025917
本文示範,通過自己編寫繪製直線函數(像素點填充),繪製任意斜率的直線。
1)建立CP2類
標頭檔:p2.h
// P2.h: interface for the CP2 class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_P2_H__85E77AD6_0704_4E12_8E74_5BE1744B3154__INCLUDED_)#define AFX_P2_H__85E77AD6_0704_4E12_8E74_5BE1744B3154__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__85E77AD6_0704_4E12_8E74_5BE1744B3154__INCLUDED_)
實現檔案:p2.cpp
// P2.cpp: implementation of the CP2 class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "DrawLine.h"#include "P2.h"#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CP2::CP2(){x=0.0;y=0.0;}CP2::CP2(double x,double y){this->x=x;this->y=y;}CP2::~CP2(){}
2)建立CLine類
標頭檔:Line.h
// Line.h: interface for the CLine class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_LINE_H__8A91E283_F40D_4086_8BB8_7A9D9A6DB4D5__INCLUDED_)#define AFX_LINE_H__8A91E283_F40D_4086_8BB8_7A9D9A6DB4D5__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include "P2.h"class CLine {public:CLine();virtual ~CLine();void MoveTo(CP2 p0); //移動到指定位置void MoveTo(double x, double y);void LineTo(CP2 p1, CDC *pDC); //繪製直線,不含終點void LineTo(double x, double y, CDC *pDC);private:CP2 P0; //起點CP2 P1; //終點};#endif // !defined(AFX_LINE_H__8A91E283_F40D_4086_8BB8_7A9D9A6DB4D5__INCLUDED_)
實現檔案:Line.cpp
// Line.cpp: implementation of the CLine class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "DrawLine.h"#include "Line.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//////////////////////////////////////////////////////////////////////CLine::CLine(){}CLine::~CLine(){}void CLine::MoveTo(CP2 p0) //記錄直線起點函數{P0=p0;}void CLine::MoveTo(double x, double y){P0.x = x;P0.y = y;}void CLine::LineTo(double x, double y, CDC *pDC) //繪製{CP2 p;p.x = x;p.y = y;LineTo(p, pDC);}void CLine::LineTo(CP2 p1, CDC *pDC){P1=p1;CP2 p,t;COLORREF clr = RGB(0,0,0); //黑色像素點if(fabs(P0.x-P1.x)<1e-6) //繪製垂線{if(P0.y>P1.y) //交換頂點,使得起始點低於終點{t=P0;P0=P1;P1=t;}for(p=P0;p.y<P1.y;p.y++) //執行繪製,填充像素點{pDC->SetPixelV(Round(p.x),Round(p.y),clr);}}else{double k,d;k=(P1.y-P0.y)/(P1.x-P0.x); //斜率if(k>1.0) //繪製k>1(y為主方向){if(P0.y>P1.y){t=P0;P0=P1;P1=t;}d=1-0.5*k; //中點初始值for(p=P0;p.y<P1.y;p.y++){pDC->SetPixelV(Round(p.x),Round(p.y), clr); if(d>=0) //中點位於將繪製點上方,填充下方點{p.x++;d=d+1-k; //遞推公式:當d>=0時,d(i+1)=d(i)+1-k;}else d+=1; }}if(0.0<=k && k<=1.0) //繪製0<=k<=1(x為主方向){if(P0.x>P1.x){t=P0;P0=P1;P1=t;}d=0.5-k; //中點初始值for(p=P0;p.x<P1.x;p.x++){pDC->SetPixelV(Round(p.x),Round(p.y), clr); if(d<0) //中點位於將繪製點下方,填充上方點{p.y++;d=d+1-k; //遞推公式:當d<0時,d(i+1)=d(i)+1-k;}else d-=k;}}if(k>=-1.0 && k<0.0)//繪製-1<=k<0(x為主方向){if(P0.x>P1.x){t=P0;P0=P1;P1=t;}d=-0.5-k; //中點初始化 for(p=P0;p.x<P1.x;p.x++){pDC->SetPixelV(Round(p.x),Round(p.y), clr); if(d>0) //中點位於將繪製點上方,填充下方點{p.y--;d=d-1-k; //遞推公式:當d>0時,d(i+1)=d(i)-1-k;}else d-=k;}}if(k<-1.0)//繪製k<-1 (y為主方向){if(P0.y<P1.y){t=P0;P0=P1;P1=t;}d=-1-0.5*k; //中點初始化for(p=P0;p.y>P1.y;p.y--){pDC->SetPixelV(Round(p.x),Round(p.y), clr); if(d<0) //中點位於將繪製點下方,填充上方點{p.x++;d=d-1-k;//遞推公式:當d>0時,d(i+1)=d(i)-1-k;}else d-=1; }}}P0=p1; //將終點賦值給起點}
3)OnDraw函數
void CDrawLineView::OnDraw(CDC* pDC){CDrawLineDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data here//繪製直線CLine *line=new CLine;//動態建立直線繪製類對象line->MoveTo(100,100);line->LineTo(500,500, pDC);}
4)繪製效果如下
原文地址:http://blog.csdn.net/qingdujun/article/details/40025917
參考文獻:電腦圖形學基礎教程(Visual C++版)(第2版) 孔令德 編著
電腦圖形學 繪製任意斜率的直線(1)