矩形点位自动排布算法研究.docx
- 文档编号:4050791
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:17
- 大小:127.94KB
矩形点位自动排布算法研究.docx
《矩形点位自动排布算法研究.docx》由会员分享,可在线阅读,更多相关《矩形点位自动排布算法研究.docx(17页珍藏版)》请在冰豆网上搜索。
矩形点位自动排布算法研究
矩形点位自动排布算法研究
王朋武darlingpeng@20/12,2011
在游戏软件中的人员与机关排布、在粮库粮食温度测量时的监测杆布置以及各种与点位排布有关的软件编程中,如何根据需要快速自动地计算出各点位的坐标,是一个很重要的问题。
近段时间我对此进行了一番研究,以下仅对此进行简单地阐述,希望会对有关人员有所帮助。
假设排布方式为矩形。
对排布条件做如下分类:
第一点位置可能在:
西北角
东北角
西南角
东南角
排列方式分为:
层叠形
蛇形
排列方向有:
横排
竖排
横梅花
竖梅花
说明:
层叠形是指点位排列时,每一行或列的数字排列方式都一样,具有重复性,如下图例:
蛇形是指点位排列时,整个行或列的数字连线就象蛇一样具有连续性,如下图例:
梅花形是指点位排列时,行或列的点处在其上下左右行或列中间的空隙位置,每一点四周都均匀地排列着六个点,就象梅花一样。
如下图例:
由于在计算过程中的浮点数到整数变换时造成的累积误差,所以添加可调整各点间距以及整体位移的上下左右四个方向控件,进行点位调整。
程序用Delphi7编制。
程序不算复杂,在此我列出需要的主要程序源码,并把有关界面图拷屏贴出来,方便大家参考。
布点范围:
500×500。
{*****************************************
作者:
王朋武
时间:
30/11,2011
说明:
重新排列点位
******************************************}
procedureTfrm_poleloc.btn_arrangeClick(Sender:
TObject);
var
i,j,m,n,k,x0,y0,x,y,p,q,r,w,t:
Integer;
h,v:
Integer;
str_name:
String;
begin
ifnotgbx_arrange.Visiblethenexit;
m:
=5;
ifedt_row.Text<>''thenm:
=edt_row.Value;
if(cmb_dir.Text=‘横梅花’)or(cmb_dir.Text=‘竖梅花’)then
begin
i:
=1;
k:
=0;
whilek<=a_room.i_polesdo
begin
ifimod2=1then
k:
=k+m
else
k:
=k+m-1;
i:
=i+1;
end;
n:
=i-1;
endelse
n:
=Math.Ceil(a_room.i_poles/m);
ifcmb_dir.Text=‘横梅花’then
begin
t:
=m;
m:
=n;
n:
=t;
end;
k:
=m;
ifn>mthenk:
=n;
q:
=Math.Floor((490-20*k)/(k-1));//圆外轮廓间距
ifq<0thenq:
=0;
ifq=0then
begin//不可再增减
ii_h:
=0;
ii_v:
=0;
end;
p:
=20+q;//圆间距
h:
=p+ii_h;
v:
=p+ii_v;
ifm>nthen
begin
ifcmb_first.Text=‘西北角’then
begin
x0:
=Math.Floor((490-h*n+q)/2)+ii_ho;
y0:
=5-Math.Floor((m-1)/2)*ii_v-ii_ve;
endelseifcmb_first.Text=‘西南角’then
begin
x0:
=Math.Floor((490-h*n+q)/2)+ii_ho;
y0:
=475+Math.Floor((m-1)/2)*ii_v-ii_ve;
endelseifcmb_first.Text=‘东北角’then
begin
x0:
=Math.Floor((490+h*n-q)/2)+ii_ho;
y0:
=5-Math.Floor((m-1)/2)*ii_v-ii_ve;
endelse
begin//东南角
x0:
=Math.Floor((490+h*n-q)/2)+ii_ho;
y0:
=475+Math.Floor((m-1)/2)*ii_v-ii_ve;
end;
endelse
begin
ifcmb_first.Text=‘西北角’then
begin
x0:
=5-Math.Floor((n-1)/2)*ii_h+ii_ho;
y0:
=Math.Floor((490-v*m+q)/2)-ii_ve;
endelseifcmb_first.Text=‘西南角’then
begin
x0:
=5-Math.Floor((n-1)/2)*ii_h+ii_ho;
y0:
=Math.Floor((490+v*m-q)/2)-ii_ve;
endelseifcmb_first.Text=’东北角’then
begin
x0:
=475+Math.Floor((n-1)/2)*ii_h+ii_ho;
y0:
=Math.Floor((490-v*m+q)/2)-ii_ve;
endelse
begin//东南角
x0:
=475+Math.Floor((n-1)/2)*ii_h+ii_ho;
y0:
=Math.Floor((490+v*m-q)/2)-ii_ve;
end;
end;
ifcmb_dir.Text=‘横梅花’then
begin
t:
=m;
m:
=n;
n:
=t;
end;
w:
=m;
if(cmb_dir.Text=’横梅花’)or(cmb_dir.Text=‘竖梅花’)then
begin//梅花形
ifcmb_dir.Text=‘竖梅花’then//竖梅花
fori:
=1tondo//列
begin
ifimod2=1thenm:
=w
elsem:
=w-1;
j:
=1;
whilej<=mdo//行
begin
ifcmb_first.Text=‘西北角’then
begin
x:
=x0+(i-1)*h;
ifimod2=1then
y:
=y0+(j-1)*v
else
y:
=y0+(j-1)*v+Math.Floor(v/2);
endelseifcmb_first.Text=‘西南角’then
begin
x:
=x0+(i-1)*h;
ifimod2=1then
y:
=y0-(j-1)*v
else
y:
=y0-(j-1)*v-Math.Floor(v/2);
endelseifcmb_first.Text=‘东北角’then
begin
x:
=x0-(i-1)*h;
ifimod2=1then
y:
=y0+(j-1)*v
else
y:
=y0+(j-1)*v+Math.Floor(v/2);
endelse
begin//东南角
x:
=x0-(i-1)*h;
ifimod2=1then
y:
=y0-(j-1)*v
else
y:
=y0-(j-1)*v-Math.Floor(v/2);
end;
ifcmb_type.Text=‘层叠形’then
begin//层叠形
k:
=0;
fort:
=1toi-1do
iftmod2=1then
k:
=k+w
else
k:
=k+w-1;
k:
=k+j;//竖排
endelse
begin//蛇形
k:
=0;
fort:
=1toi-1do
iftmod2=1then
k:
=k+w
else
k:
=k+w-1;
ifimod2=1then
k:
=k+j
else
k:
=k+(m-j+1);
end;
j:
=j+1;
ifk>a_room.i_polesthencontinue;
r:
=0;
forq:
=0topan_loc.ControlCount-1do
begin
str_name:
=pan_loc.Controls[q].Name;
ifPos('btn_pole',str_name)<>1thencontinue;
ifStrToInt(Copy(str_name,9,10))=kthen
begin
r:
=q;
break;
end;
end;//endofforq
pan_loc.Controls[r].Left:
=x;
pan_loc.Controls[r].Top:
=y;
end;//endofwhilej
end//endoffori
else//横梅花
fori:
=1tondo//行
begin
ifimod2=1thenm:
=w
elsem:
=w-1;
j:
=1;
whilej<=mdo//列
begin
ifcmb_first.Text=‘西北角’then
begin
ifimod2=1then
x:
=x0+(j-1)*h
else
x:
=x0+(j-1)*h+Math.Floor(h/2);
y:
=y0+(i-1)*v
endelseifcmb_first.Text=‘西南角’then
begin
ifimod2=1then
x:
=x0+(j-1)*h
else
x:
=x0+(j-1)*h+Math.Floor(h/2);
y:
=y0-(i-1)*v
endelseifcmb_first.Text=‘东北角’then
begin
ifimod2=1then
x:
=x0-(j-1)*h
else
x:
=x0-(j-1)*h-Math.Floor(h/2);
y:
=y0+(i-1)*v
endelse
begin//东南角
ifimod2=1then
x:
=x0-(j-1)*h
else
x:
=x0-(j-1)*h-Math.Floor(h/2);
y:
=y0-(i-1)*v
end;
ifcmb_type.Text=‘层叠形’then
begin//层叠形
k:
=0;
fort:
=1toi-1do
iftmod2=1then
k:
=k+w
else
k:
=k+w-1;
k:
=k+j;//竖排
endelse
begin//蛇形
k:
=0;
fort:
=1toi-1do
iftmod2=1then
k:
=k+w
else
k:
=k+w-1;
ifimod2=1then
k:
=k+j
else
k:
=k+(m-j+1);
end;
j:
=j+1;
ifk>a_room.i_polesthencontinue;
r:
=0;
forq:
=0topan_loc.ControlCount-1do
begin
str_name:
=pan_loc.Controls[q].Name;
ifPos('btn_pole',str_name)<>1thencontinue;
ifStrToInt(Copy(str_name,9,10))=kthen
begin
r:
=q;
break;
end;
end;//endofforq
pan_loc.Controls[r].Left:
=x;
pan_loc.Controls[r].Top:
=y;
end;//endofwhilej
end;//endoffori
endelse//横排与竖排
fori:
=1tomdo
begin
forj:
=1tondo
begin
ifcmb_first.Text=‘西北角’then
begin
x:
=x0+(j-1)*h;
y:
=y0+(i-1)*v;
endelseifcmb_first.Text=‘西南角’then
begin
x:
=x0+(j-1)*h;
y:
=y0-(i-1)*v;
endelseifcmb_first.Text=‘东北角’then
begin
x:
=x0-(j-1)*h;
y:
=y0+(i-1)*v;
endelse
begin//东南角
x:
=x0-(j-1)*h;
y:
=y0-(i-1)*v;
end;
ifcmb_type.Text=‘层叠形’then
begin//矩形
ifcmb_dir.Text=‘横排’then
k:
=(i-1)*n+j//横排
else
k:
=(j-1)*m+i;//竖排
endelse
begin//蛇形
ifcmb_dir.Text=‘横排’then
begin//横排
ifimod2=1then
k:
=(i-1)*n+j
else
k:
=(i-1)*n+(n-j+1);
endelse
begin//竖排
ifjmod2=1then
k:
=(j-1)*m+i
else
k:
=(j-1)*m+(m-i+1);
end;
end;
ifk>a_room.i_polesthencontinue;
r:
=0;
forq:
=0topan_loc.ControlCount-1do
begin
str_name:
=pan_loc.Controls[q].Name;
ifPos('btn_pole',str_name)<>1thencontinue;
ifStrToInt(Copy(str_name,9,10))=kthen
begin
r:
=q;
break;
end;
end;//endofforq
pan_loc.Controls[r].Left:
=x;
pan_loc.Controls[r].Top:
=y;
end;//endofforj
end;//endoffori
end;
{*****************************************
作者:
王朋武
时间:
16/12,2011
说明:
纵向+1
******************************************}
procedureTfrm_poleloc.btn_upClick(Sender:
TObject);
begin
ifcmb_aid.Text=‘间隙’then
begin
ii_v:
=ii_v+1;
//ifii_v>0thenii_v:
=0;
endelse
ii_ve:
=ii_ve+1;
btn_arrange.OnClick(Sender);
end;
{*****************************************
作者:
王朋武
时间:
16/12,2011
说明:
纵向-1
******************************************}
procedureTfrm_poleloc.btn_downClick(Sender:
TObject);
begin
ifcmb_aid.Text=‘间隙’then
ii_v:
=ii_v-1
else
ii_ve:
=ii_ve-1;
btn_arrange.OnClick(Sender);
end;
{*****************************************
作者:
王朋武
时间:
16/12,2011
说明:
横向-1
******************************************}
procedureTfrm_poleloc.btn_leftClick(Sender:
TObject);
begin
ifcmb_aid.Text=‘间隙’then
ii_h:
=ii_h-1
else
ii_ho:
=ii_ho-1;
btn_arrange.OnClick(Sender);
end;
{*****************************************
作者:
王朋武
时间:
16/12,2011
说明:
横向+1
******************************************}
procedureTfrm_poleloc.btn_rightClick(Sender:
TObject);
begin
ifcmb_aid.Text=‘间隙’then
begin
ii_h:
=ii_h+1;
//ifii_h>0thenii_h:
=0;
endelse
ii_ho:
=ii_ho+1;
btn_arrange.OnClick(Sender);
end;
{*****************************************
作者:
王朋武
时间:
16/12,2011
说明:
首点位置改变
******************************************}
procedureTfrm_poleloc.cmb_firstChange(Sender:
TObject);
begin
btn_arrange.OnClick(Sender);
end;
真实点位排布效果图如下:
图1
图2
图3
图4
由于才疏学浅,水平有限,考虑可能不周,但对大家可起到抛砖引玉的作用,希望对大家有用处。
我一直信奉的格言是:
思路决定优劣,
胆量决定成败。
作者邮箱:
darlingpeng@
pengwuwang@
20/12,2011写
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 矩形 自动 排布 算法 研究