File: Polygon.pm

package info (click to toggle)
libmath-polygon-perl 2.00-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 256 kB
  • sloc: perl: 1,618; makefile: 2
file content (286 lines) | stat: -rw-r--r-- 5,933 bytes parent folder | download
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# This code is part of Perl distribution Math-Polygon version 2.00.
# The POD got stripped from this file by OODoc version 3.03.
# For contributors see file ChangeLog.

# This software is copyright (c) 2004-2025 by Mark Overmeer.

# This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later

#oodist: *** DO NOT USE THIS VERSION FOR PRODUCTION ***
#oodist: This file contains OODoc-style documentation which will get stripped
#oodist: during its release in the distribution.  You can use this file for
#oodist: testing, however the code of this development version may be broken!

package Math::Polygon;{
our $VERSION = '2.00';
}


use strict;
use warnings;

use Log::Report     'math-polygon';

# Include all implementations
use Math::Polygon::Calc;
use Math::Polygon::Clip;
use Math::Polygon::Transform;

#--------------------

sub new(@)
{	my $thing = shift;
	my $class = ref $thing || $thing;

	my @points;
	my %options;
	if(ref $thing)
	{	$options{clockwise} = $thing->{MP_clockwise};
	}

	while(@_)
	{	if(ref $_[0] eq 'ARRAY') { push @points, shift }
		else { my $k = shift; $options{$k} = shift }
	}
	$options{_points} = \@points;

	(bless {}, $class)->init(\%options);
}

sub init($$)
{	my ($self, $args) = @_;
	$self->{MP_points}    = $args->{points} || $args->{_points};
	$self->{MP_clockwise} = $args->{clockwise};
	$self->{MP_bbox}      = $args->{bbox};
	$self;
}

#--------------------

sub nrPoints() { scalar @{ $_[0]->{MP_points}} }


sub order() { @{ $_[0]->{MP_points}} -1 }


sub points(;$)
{	my ($self, $format) = @_;
	my $points = $self->{MP_points};
	$points    = [ polygon_format $format, @$points ] if $format;
	wantarray ? @$points : $points;
}


sub point(@)
{	my $points = shift->{MP_points};
	wantarray ? @{$points}[@_] : $points->[shift];
}

#--------------------

sub bbox()
{	my $self = shift;
	return @{$self->{MP_bbox}} if $self->{MP_bbox};

	my @bbox = polygon_bbox $self->points;
	$self->{MP_bbox} = \@bbox;
	@bbox;
}


sub area()
{	my $self = shift;
	return $self->{MP_area} if defined $self->{MP_area};
	$self->{MP_area} = polygon_area $self->points;
}


sub centroid(%)
{	my ($self, %args) = @_;
	$self->{MP_centroid} //= polygon_centroid \%args, $self->points;
}


sub isClockwise()
{	my $self = shift;
	return $self->{MP_clockwise} if defined $self->{MP_clockwise};  # undef == unknown here
	$self->{MP_clockwise} = (polygon_is_clockwise $self->points) || 0;
}


sub clockwise()
{	my $self = shift;
	return $self if $self->isClockwise;

	$self->{MP_points}    = [ reverse $self->points ];
	$self->{MP_clockwise} = 1;
	$self;
}


sub counterClockwise()
{	my $self = shift;
	$self->isClockwise or return $self;

	$self->{MP_points}    = [ reverse $self->points ];
	$self->{MP_clockwise} = 0;
	$self;
}


sub perimeter() { polygon_perimeter $_[0]->points }


sub startMinXY()
{	my $self = shift;
	$self->new(polygon_start_minxy $self->points);
}


sub beautify(@)
{	my ($self, %args) = @_;
	my @beauty = polygon_beautify \%args, $self->points;
	@beauty > 2 ? $self->new(points => \@beauty) : ();
}


sub equal($;@)
{	my $self  = shift;
	my ($other, $tolerance);
	if(@_ > 2 || ref $_[1] eq 'ARRAY') { $other = \@_ }
	else
	{	$other     = ref $_[0] eq 'ARRAY' ? shift : shift->points;
		$tolerance = shift;
	}
	polygon_equal scalar($self->points), $other, $tolerance;
}


sub same($;@)
{	my $self = shift;
	my ($other, $tolerance);
	if(@_ > 2 || ref $_[1] eq 'ARRAY') { $other = \@_ }
	else
	{	$other     = ref $_[0] eq 'ARRAY' ? shift : shift->points;
		$tolerance = shift;
	}
	polygon_same scalar($self->points), $other, $tolerance;
}


sub contains($)
{	my ($self, $point) = @_;
	polygon_contains_point($point, $self->points);
}


sub distance($)
{	my ($self, $point) = @_;
	polygon_distance($point, $self->points);
}


sub isClosed() { polygon_is_closed($_[0]->points) }

#--------------------

sub resize(@)
{	my ($self, %args) = @_;

	my $clockwise = $self->{MP_clockwise};
	if(defined $clockwise)
	{	my %args   = @_;
		my $xscale = $args{xscale} || $args{scale} || 1;
		my $yscale = $args{yscale} || $args{scale} || 1;
		$clockwise = not $clockwise if $xscale * $yscale < 0;
	}

	(ref $self)->new(
		points    => [ polygon_resize \%args, $self->points ],
		clockwise => $clockwise,
		# we could save the bbox calculation as well
	);
}


sub move(%)
{	my ($self, %args) = @_;

	(ref $self)->new(
		points    => [ polygon_move \%args, $self->points ],
		clockwise => $self->{MP_clockwise},
		bbox      => $self->{MP_bbox},
	);
}


sub rotate(%)
{	my ($self, %args) = @_;

	(ref $self)->new(
		points    => [ polygon_rotate \%args, $self->points ],
		clockwise => $self->{MP_clockwise},
		# we could save the bbox calculation as well
	);
}


sub grid(%)
{	my ($self, %args) = @_;

	(ref $self)->new(
		points    => [ polygon_grid \%args, $self->points ],
		clockwise => $self->{MP_clockwise},
		# probably we could save the bbox calculation as well
	);
}


sub mirror(@)
{	my ($self, %args) = @_;

	my $clockwise = $self->{MP_clockwise};
	$clockwise    = not $clockwise if defined $clockwise;

	(ref $self)->new(
		points    => [ polygon_mirror \%args, $self->points ],
		clockwise => $clockwise,
		# we could save the bbox calculation as well
	);
}


sub simplify(@)
{	my ($self, %args) = @_;

	(ref $self)->new(
		points    => [ polygon_simplify \%args, $self->points ],
		clockwise => $self->{MP_clockwise},
		bbox      => $self->{MP_bbox},       # protect bounds
	);
}

#--------------------

sub lineClip($$$$)
{	my ($self, @bbox) = @_;
	polygon_line_clip \@bbox, $self->points;
}


sub fillClip1($$$$)
{	my ($self, @bbox) = @_;
	my @clip = polygon_fill_clip1 \@bbox, $self->points;
	@clip ? $self->new(points => \@clip) : undef;
}

#--------------------

sub string(;$)
{	my ($self, $format) = @_;
	polygon_string $self->points($format);
}

1;