| 1 | package Math::Fractal::DLA::Surrounding; | ||
| 2 | |||
| 3 | use Math::Fractal::DLA qw(:all); | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Exporter; | ||
| 7 | |||
| 8 | our @ISA = qw(Math::Fractal::DLA Exporter); | ||
| 9 | |||
| 10 | our @EXPORT_OK = qw(generate); | ||
| 11 | our @EXPORT = qw(); | ||
| 12 | |||
| 13 | our $VERSION = 0.21; | ||
| 14 | |||
| 15 | sub new | ||
| 16 | { | ||
| 17 | my $param = shift; | ||
| 18 | my $class = ref($param) || $param; | ||
| 19 | my $self = new Math::Fractal::DLA; | ||
| 20 | $self->{TYPE} = "Surrounding"; | ||
| 21 | bless ($self,$class); | ||
| 22 | return $self; | ||
| 23 | } # new | ||
| 24 | |||
| 25 | sub generate | ||
| 26 | { | ||
| 27 | my $self = $_[0]; | ||
| 28 | |||
| 29 | $self->addLogMessage("Fractal mode ".$self->{TYPE}); | ||
| 30 | |||
| 31 | my $width = $self->{IMG_WIDTH}; | ||
| 32 | my $height = $self->{IMG_HEIGHT}; | ||
| 33 | my $maxpoints = $self->{POINTS}; | ||
| 34 | my $start_x = int($self->{IMG_WIDTH} / 2); | ||
| 35 | my $start_y = int($self->{IMG_HEIGHT} / 2); | ||
| 36 | my $left = 0; my $top = 0; my $right = $width; my $bottom = $height; | ||
| 37 | $self->addLogMessage("Begin: $start_x / $start_y"); | ||
| 38 | my $color_interval = int($maxpoints / $self->{COLORS}); | ||
| 39 | my $color_value = 0; | ||
| 40 | my $x = 0; my $y = 0; | ||
| 41 | my $setpoints = 0; | ||
| 42 | |||
| 43 | while ($setpoints < $maxpoints) | ||
| 44 | { | ||
| 45 | if (($setpoints % $color_interval) == 0) | ||
| 46 | { $color_value ++; $self->addLogMessage("New color $color_value after $setpoints points"); } | ||
| 47 | $x = $start_x; | ||
| 48 | $y = $start_y; | ||
| 49 | if ($self->{MATRIX}->[$start_x][$start_y] > 0) | ||
| 50 | { | ||
| 51 | $self->addLogMessage("Finished after $setpoints points"); | ||
| 52 | last; | ||
| 53 | } | ||
| 54 | my $dir = $self->getDirection(); | ||
| 55 | my $back_dir = ($dir + 2) % 4; | ||
| 56 | if (($dir == 0) || ($dir == 2)) { $x = $left + rand($right - $left); } | ||
| 57 | elsif (($dir == 1) || ($dir == 3)) { $y = $top + rand($bottom - $top); } | ||
| 58 | |||
| 59 | my $set = 0; | ||
| 60 | while ($set == 0) | ||
| 61 | { | ||
| 62 | if ((($x > 0) && ($self->{MATRIX}->[$x-1][$y] > 0)) || | ||
| 63 | (($x < $width) && ($self->{MATRIX}->[$x+1][$y] > 0)) || | ||
| 64 | (($y > 0) && ($self->{MATRIX}->[$x][$y-1] > 0)) || | ||
| 65 | (($y < $height) && ($self->{MATRIX}->[$x][$y+1] > 0))) | ||
| 66 | { | ||
| 67 | $set = 1; $setpoints ++; | ||
| 68 | $self->{MATRIX}->[$x][$y] = $color_value; | ||
| 69 | if ($x == $left) { $left ++; } | ||
| 70 | elsif ($x == $right) { $right --; } | ||
| 71 | if ($y == $top) { $top ++; } | ||
| 72 | elsif ($y == $bottom) { $bottom --; } | ||
| 73 | } | ||
| 74 | if (($x == 0) || ($x == $width) || ($y == 0) || ($y == $height)) | ||
| 75 | { | ||
| 76 | $set = 1; $setpoints ++; | ||
| 77 | $self->{MATRIX}->[$x][$y] = $color_value; | ||
| 78 | if ($x == $left) { $left ++; } | ||
| 79 | elsif ($x == $right) { $right --; } | ||
| 80 | if ($y == $top) { $top ++; } | ||
| 81 | elsif ($y == $bottom) { $bottom --; } | ||
| 82 | } | ||
| 83 | |||
| 84 | # Random to left or right | ||
| 85 | my $side = $self->getDirection() % 2; | ||
| 86 | my $val = $self->getDirection(); | ||
| 87 | |||
| 88 | my $x_ok = 0; my $y_ok = 0; | ||
| 89 | if (($x - $val > 0) && ($x + $val < $width)) | ||
| 90 | { $x_ok = 1; } | ||
| 91 | if (($y - $val > 0) && ($y + $val < $height)) | ||
| 92 | { $y_ok = 1; } | ||
| 93 | |||
| 94 | if (($dir == 0) && ($side == 0)) { if ($x_ok == 1) { $x = $x - $val; } $y --; } | ||
| 95 | elsif (($dir == 0) && ($side == 1)) { if ($x_ok == 1) { $x = $x + $val; } $y --; } | ||
| 96 | elsif (($dir == 1) && ($side == 0)) { $x ++; if ($y_ok == 1) { $y = $y - $val; } } | ||
| 97 | elsif (($dir == 1) && ($side == 1)) { $x ++; if ($y_ok == 1) { $y = $y + $val; } } | ||
| 98 | elsif (($dir == 2) && ($side == 0)) { if ($x_ok == 1) { $x = $x + $val; } $y ++; } | ||
| 99 | elsif (($dir == 2) && ($side == 1)) { if ($x_ok == 1) { $x = $x - $val; } $y ++; } | ||
| 100 | elsif (($dir == 3) && ($side == 0)) { $x --; if ($y_ok == 1) { $y = $y + $val; } } | ||
| 101 | elsif (($dir == 3) && ($side == 1)) { $x --; if ($y_ok == 1) { $y = $y - $val; } } | ||
| 102 | } | ||
| 103 | } | ||
| 104 | $self->createImage(); | ||
| 105 | |||
| 106 | } # generate | ||
| 107 | |||
| 108 | 1; | ||
| 109 | |||
| 110 | __END__ |