📄 edgetracer.cpp
字号:
// EdgeTracer.cpp: implementation of the CEdgeTracer class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MagicScissors.h"
#include "EdgeTracer.h"
#include "Image/Image.h"
#include "EdgeList.h"
#include "EdgePoint.h"
#include "Polygon.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CEdgeTracer::CEdgeTracer(CImage* pEdgeMap)
{
m_pEdgeMap = pEdgeMap;
}
CEdgeTracer::~CEdgeTracer()
{
}
CEdgeList* CEdgeTracer::TraceEdge(CEdgeList* pList, int sw)
{
if( !m_pEdgeMap ) return NULL;
int n = pList->GetLength();
CPoint pt;
BYTE** ptr = m_pEdgeMap->GetPtr();
CSize size = m_pEdgeMap->GetImageSize();
int sx, sy, ex, ey;
//int sw;
int x,y;
sw = 20;
CPoint ptm;
float dis, min;
CEdgeList* pEdgeList = new CEdgeList;
CPoint start;
for( int i = 0 ; i < n ; i += 3 )
{
pt = *(pList->GetPoint(i));
sx = pt.x - sw/2;
sy = pt.y - sw/2;
ex = sx + sw;
ey = sy + sw;
if( sx < 0 ) sx = 0;
if( sy < 0 ) sy = 0;
if( ex >= size.cx ) ex = size.cx-1;
if( ey >= size.cy ) ey = size.cy-1;
min = 9999;
dis = -1.f;
for( y = sy ; y <= ey ; y++ )
{
for( x = sx ; x <= ex ; x++ )
{
if( ptr[y][x] == 255 )
{
dis = (float)((pt.x-x)*(pt.x-x)+(pt.y-y)*(pt.y-y));
if( dis < min )
{
min = dis;
ptm.x = x;
ptm.y = y;
}
}
}
}
if( dis != -1.f )
pEdgeList->AddEdgePoint( ptm );
else
pEdgeList->AddEdgePoint( pt );
if( i == 0 )
{
if( dis != -1.f ) start = ptm;
else start = pt;
}
}
pEdgeList->AddEdgePoint(start);
return pEdgeList;
}
CEdgeList* CEdgeTracer::TraceEdge(CPolygon* pList, int sw)
{
if( !m_pEdgeMap ) return NULL;
int n = pList->GetLength();
CPoint pt;
BYTE** ptr = m_pEdgeMap->GetPtr();
CSize size = m_pEdgeMap->GetImageSize();
int sx, sy, ex, ey;
//int sw;
int x,y;
sw = 20;
CPoint ptm;
float dis, min;
CEdgeList* pEdgeList = new CEdgeList;
for( int i = 0 ; i < n ; i++ )
{
pt = *(pList->GetPoint(i));
sx = pt.x - sw/2;
sy = pt.y - sw/2;
ex = sx + sw;
ey = sy + sw;
if( sx < 0 ) sx = 0;
if( sy < 0 ) sy = 0;
if( ex >= size.cx ) ex = size.cx-1;
if( ey >= size.cy ) ey = size.cy-1;
min = 9999;
dis = -1.f;
for( y = sy ; y <= ey ; y++ )
{
for( x = sx ; x <= ex ; x++ )
{
if( ptr[y][x] == 255 )
{
dis = (float)((pt.x-x)*(pt.x-x)+(pt.y-y)*(pt.y-y));
if( dis < min )
{
min = dis;
ptm.x = x;
ptm.y = y;
}
}
}
}
if( dis != -1.f )
pEdgeList->AddEdgePoint( ptm );
else
pEdgeList->AddEdgePoint( pt );
}
return pEdgeList;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -