File: 90polygon.t

package info (click to toggle)
libmath-polygon-perl 1.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 256 kB
  • sloc: perl: 1,598; makefile: 2
file content (87 lines) | stat: -rw-r--r-- 2,016 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More tests => 39;

use Math::Polygon;

my @p = ([0,0],[1,1],[0,2],[0,0]);
my @q = ([1,1],[0,2],[0,0],[1,1]);  # rotated left 1

# Instantiate from array
my $p = Math::Polygon->new(@p);
ok(defined $p);

isa_ok($p, 'Math::Polygon');
cmp_ok($p->nrPoints, '==', scalar(@p));
cmp_ok($p->order, '==', 3);    # triangle
cmp_ok($p->area, '==', 1);
ok(!$p->isClockwise);  # computed

my $p02 = $p->point(2);
ok(defined $p02,                  "got point");
cmp_ok($p02->[0], '==', 0);
cmp_ok($p02->[1], '==', 2);

my @p02 = $p->point(2);
cmp_ok(scalar(@p02), '==', 1,     "got one point");
cmp_ok($p02[0][0], '==', 0);
cmp_ok($p02[0][1], '==', 2);

# Instantiate by option
my $p2 = Math::Polygon->new(points => \@p, clockwise => 1);
ok(defined $p);

isa_ok($p2, 'Math::Polygon');
cmp_ok($p2->nrPoints, '==', scalar(@p));

ok($p2->isClockwise);   # specified, incorrect ;-)

# Instantiate by instance call
my $p3 = $p2->new(@q);

isa_ok($p3, 'Math::Polygon');
cmp_ok($p3->nrPoints, '==', scalar(@q));
ok($p3->isClockwise);   # specified, incorrect ;-)

my $p31 = $p3->point(1);
ok(defined $p31,                  "got point from q (not p)");
cmp_ok($p31->[0], '==', 0);
cmp_ok($p31->[1], '==', 2);

# Comparison

ok($p->equal(@p));
ok($p->same(@p));
ok(!$p->equal(@q));
ok($p->same(@q));

ok($p->startMinXY(@p));
my $q = Math::Polygon->new(@q);
ok($q->startMinXY(@p)->equal($p));

my @r = $p->lineClip(-1,-1,1,1);

cmp_ok(scalar(@r),'==',1);
my $r = shift @r;
cmp_ok(scalar(@$r),'==',3);
cmp_ok($r->[0][0],'==',0);
cmp_ok($r->[0][1],'==',1);
cmp_ok($r->[1][0],'==',0);
cmp_ok($r->[1][1],'==',0);
cmp_ok($r->[2][0],'==',1);
cmp_ok($r->[2][1],'==',1);

# String

is $p->string, '[0,0], [1,1], [0,2], [0,0]', 'string';

my $p4 = Math::Polygon->new([3.1415, 2.182], [1.414, 1.732]);
is $p4->string("%.2f"), '[3.14,2.18], [1.41,1.73]', 'string format';

use Data::Dumper;
is_deeply [ $p4->points('%.1f') ],
          [ [ '3.1', '2.2' ], [ '1.4', '1.7' ] ], 'points format';