| 1 |
#!perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Test::More tests => 12; |
| 5 |
|
| 6 |
use Imager qw/NC/; |
| 7 |
use Imager::Test qw(is_image is_color3); |
| 8 |
|
| 9 |
sub PI () { 3.14159265358979323846 } |
| 10 |
|
| 11 |
-d "testout" or mkdir "testout"; |
| 12 |
|
| 13 |
my @cleanup; |
| 14 |
push @cleanup, "testout/060-polypoly.log"; |
| 15 |
Imager->open_log(log => "testout/060-polypoly.log"); |
| 16 |
|
| 17 |
my $red = NC(255,0,0); |
| 18 |
my $green = NC(0,255,0); |
| 19 |
my $blue = NC(0,0,255); |
| 20 |
my $white = NC(255,255,255); |
| 21 |
my $black = NC(0, 0, 0); |
| 22 |
|
| 23 |
END { |
| 24 |
unlink @cleanup unless $ENV{IMAGER_KEEP_FILES}; |
| 25 |
rmdir "testout"; |
| 26 |
} |
| 27 |
|
| 28 |
{ |
| 29 |
my $out = "testout/060-ppsimple.ppm"; |
| 30 |
my $im = Imager->new(xsize => 100, ysize => 100); |
| 31 |
ok($im->polypolygon |
| 32 |
( |
| 33 |
filled => 1, |
| 34 |
color => $red, |
| 35 |
points => |
| 36 |
[ |
| 37 |
[ |
| 38 |
[ 20, 20, 40, 40 ], |
| 39 |
[ 20, 80, 80, 20 ], |
| 40 |
], |
| 41 |
[ |
| 42 |
[ 60, 60, 80, 80 ], |
| 43 |
[ 20, 80, 80, 20 ], |
| 44 |
], |
| 45 |
] |
| 46 |
), "simple filled polypolygon"); |
| 47 |
ok($im->write(file => $out), "save to $out"); |
| 48 |
my $cmp = Imager->new(xsize => 100, ysize => 100); |
| 49 |
$cmp->box(filled => 1, color => $red, box => [ 20, 20, 39, 79 ]); |
| 50 |
$cmp->box(filled => 1, color => $red, box => [ 60, 20, 79, 79 ]); |
| 51 |
is_image($im, $cmp, "check expected output"); |
| 52 |
push @cleanup, $out; |
| 53 |
} |
| 54 |
|
| 55 |
{ |
| 56 |
my $im = Imager->new(xsize => 100, ysize => 100); |
| 57 |
my $cross_cw = |
| 58 |
[ |
| 59 |
[ |
| 60 |
[ 10, 90, 90, 10 ], |
| 61 |
[ 40, 40, 60, 60 ], |
| 62 |
], |
| 63 |
[ |
| 64 |
[ 40, 60, 60, 40 ], |
| 65 |
[ 10, 10, 90, 90 ], |
| 66 |
], |
| 67 |
]; |
| 68 |
ok($im->polypolygon |
| 69 |
( |
| 70 |
filled => 1, |
| 71 |
color => $red, |
| 72 |
points =>$cross_cw, |
| 73 |
mode => "nonzero", |
| 74 |
), "cross polypolygon nz"); |
| 75 |
save($im, "060-ppcrossnz.ppm"); |
| 76 |
my $cmp = Imager->new(xsize => 100, ysize => 100); |
| 77 |
$cmp->box(filled => 1, color => $red, box => [ 10, 40, 89, 59 ]); |
| 78 |
$cmp->box(filled => 1, color => $red, box => [ 40, 10, 59, 89 ]); |
| 79 |
is_image($im, $cmp, "check expected output"); |
| 80 |
|
| 81 |
my $im2 = Imager->new(xsize => 100, ysize => 100); |
| 82 |
ok($im2->polypolygon |
| 83 |
( |
| 84 |
filled => 1, |
| 85 |
color => $red, |
| 86 |
points =>$cross_cw, |
| 87 |
#mode => "nonzero", # default to evenodd |
| 88 |
), "cross polypolygon eo"); |
| 89 |
save($im2, "060-ppcrosseo.ppm"); |
| 90 |
|
| 91 |
$cmp->box(filled => 1, color => $black, box => [ 40, 40, 59, 59 ]); |
| 92 |
is_image($im2, $cmp, "check expected output"); |
| 93 |
|
| 94 |
# same as cross_cw except that the second box is in reversed order |
| 95 |
my $cross_diff = |
| 96 |
[ |
| 97 |
[ |
| 98 |
[ 10, 90, 90, 10 ], |
| 99 |
[ 40, 40, 60, 60 ], |
| 100 |
], |
| 101 |
[ |
| 102 |
[ 40, 60, 60, 40 ], |
| 103 |
[ 90, 90, 10, 10 ], |
| 104 |
], |
| 105 |
]; |
| 106 |
my $im3 = Imager->new(xsize => 100, ysize => 100); |
| 107 |
ok($im3->polypolygon |
| 108 |
( |
| 109 |
filled => 1, |
| 110 |
color => $red, |
| 111 |
points => $cross_diff, |
| 112 |
mode => "nonzero", |
| 113 |
), "cross polypolygon diff"); |
| 114 |
is_image($im3, $cmp, "check expected output"); |
| 115 |
save($im3, "060-ppcrossdiff.ppm"); |
| 116 |
} |
| 117 |
|
| 118 |
Imager->close_log; |
| 119 |
|
| 120 |
sub save { |
| 121 |
my ($im, $file) = @_; |
| 122 |
|
| 123 |
$file = "testout/" . $file; |
| 124 |
push @cleanup, $file; |
| 125 |
ok($im->write(file => $file), "save to $file"); |
| 126 |
} |