标签:des style blog http color io ar for strong
作者:卿笃军
原文地址:http://blog.csdn.net/qingdujun/article/details/40048285
本文通过一个完整的实例来演示,直线反走样Wu算法。
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
//二维点类
class CP2
{
public:
CP2();
CP2(double x,double y);
virtual ~CP2();
public: //方便访问,直接定义为共有
double x;
double y;
};
#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_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CP2::CP2()
{
}
CP2::~CP2()
{
}
2)创建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(CP2 p0); //移动到指定位置
void MoveTo(double x, double y);
void LineTo(CP2 p1, CDC *pDC); //绘制Wu反走样直线,不含终点
void LineTo(double x, double y, CDC *pDC);
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 <math.h>
#define Round(d) int(floor(d+0.5))//四舍五入宏定义
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWuAnti::CWuAnti()
{
}
CWuAnti::~CWuAnti()
{
}
void CWuAnti::MoveTo(CP2 p0) //记录直线起点函数
{
P0=p0;
}
void CWuAnti::MoveTo(double x, double y)
{
P0.x = x;
P0.y = y;
}
void CWuAnti::LineTo(double x, double y, CDC *pDC) //绘制
{
CP2 p;
p.x = x;
p.y = y;
LineTo(p, pDC);
}
void CWuAnti::LineTo(CP2 p1, CDC *pDC)
{
P1=p1;
CP2 p,t;
double k,e; //P(u)和P(d)的交点F(i),e为F(i)到P(u)的距离
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
{
k=(P1.y-P0.y)/(P1.x-P0.x); //斜率
if(P0.x>P1.x)
{
t=P0;P0=P1;P1=t;
}
if(k>=0) //绘制一三象限直线
{
for(p=P0,e=0;p.x<P1.x;p.x++)
{
pDC->SetPixelV(Round(p.x),Round(p.y),RGB(e*255,e*255,e*255));
pDC->SetPixelV(Round(p.x),Round(p.y+1),RGB((1-e)*255,(1-e)*255,(1-e)*255));
e+=k;
if(e>=1.0)
{
p.y++;
e--;
}
}
}
if(k<0) //绘制二四象限直线
{
for(p=P0,e=0;p.x<P1.x;p.x++)
{
pDC->SetPixelV(Round(p.x),Round(p.y),RGB(e*255,e*255,e*255));
pDC->SetPixelV(Round(p.x),Round(p.y+1),RGB((1-e)*255,(1-e)*255,(1-e)*255));
e+=k;
if(e<=-1.0)
{
p.y--;
e--;
}
}
}
}
P0=p1; //将终点赋值给起点
}
3)onDraw函数
void CWuLineView::OnDraw(CDC* pDC)
{
CWuLineDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CWuAnti *line=new CWuAnti;//动态创建直线绘制类对象
line->MoveTo(100,100);
line->LineTo(500,500, pDC);
}4)运行效果(放大25倍)
原文地址:http://blog.csdn.net/qingdujun/article/details/40048285
参考文献:计算机图形学基础教程(Visual C++版)(第2版) 孔令德 编著
标签:des style blog http color io ar for strong
原文地址:http://blog.csdn.net/qingdujun/article/details/40048285