電腦圖形學 彩色直線段的反走樣(5)

來源:互聯網
上載者:User

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

卿篤軍

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


本文通過一個完整的例子來示範彩色直線段的反走樣。

1)建立CP2類

標頭檔:P2.h

// P2.h: interface for the CP2 class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_P2_H__DD23884F_7F62_48E8_A906_65C4558DE4EB__INCLUDED_)#define AFX_P2_H__DD23884F_7F62_48E8_A906_65C4558DE4EB__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include "RGB.h"//二維點類  class CP2    {  public:      CP2();      CP2(double x,double y, CRGB c);     virtual ~CP2();  public:         //方便訪問,直接定義為共有      double x;      double y;  CRGB c;};    #endif // !defined(AFX_P2_H__DD23884F_7F62_48E8_A906_65C4558DE4EB__INCLUDED_)
實現檔案:P2.cpp

// P2.cpp: implementation of the CP2 class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "WuLine.h"#include "P2.h"#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CP2::CP2(){}CP2::CP2(double x,double y, CRGB c){this->x = x;this->y = y;this->c = c;}CP2::~CP2(){}
2)建立CRGB類

標頭檔:RGB.h

// RGB.h: interface for the CRGB class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_RGB_H__F163CE21_A4E9_4B66_9011_3B63D6E588D2__INCLUDED_)#define AFX_RGB_H__F163CE21_A4E9_4B66_9011_3B63D6E588D2__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000class CRGB  {public:CRGB();CRGB(double r,double g,double b);virtual ~CRGB();friend CRGB operator+(const CRGB &,const CRGB &);//運算子多載friend CRGB operator-(const CRGB &,const CRGB &);friend CRGB operator*(const CRGB &,const CRGB &);friend CRGB operator*(const CRGB &,double);friend CRGB operator*(double,const CRGB &);friend CRGB operator/(const CRGB &,double);public:public:double red;   //紅色分量double green; //綠色分量double blue;  //藍色分量};#endif // !defined(AFX_RGB_H__F163CE21_A4E9_4B66_9011_3B63D6E588D2__INCLUDED_)
實現檔案:RGB.cpp

// RGB.cpp: implementation of the CRGB class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "WuLine.h"#include "RGB.h"#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CRGB::CRGB(){red=1.0;green=1.0;blue=1.0;}CRGB::~CRGB(){}CRGB::CRGB(double r,double g,double b)//重載建構函式{red=r;green=g;blue=b;}CRGB operator +(const CRGB &c1,const CRGB &c2)//+運算子多載{CRGB c;c.red=c1.red+c2.red;c.green=c1.green+c2.green;c.blue=c1.blue+c2.blue;return c;}CRGB operator -(const CRGB &c1,const CRGB &c2)//-運算子多載{CRGB c;c.red=c1.red-c2.red;c.green=c1.green-c2.green;c.blue=c1.blue-c2.blue;return c;}CRGB operator *(const CRGB &c1,const CRGB &c2)//*運算子多載{CRGB c;c.red=c1.red*c2.red;c.green=c1.green*c2.green;c.blue=c1.blue*c2.blue;return c;}CRGB operator *(const CRGB &c1,double k)//*運算子多載{   CRGB c;   c.red=k*c1.red;   c.green=k*c1.green;   c.blue=k*c1.blue;   return c;}CRGB operator *(double k,const CRGB &c1)//*運算子多載{   CRGB c;   c.red=k*c1.red;   c.green=k*c1.green;   c.blue=k*c1.blue;   return c;}CRGB operator /(const CRGB &c1,double k)// /運算子多載{   CRGB c;   c.red=c1.red/k;   c.green=c1.green/k;   c.blue=c1.blue/k;   return c;}CRGB operator +=(CRGB &c1,CRGB &c2)//+=運算子多載{c1.red=c1.red+c2.red;c1.green=c1.green+c2.green;c1.blue=c1.blue+c2.blue;return c1;}CRGB operator -=(CRGB &c1,CRGB &c2)//-=運算子多載{c1.red=c1.red-c2.red;c1.green=c1.green-c2.green;c1.blue=c1.blue-c2.blue;return c1;}CRGB operator *=(CRGB &c1,CRGB &c2)//*=運算子多載{c1.red=c1.red*c2.red;c1.green=c1.green*c2.green;c1.blue=c1.blue*c2.blue;return c1;}CRGB operator /=(CRGB &c1,double k)///=運算子多載{c1.red=c1.red/k;c1.green=c1.green/k;c1.blue=c1.blue/k;return c1;}
3)建立CWuAnti類
標頭檔:WuAnti.h

// WuAnti.h: interface for the CWuAnti class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_WUANTI1_H__2C2D354F_C8AA_4F64_81CC_56195DEE5704__INCLUDED_)#define AFX_WUANTI1_H__2C2D354F_C8AA_4F64_81CC_56195DEE5704__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include "P2.h"class CWuAnti  {public:CWuAnti();virtual ~CWuAnti();  void MoveTo(double x, double y, CRGB c); //移動到指定位置,指定起點顏色  void MoveTo(CP2 p0);void LineTo(CDC *pDC, double x, double y, CRGB c); //繪製Wu反走樣直線,不含終點,指定終點顏色void LineTo(CDC *pDC, CP2 p1);private:      CP2 P0;      //起點      CP2 P1;      //終點};#endif // !defined(AFX_WUANTI1_H__2C2D354F_C8AA_4F64_81CC_56195DEE5704__INCLUDED_)
實現檔案:WuAnti.cpp

// WuAnti.cpp: implementation of the CWuAnti class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "WuLine.h"#include "WuAnti.h"#include "RGB.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//////////////////////////////////////////////////////////////////////CWuAnti::CWuAnti(){}CWuAnti::~CWuAnti(){}void CWuAnti::MoveTo(double x, double y, CRGB c)  //繪製彩色線條,指定起點顏色{      P0.x=x;P0.y=y;P0.c=c;}  void CWuAnti::MoveTo(CP2 p0)      //繪製彩色線條{  P0=p0;}  void CWuAnti::LineTo(CDC *pDC, double x, double y, CRGB c) //繪製,指定終點顏色  {  LineTo(pDC, CP2(x, y, c));   }  void CWuAnti::LineTo(CDC *pDC, CP2 p1){P1=p1;CP2 p,t;CRGB c0,c1;//COLORREF Cf,Cb = pDC->GetBkColor(); if(fabs(P0.x-P1.x)==0)//繪製垂線{if(P0.y>P1.y)//交換頂點,使得起始點低於終點頂點{t=P0;P0=P1;P1=t;}for(p=P0;p.y<P1.y;p.y++){//線性插值:c=(1-t)c0+tc1, t=(x-x0)/(x1-x0)或t=(y-y0)/(y1-y0)//  ------>c=c0+t(c1-c0)p.c=P0.c+(P1.c-P0.c)*(p.y-P0.y)/(p1.y-P0.y); //重載RGB,‘+’‘-’‘*’‘/’運算子pDC->SetPixelV(Round(p.x),Round(p.y),RGB(p.c.red,p.c.green,p.c.blue));}}else{double k,e=0;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;}  for(p=P0;p.y<P1.y;p.y++){//像素亮度層級c0=CRGB(e,e,e);c1=CRGB(1.0-e,1.0-e,1.0-e);//線性插值:c=(1-t)c0+tc1, t=(x-x0)/(x1-x0)或t=(y-y0)/(y1-y0)p.c=P0.c+(P1.c-P0.c)*(p.y-P0.y)/(p1.y-P0.y);pDC->SetPixelV(Round(p.x),Round(p.y),RGB(c0.red*p.c.red,c0.green*p.c.green,c0.blue*p.c.blue));pDC->SetPixelV(Round(p.x+1),Round(p.y),RGB(c1.red*p.c.red,c1.green*p.c.green,c1.blue*p.c.blue));e+=(1.0/k);                if(e>=1.0){p.x++;e--;}}}if(0.0<=k && k<=1.0)//繪製0=<k=<1 (x為主方向){if(P0.x>P1.x){t=P0;P0=P1;P1=t;}  for(p=P0;p.x<P1.x;p.x++){c0=CRGB(e,e,e);c1=CRGB(1.0-e,1.0-e,1.0-e);p.c=P0.c+(P1.c-P0.c)*(p.x-P0.x)/(p1.x-P0.x);pDC->SetPixelV(Round(p.x),Round(p.y),RGB(c0.red*p.c.red,c0.green*p.c.green,c0.blue*p.c.blue));pDC->SetPixelV(Round(p.x),Round(p.y+1),RGB(c1.red*p.c.red,c1.green*p.c.green,c1.blue*p.c.blue));e=e+k;                if(e>=1.0){p.y++;e--;}}}if(k>=-1.0 && k<0.0)//繪製-1=<k<0 (x為主方向){if(P0.x>P1.x){t=P0;P0=P1;P1=t;} for(p=P0;p.x<P1.x;p.x++){c0=CRGB(e,e,e);c1=CRGB(1.0-e,1.0-e,1.0-e);p.c=P0.c+(P1.c-P0.c)*(p.x-P0.x)/(p1.x-P0.x);pDC->SetPixelV(Round(p.x),Round(p.y),RGB(c0.red*p.c.red,c0.green*p.c.green,c0.blue*p.c.blue));pDC->SetPixelV(Round(p.x),Round(p.y-1),RGB(c1.red*p.c.red,c1.green*p.c.green,c1.blue*p.c.blue));e=e-k;                if(e>=1.0){p.y--;e--;}}}if(k<-1.0)//繪製k<-1 (y為主方向){if(P0.y<P1.y){t=P0;P0=P1;P1=t;}  for(p=P0;p.y>P1.y;p.y--){c0=CRGB(e,e,e);c1=CRGB(1.0-e,1.0-e,1.0-e);p.c=P0.c+(P1.c-P0.c)*(p.y-P0.y)/(p1.y-P0.y);pDC->SetPixelV(Round(p.x),Round(p.y),RGB(c0.red*p.c.red,c0.green*p.c.green,c0.blue*p.c.blue));pDC->SetPixelV(Round(p.x+1),Round(p.y),RGB(c1.red*p.c.red,c1.green*p.c.green,c1.blue*p.c.blue));e=e-1/k;                if(e>=1.0){p.x++;e--;}}}}P0=p1;}
4)OnDraw函數

void CWuLineView::OnDraw(CDC* pDC){CWuLineDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// 設定繪圖座標系:原點為View視圖地區中心,X軸正向水平向右,Y軸正向垂直向上CRect rect;GetClientRect(&rect);//獲得客戶區矩形的大小pDC->SetMapMode(MM_ANISOTROPIC);                    //自訂座標系pDC->SetWindowExt(rect.Width(),rect.Height());      //設定視窗比例pDC->SetViewportExt(rect.Width(),-rect.Height());   //設定視區比例,且x軸向右,y軸向上pDC->SetViewportOrg(rect.Width()/2,rect.Height()/2);//設定客戶區中心為座標系原點rect.OffsetRect(-rect.Width()/2,-rect.Height()/2);  //矩形與客戶區重合CWuAnti *line=new CWuAnti;//動態建立直線繪製類對象line->MoveTo(100,-200,CRGB(255,0,0));      line->LineTo(pDC,100,200,CRGB(0,255,0)); //0<=k<=1    line->MoveTo(-206,-137,CRGB(255,0,0));      line->LineTo(pDC,214,175,CRGB(0,255,0));  //k>1line->MoveTo(-201,-253,CRGB(255,0,0));      line->LineTo(pDC,201,266,CRGB(0,255,0));//k<-1line->MoveTo(-186,293,CRGB(255,0,0));      line->LineTo(pDC,201,-236,CRGB(0,255,0)); //-1<k<0line->MoveTo(-286,193,CRGB(255,0,0));      line->LineTo(pDC,221,-246,CRGB(0,255,0));  }
5)運行效果(放大25倍)







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

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

百度文庫,人生憶夢貢獻,Wu反走樣直線演算法:http://wenku.baidu.com/link?url=6Nqvs2eaZTf9XrEXL7siH0bZOkfEk3W2OYM33Lo4ItYbA68Q1bquvCqt5O-BsJVxts8AKvBXgnx_qi_X1Ys7kG9wlcO8zGgq7JBXE8tdr6a


電腦圖形學 彩色直線段的反走樣(5)

聯繫我們

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