王朝网络
分享
 
 
 

求图形外轮廓线的一个类

王朝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);

}

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有