求图形外轮廓线的一个类

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

用图形剪切了做软件界面是个常用的手段,算法也有很多,这个算做原创,非常简单,图形不是很复杂的情况下效率比较高(也是一般的情况)

.h

#ifndef _UtilClsH

#define _UtilClsH

#include <vcl.h>

//---------------------------------------------------------------------------

class UtilCls

{

public:

static TPoint* ComputeOutline(TImage * image, TColor crKey, int& count);

protected:

private:

static TPoint LocateFirstPoint(TImage * image, TColor crKey);

static TPoint LocatePoint(TPoint curr, int& orgin, TImage * image, TColor crKey);

};

#endif

.cpp

#pragma hdrstop

#include "_UtilCls.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

TPoint UtilCls::LocateFirstPoint(TImage * image, TColor crKey)

{

//TODO: Add your source code here

int imgW,imgH;

imgW = image->Width;

imgH = image->Height;

int w,h;

for(h = (imgH>>1);h < imgH; h++)

{

for(w = 0; w < imgW; w++)

{

if(image->Canvas->Pixels[w][h] != crKey)

{

return TPoint(w,h);

}

}

}

int limit = imgH >>1;

for(h = 0;h < limit; h++)

{

for(w = 0; w < imgW; w++)

{

if(image->Canvas->Pixels[w][h] != crKey)

{

return TPoint(w,h);

}

}

}

return TPoint(0,0);

}

TPoint UtilCls::LocatePoint(TPoint curr, int& orgin, TImage * image, TColor crKey)

{

//TODO: Add your source code here

TPoint tpArr[8];

tpArr[0].x = curr.x - 1;

tpArr[0].y = curr.y - 1;

tpArr[1].x = curr.x - 1;

tpArr[1].y = curr.y;

tpArr[2].x = curr.x - 1;

tpArr[2].y = curr.y + 1;

tpArr[3].x = curr.x;

tpArr[3].y = curr.y + 1;

tpArr[4].x = curr.x + 1;

tpArr[4].y = curr.y + 1;

tpArr[5].x = curr.x + 1;

tpArr[5].y = curr.y;

tpArr[6].x = curr.x + 1;

tpArr[6].y = curr.y - 1;

tpArr[7].x = curr.x;

tpArr[7].y = curr.y - 1;

int imgW,imgH;

imgW = image->Width;

imgH = image->Height;

if(orgin<4)

{

orgin=orgin+4;

}

else

{

orgin=orgin-4;

}

for(int loop = 0;loop < 8;loop++)

{

orgin++;

if(orgin > 7)

orgin -= 8;

if(tpArr[orgin].x < 0 || tpArr[orgin].x >= imgW ||tpArr[orgin].y < 0 || tpArr[orgin].y >= imgH)

continue;

if(image->Canvas->Pixels[tpArr[orgin].x][tpArr[orgin].y] != crKey)

{

return tpArr[orgin];

}

}

return TPoint(0,0);

}

TPoint* UtilCls::ComputeOutline(TImage * image, TColor crKey, int& count)

{

//TODO: Add your source code here

DynamicArray<TPoint> tp;

TPoint orginp,nextp,currp;

int orgin = 4;

orginp = LocateFirstPoint(image,crKey);

currp = orginp;

do

{

nextp = LocatePoint(currp,orgin,image,crKey);

if(nextp.x == orginp.x && nextp.y == orginp.y)

{

break;

}

tp.Length = tp.Length + 1;

tp[tp.Length - 1] = nextp;

currp = nextp;

}while(true);

int loop = tp.Length;

count = loop;

TPoint * rtn = new TPoint[loop];

for(int i=0;i < loop;i++)

{

rtn[i].x = tp[i].x;

rtn[i].y = tp[i].y;

}

return rtn;

}

实际使用

//---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"

#include "_UtilCls.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

int count;

HRGN WndRgn,TempRgn;

TPoint* a = UtilCls::ComputeOutline(Image1,Image1->Canvas->Pixels[0][0],count); //一个函数就可以了

WndRgn = CreateRectRgn(0,0,Image1->Width,Image1->Height);

TempRgn = CreatePolygonRgn(a,count,ALTERNATE);

CombineRgn(WndRgn,WndRgn,TempRgn,RGN_AND);

DeleteObject(TempRgn);

delete a;

SetWindowRgn(Handle,WndRgn,true);

}

//---------------------------------------------------------------------------

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有