1 package Imager::Matrix2d;
4 use Scalar::Util qw(reftype looks_like_number);
11 Imager::Matrix2d - simple wrapper for matrix construction
16 $m1 = Imager::Matrix2d->identity;
17 $m2 = Imager::Matrix2d->rotate(radians=>$angle, x=>$cx, y=>$cy);
18 $m3 = Imager::Matrix2d->translate(x=>$dx, y=>$dy);
19 $m4 = Imager::Matrix2d->shear(x=>$sx, y=>$sy);
20 $m5 = Imager::Matrix2d->reflect(axis=>$axis);
21 $m6 = Imager::Matrix2d->scale(x=>$xratio, y=>$yratio);
22 $m8 = Imager::Matric2d->matrix($v11, $v12, $v13,
27 use Imager::Matrix2d qw(:handy);
28 # various m2d_* functions imported
29 # where m2d_(.*) calls Imager::Matrix2d->$1()
33 This class provides a simple wrapper around a reference to an array of
34 9 co-efficients, treated as a matrix:
40 Most of the methods in this class are constructors. The others are
43 Note that since Imager represents images with y increasing from top to
44 bottom, rotation angles are clockwise, rather than counter-clockwise.
50 use vars qw(@EXPORT_OK %EXPORT_TAGS @ISA);
52 require 'Exporter.pm';
53 @EXPORT_OK = qw(m2d_rotate m2d_identity m2d_translate m2d_shear
54 m2d_reflect m2d_scale);
57 handy=> [ qw(m2d_rotate m2d_identity m2d_translate m2d_shear
58 m2d_reflect m2d_scale) ],
69 Returns the identity matrix.
74 return bless [ 1, 0, 0,
79 =item rotate(radians=>$angle)
81 =item rotate(degrees=>$angle)
83 Creates a matrix that rotates around the origin, or around the point
84 (x,y) if the 'x' and 'y' parameters are provided.
89 my ($class, %opts) = @_;
92 if (defined $opts{radians}) {
93 $angle = $opts{radians};
95 elsif (defined $opts{degrees}) {
96 $angle = $opts{degrees} * 3.1415926535 / 180;
99 $Imager::ERRSTR = "degrees or radians parameter required";
103 if ($opts{'x'} || $opts{'y'}) {
106 return $class->translate('x'=>-$opts{'x'}, 'y'=>-$opts{'y'})
107 * $class->rotate(radians=>$angle)
108 * $class->translate('x'=>$opts{'x'}, 'y'=>$opts{'y'});
111 my $sin = sin($angle);
112 my $cos = cos($angle);
113 return bless [ $cos, -$sin, 0,
119 =item translate(x=>$dx, y=>$dy)
121 =item translate(x=>$dx)
123 =item translate(y=>$dy)
125 Translates by the specify amounts.
130 my ($class, %opts) = @_;
132 if (defined $opts{'x'} || defined $opts{'y'}) {
133 my $x = $opts{'x'} || 0;
134 my $y = $opts{'y'} || 0;
135 return bless [ 1, 0, $x,
140 $Imager::ERRSTR = 'x or y parameter required';
144 =item shear(x=>$sx, y=>$sy)
150 Shear by the given amounts.
154 my ($class, %opts) = @_;
156 if (defined $opts{'x'} || defined $opts{'y'}) {
157 return bless [ 1, $opts{'x'}||0, 0,
161 $Imager::ERRSTR = 'x and y parameters required';
165 =item reflect(axis=>$axis)
167 Reflect around the given axis, either 'x' or 'y'.
169 =item reflect(radians=>$angle)
171 =item reflect(degrees=>$angle)
173 Reflect around a line drawn at the given angle from the origin.
178 my ($class, %opts) = @_;
180 if (defined $opts{axis}) {
181 my $result = $class->identity;
182 if ($opts{axis} eq "y") {
183 $result->[0] = -$result->[0];
185 elsif ($opts{axis} eq "x") {
186 $result->[4] = -$result->[4];
189 $Imager::ERRSTR = 'axis must be x or y';
196 if (defined $opts{radians}) {
197 $angle = $opts{radians};
199 elsif (defined $opts{degrees}) {
200 $angle = $opts{degrees} * 3.1415926535 / 180;
203 $Imager::ERRSTR = 'axis, degrees or radians parameter required';
208 return $class->rotate(radians=>-$angle) * $class->reflect(axis=>'x')
209 * $class->rotate(radians=>$angle);
212 =item scale(x=>$xratio, y=>$yratio)
214 Scales at the given ratios.
216 You can also specify a center for the scaling with the C<cx> and C<cy>
222 my ($class, %opts) = @_;
224 if (defined $opts{'x'} || defined $opts{'y'}) {
225 $opts{'x'} = 1 unless defined $opts{'x'};
226 $opts{'y'} = 1 unless defined $opts{'y'};
227 if ($opts{cx} || $opts{cy}) {
228 return $class->translate('x'=>-$opts{cx}, 'y'=>-$opts{cy})
229 * $class->scale('x'=>$opts{'x'}, 'y'=>$opts{'y'})
230 * $class->translate('x'=>$opts{cx}, 'y'=>$opts{cy});
233 return bless [ $opts{'x'}, 0, 0,
239 $Imager::ERRSTR = 'x or y parameter required';
244 =item matrix($v11, $v12, $v13, $v21, $v22, $v23, $v31, $v32, $v33)
246 Create a matrix with custom co-efficients.
251 my ($class, @self) = @_;
254 return bless \@self, $class;
257 $Imager::ERRSTR = "9 co-efficients required";
264 Implements the overloaded '*' operator. Internal use.
266 Currently both the left and right-hand sides of the operator must be
272 my ($left, $right, $order) = @_;
275 if (reftype($right) eq "ARRAY") {
277 or croak "9 elements required in array ref";
279 ($left, $right) = ($right, $left);
286 $accum += $left->[3*$i + $k] * $right->[3*$k + $j];
288 $result[3*$i+$j] = $accum;
291 return bless \@result, __PACKAGE__;
294 croak "multiply by array ref or number";
297 elsif (defined $right && looks_like_number($right)) {
298 my @result = map $_ * $right, @$left;
300 return bless \@result, __PACKAGE__;
303 # something we don't handle
304 croak "multiply by array ref or number";
310 Implements the overloaded binary '+' operator.
312 Currently both the left and right sides of the operator must be
313 Imager::Matrix2d objects.
317 my ($left, $right, $order) = @_;
319 if (ref($right) && UNIVERSAL::isa($right, __PACKAGE__)) {
322 push @result, $left->[$_] + $right->[$_];
325 return bless \@result, __PACKAGE__;
334 Implements the overloaded stringification operator.
336 This returns a string containing 3 lines of text with no terminating
339 I tried to make it fairly nicely formatted. You might disagree :)
348 if (length() > $maxlen) {
352 $maxlen <= 9 or $maxlen = 9;
354 my @left = ('[ ', ' ', ' ');
355 my @right = ("\n", "\n", ']');
357 my $width = $maxlen+2;
361 my $val = $m->[$i*3+$j];
362 if (length $val > 9) {
363 $val = sprintf("%9f", $val);
364 if ($val =~ /\./ && $val !~ /e/i) {
370 $out .= sprintf("%-${width}s", "$val, ");
380 Implement the overloaded equality operator.
382 Provided for older perls that don't handle magic auto generation of eq
388 my ($left, $right) = @_;
390 return $left . "" eq $right . "";
395 The following functions are shortcuts to the various constructors.
397 These are not methods.
399 You can import these methods with:
401 use Imager::Matrix2d ':handy';
409 =item m2d_translate()
422 return __PACKAGE__->identity;
426 return __PACKAGE__->rotate(@_);
430 return __PACKAGE__->translate(@_);
434 return __PACKAGE__->shear(@_);
438 return __PACKAGE__->reflect(@_);
442 return __PACKAGE__->scale(@_);
449 Tony Cook <tony@develop-help.com>
453 Needs a way to invert a matrix.
457 Imager(3), Imager::Font(3)
459 http://imager.perl.org/