1 package Imager::Matrix2d;
5 $VERSION = sprintf "%d.%03d", q$Revision$=~/\d+/g;
9 Imager::Matrix2d - simple wrapper for matrix construction
14 $m1 = Imager::Matrix2d->identity;
15 $m2 = Imager::Matrix2d->rotate(radians=>$angle, x=>$cx, y=>$cy);
16 $m3 = Imager::Matrix2d->translate(x=>$dx, y=>$dy);
17 $m4 = Imager::Matrix2d->shear(x=>$sx, y=>$sy);
18 $m5 = Imager::Matrix2d->reflect(axis=>$axis);
19 $m6 = Imager::Matrix2d->scale(x=>$xratio, y=>$yratio);
22 use Imager::Matrix2d qw(:handy);
23 # various m2d_* functions imported
24 # where m2d_(.*) calls Imager::Matrix2d->$1()
28 This class provides a simple wrapper around a reference to an array of
29 9 co-efficients, treated as a matrix:
35 Most of the methods in this class are constructors. The others are
38 Note that since Imager represents images with y increasing from top to
39 bottom, rotation angles are clockwise, rather than counter-clockwise.
45 use vars qw(@EXPORT_OK %EXPORT_TAGS @ISA);
47 require 'Exporter.pm';
48 @EXPORT_OK = qw(m2d_rotate m2d_identity m2d_translate m2d_shear
49 m2d_reflect m2d_scale);
52 handy=> [ qw(m2d_rotate m2d_identity m2d_translate m2d_shear
53 m2d_reflect m2d_scale) ],
63 Returns the identity matrix.
68 return bless [ 1, 0, 0,
73 =item rotate(radians=>$angle)
75 =item rotate(degrees=>$angle)
77 Creates a matrix that rotates around the origin, or around the point
78 (x,y) if the 'x' and 'y' parameters are provided.
83 my ($class, %opts) = @_;
86 if (defined $opts{radians}) {
87 $angle = $opts{radians};
89 elsif (defined $opts{degrees}) {
90 $angle = $opts{degrees} * 3.1415926535 / 180;
93 $Imager::ERRSTR = "degrees or radians parameter required";
97 if ($opts{'x'} || $opts{'y'}) {
100 return $class->translate('x'=>-$opts{'x'}, 'y'=>-$opts{'y'})
101 * $class->rotate(radians=>$angle)
102 * $class->translate('x'=>$opts{'x'}, 'y'=>$opts{'y'});
105 my $sin = sin($angle);
106 my $cos = cos($angle);
107 return bless [ $cos, -$sin, 0,
113 =item translate(x=>$dx, y=>$dy)
115 Translates by the specify amounts.
119 my ($class, %opts) = @_;
121 if (defined $opts{'x'} && defined $opts{'y'}) {
122 return bless [ 1, 0, $opts{'x'},
127 $Imager::ERRSTR = 'x and y parameters required';
131 =item shear(x=>$sx, y=>$sy)
133 Shear by the given amounts.
137 my ($class, %opts) = @_;
139 if (defined $opts{'x'} || defined $opts{'y'}) {
140 return bless [ 1, $opts{'x'}||0, 0,
144 $Imager::ERRSTR = 'x and y parameters required';
148 =item reflect(axis=>$axis)
150 Reflect around the given axis, either 'x' or 'y'.
152 =item reflect(radians=>$angle)
154 =item reflect(degrees=>$angle)
156 Reflect around a line drawn at the given angle from the origin.
161 my ($class, %opts) = @_;
163 if (defined $opts{axis}) {
164 my $result = $class->identity;
165 if ($opts{axis} eq "y") {
166 $result->[0] = -$result->[0];
168 elsif ($opts{axis} eq "x") {
169 $result->[4] = -$result->[4];
172 $Imager::ERRSTR = 'axis must be x or y';
179 if (defined $opts{radians}) {
180 $angle = $opts{radians};
182 elsif (defined $opts{degrees}) {
183 $angle = $opts{degrees} * 3.1415926535 / 180;
186 $Imager::ERRSTR = 'axis, degrees or radians parameter required';
191 return $class->rotate(radians=>-$angle) * $class->reflect(axis=>'x')
192 * $class->rotate(radians=>$angle);
195 =item scale(x=>$xratio, y=>$yratio)
197 Scales at the given ratios.
199 You can also specify a center for the scaling with the cx and cy
205 my ($class, %opts) = @_;
207 if (defined $opts{'x'} || defined $opts{'y'}) {
208 $opts{'x'} = 1 unless defined $opts{'x'};
209 $opts{'y'} = 1 unless defined $opts{'y'};
210 if ($opts{cx} || $opts{cy}) {
211 return $class->translate('x'=>-$opts{cx}, 'y'=>-$opts{cy})
212 * $class->scale('x'=>$opts{'x'}, 'y'=>$opts{'y'})
213 * $class->translate('x'=>$opts{cx}, 'y'=>$opts{cy});
216 return bless [ $opts{'x'}, 0, 0,
222 $Imager::ERRSTR = 'x or y parameter required';
229 Implements the overloaded '*' operator. Internal use.
231 Currently both the left and right-hand sides of the operator must be
236 my ($left, $right, $order) = @_;
238 if (ref($right) && UNIVERSAL::isa($right, __PACKAGE__)) {
240 ($left, $right) = ($right, $left);
247 $accum += $left->[3*$i + $k] * $right->[3*$k + $j];
249 $result[3*$i+$j] = $accum;
252 return bless \@result, __PACKAGE__;
255 # presumably N * matrix or matrix * N
256 return undef; # for now
262 Implements the overloaded binary '+' operator.
264 Currently both the left and right sides of the operator must be
265 Imager::Matrix2d objects.
269 my ($left, $right, $order) = @_;
271 if (ref($right) && UNIVERSAL::isa($right, __PACKAGE__)) {
274 push @result, $left->[$_] + $right->[$_];
277 return bless \@result, __PACKAGE__;
286 Implements the overloaded stringification operator.
288 This returns a string containing 3 lines of text with no terminating
291 I tried to make it fairly nicely formatted. You might disagree :)
299 if (length() > $maxlen) {
303 $maxlen <= 9 or $maxlen = 9;
305 my @left = ('[ ', ' ', ' ');
306 my @right = ("\n", "\n", ']');
308 my $width = $maxlen+2;
312 my $val = $m->[$i*3+$j];
313 if (length $val > 9) {
314 $val = sprintf("%9f", $val);
315 if ($val =~ /\./ && $val !~ /e/i) {
321 $out .= sprintf("%-${width}s", "$val, ");
331 The following functions are shortcuts to the various constructors.
333 These are not methods.
335 You can import these methods with:
337 use Imager::Matrix2d ':handy';
345 =item m2d_translate()
356 return __PACKAGE__->identity;
360 return __PACKAGE__->rotate(@_);
364 return __PACKAGE__->translate(@_);
368 return __PACKAGE__->shear(@_);
372 return __PACKAGE__->reflect(@_);
376 return __PACKAGE__->scale(@_);
383 Tony Cook <tony@develop-help.com>
387 Needs a way to invert matrixes.
391 Imager(3), Imager::Font(3)
393 http://imager.perl.org/