(I)。我有一个包含命令“rectangle(x1,x2,y1,y2)”的软件,它可以在xy上生成一个左下角坐标(x1,y1)和右上角坐标(x2,y2)的矩形飞机
(II)。我想使用这个软件在x-y平面上创建所有带有堆叠矩形的字母数字,基本上我想用perl打印出那些命令
(III)。我的想法是通过用矩形填充此表来创建一个5x7表,然后将其放入我想要的坐标,如下所示:
#!/usr/bin/perl
use strict;
use warnings;
###=================================================================================###
###=================================================================================###
###========= This script will generate alphanumeric by filling polygon in =========###
###==================== the specific position of a 5 x 7 table =====================###
###================= following case represents the alphanumeric:"1"================###
###=================================================================================###
###=== --------------------- ===###
###=== | | | O | | | 6 ===###
###=== --------------------- ===###
###=== | | O | O | | | 5 ===###
###=== --------------------- ===###
###=== | | | O | | | 4 ===###
###=== --------------------- ===###
###=== | | | O | | | 3 row number ===###
###=== --------------------- ===###
###=== | | | O | | | 2 ===###
###=== --------------------- ===###
###=== | | | O | | | 1 ===###
###=== --------------------- ===###
###=== | | O | O | O | | 0 ===###
###=== ----------*---------- "*" stand for (coor_x, coor_y) ===###
###=== -2 -1 0 1 2 --> column number ===###
my grid_x = 1; # the size of a grid along x-direction
my grid_y = 1; # the size of a grid along y-direction
sub alphanum_1 {
my $coor_x = shift; # the x coordinate that I wanna put this "polygon 1"
my $coor_y = shift; # the y coordinate that I wanna put this "polygon 1"
my ($i,$j,$mkrstring);
my @col_neg2 = (""); # the positions which needs to be filled in col -2
my @col_neg1 = (0,5); # the positions which needs to be filled in col -1
my @col_zero = (0,1,2,3,4,5,6); # the positions which needs to be filled in col 0
my @col_pos1 = (0); # the positions which needs to be filled in col +1
my @col_pos2 = (""); # the positions which needs to be filled in col +2
my (@marker,@anchor);
for ($i=0; $i<=$#col_zero-1; $i++) {
$marker[$i] = ($col_zero[$i+1] - $col_zero[$i] == 1)? "m" : "0";
$mkrstring = $mkrstring.$marker[$i];
}
}
&alphanum_1;
问题就出现了,因为你可以看到列0需要被所有7行填充,如果我只想使用“矩形”命令一次而不是7次:
假设coor_x = 0,coor_y = 0
方法1:
rectangle(-0.5,0.5,0,7)
方法2:
rectangle(-0.5,0.5,0,1)
rectangle(-0.5,0.5,1,2)
rectangle(-0.5,0.5,2,3)
rectangle(-0.5,0.5,3,4)
rectangle(-0.5,0.5,4,5)
rectangle(-0.5,0.5,5,6)
rectangle(-0.5,0.5,6,7)
方法1和2将达到相同的结果,但我更喜欢使用方法1,这意味着我需要检查同一列中的“邻居关系”,但我陷入了这一部分,基本上,我想坚持矩形,只要它们可以在同一列中粘在一起。
我是perl的新手,有什么提示吗?我需要你的帮助!
答案 0 :(得分:1)
这是一种如何略微减少矩形数量的方法(绝对不是减少到最小值,但是,它可以帮助):
首先,我会选择矩阵的另一种表示形式:
my @one = ([0,0,0,0,0,0,0],
[1,0,0,0,0,1,0],
[1,1,1,1,1,1,1],
[1,0,0,0,0,0,0],
[0,0,0,0,0,0,0]);
@one
变量是对表示列的数组的引用数组。然后,
我会创建一个必须绘制的矩形数组:
my @rectangles;
for my $columnRef (@one) {
my @column = @$columnRef;
my @colRects = ();
my $inRectangle = 0;
my $rectBegin = 0;
for my $i (0 .. $#column) {
my $pixel = $column[$i];
# closing a rectangle
if($inRectangle == 1 and $pixel == 0) {
$inRectangle = 0;
my $rectEnd = $i - 1;
push @colRects, [$rectBegin, $rectEnd];
}
# opening a rectangle
if ($inRectangle == 0 and $pixel == 1) {
$inRectangle = 1;
$rectBegin = $i;
}
}
# don't forget to close a rectangle if opened
if ($inRectangle == 1) {
push @colRects, [$rectBegin, $#column];
}
push @rectangles, \@colRects;
}
此代码应在@rectangles
变量中填入每列的矩形开头和结尾列表:
([], [[0,0],[5,5]], [[0,6]], [[0,0]], [])
您可以使用
生成所需的输出my $x = -2;
for my $col_rectangles (@rectangles) {
for my $col_rectangle (@$col_rectangles) {
printf("rectangle(%d,%d,%d,%d)",
$x - 0.5,
$x + 0.5,
@$col_rectangle,
);
}
}
现在,您已经分别最小化了每列中rectangle
函数的调用次数。但是,这种解决方案在更多面向水平的字母上表现不佳。
您当然可以转置它并在行而不是列上执行优化,但不知何故,我认为这样会更好。