Math::FitRect - Resize one rect in to another while preserving aspect ratio.


NAME

Math::FitRect - Resize one rect in to another while preserving aspect ratio.


SYNOPSIS

  use Math::FitRect;

  # This will return: {w=>40, h=>20, x=>0, y=>10}
  my $rect = fit_rect( [80,40] => 40 );

  # This will return: {w=>80, h=>40, x=>-19, y=>0}
  my $rect = crop_rect( [80,40] => 40 );


DESCRIPTION

This module is very simple in its content but can save much time, much like other simplistic modules like the Data::Pager manpage. This module is useful for calculating what size you should resize images as for such things as thumbnails. If you find any other uses for ``fitting rectangles in to each other'' let the author know.


RECT TYPE

When a method needs a rectangle type it can be in one of the following forms:

  1) $size
  This is a single scalar holding an unsigned integer that specifys a square.

  2) [ $w, $h ]
  An array ref with the first entry being the width and the second the height.

  3) { w=>$w, h=>$h }
  Hash ref.


METHODS

The $rect1 and $rect2 in the proceding examples will use the following:

    $rect1 = [20,10];               $rect2 = [40,8];
  ....................
  .                  .   ........................................
  .                  .   .                                      .
  .                  .   .                                      .
  .                  .   .                                      .
  .                  .   .                                      .
  .                  .   .                                      .
  .                  .   .                                      .
  .                  .   ........................................
  ....................

fit_rect

  my $new_rect1 = fit_rect( $rect1 => $rect2 );

Returns the following hash ref:

  { w=>16, h=>8, x=>12, y=>0 } = $new_rect1;
  ............----------------............
  .           |              |           .
  .           |              |           .
  .           |              |           .
  .           |              |           .
  .           |              |           .
  .           |              |           .
  ............----------------............

Takes two rectangles and fits the first one inside the second one. The rectangle that will be returned will be a hash ref with a 'w' and 'h' parameter as well as 'x' and 'y' parameters which will specify any offset.

crop_rect

  my $new_rect1 = crop_rect( $rect1 => $rect2 );

Returns the following hash ref:

  { w=>40, h=>20, x=>0, y=>-6 } = $new_rect1;
  ----------------------------------------
  |                                      |
  |                                      |
  |                                      |
  |                                      |
  |                                      |
  ........................................
  .                                      .
  .                                      .
  .                                      .
  .                                      .
  .                                      .
  .                                      .
  ........................................
  |                                      |
  |                                      |
  |                                      |
  |                                      |
  |                                      |
  ----------------------------------------

Like the fit_rect function, crop_rect takes two rectangles as a parameter and it makes $rect1 completely fill $rect2. This can mean that the top and bottom (as in the diagram above) or the left and right get ``chopped'' off, or cropped as it is usually called. This method returns a has just like fit_rect.


PRIVATE METHODS

Do not use these methods directly as there is no garauntee that they work as documented or that they will work the same in future versions.

_calc_rect

  _calc_rect( $type, $from_rect, $to_rect );

_normalize

  $rect = _normalize( $rect );

Take in a rect in any of the supported formats and returns a hash ref with the following keys:

  w - Width.
  h - Height.
  r - Aspect ratio. (w/h)


AUTHOR

Copyright (C) 2005 Aran Clary Deltac (CPAN: BLUEFEET)

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Address bug reports and comments to: <aran@arandeltac.com>. When sending bug reports, please provide the version of Geo::Distance, the version of Perl, and the name and version of the operating system you are using. Patches are welcome if you are brave!

 Math::FitRect - Resize one rect in to another while preserving aspect ratio.