Algorithm-BinPack-2D

 view release on metacpan or  search on metacpan

t/01_basic.t  view on Meta::CPAN

# vim: set expandtab ts=4 sw=4 nowrap ft=perl ff=unix :
use strict;
use warnings;
use Test::More;
use Algorithm::BinPack::2D;

subtest 'Basic algorithm' => sub {
    my $packer = Algorithm::BinPack::2D->new(
        binwidth  => 500,
        binheight => 400,
    );

    $packer->add_item(label => 'one',   width => 300, height => 100);
    $packer->add_item(label => 'two',   width => 200, height => 100);
    $packer->add_item(label => 'three', width => 100, height => 200);
    $packer->add_item(label => 'four',  width => 100, height => 200);
    $packer->add_item(label => 'five',  width => 200, height => 100);
    $packer->add_item(label => 'six',   width => 300, height => 300);
    $packer->add_item(label => 'seven', width => 200, height => 100);
    $packer->add_item(label => 'eight', width => 450, height => 350);

    my @bins = $packer->pack_bins;
    is_deeply(
        \@bins,
        [
            {
                'width'  => 450,
                'height' => 350,
                'items'  => [
                    {
                        'width'  => 450,
                        'y'      => 0,
                        'label'  => 'eight',
                        'x'      => 0,
                        'height' => 350
                    }
                ]
            },
            {
                'width'  => 500,
                'height' => 400,
                'items'  => [
                    {
                        'width'  => 300,
                        'y'      => 0,
                        'label'  => 'six',
                        'x'      => 0,
                        'height' => 300
                    },
                    {
                        'width'  => 300,
                        'y'      => 300,
                        'label'  => 'one',
                        'x'      => 0,
                        'height' => 100
                    },
                    {
                        'width'  => 200,
                        'y'      => 0,
                        'label'  => 'five',
                        'x'      => 300,



( run in 0.672 second using v1.01-cache-2.11-cpan-5623c5533a1 )