| 1 | package Math::Fractal::DLA::Race2Center; | ||
| 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} = "Race2Center"; | ||
| 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 | $self->addLogMessage("Begin: $start_x / $start_y"); | ||
| 37 | my $x = 0; my $y = 0; | ||
| 38 | my $setpoints = 0; | ||
| 39 | my $color_interval = int($maxpoints / $self->{COLORS}); | ||
| 40 | my $color_value = 0; | ||
| 41 | |||
| 42 | while ($setpoints < $maxpoints) | ||
| 43 | { | ||
| 44 | if (($setpoints % $color_interval) == 0) | ||
| 45 | { $color_value ++; $self->addLogMessage("New color $color_value after $setpoints points"); } | ||
| 46 | $x = $start_x; | ||
| 47 | $y = $start_y; | ||
| 48 | if ($self->{MATRIX}->[$start_x][$start_y] > 0) | ||
| 49 | { | ||
| 50 | $self->addLogMessage("Finished after $setpoints points"); | ||
| 51 | last; | ||
| 52 | } | ||
| 53 | my $dir = $self->getDirection(); | ||
| 54 | my $back_dir = ($dir + 2) % 4; | ||
| 55 | my $set = 0; | ||
| 56 | while ($set == 0) | ||
| 57 | { | ||
| 58 | if ((($x > 0) && ($self->{MATRIX}->[$x-1][$y] > 0)) || | ||
| 59 | (($x < $width) && ($self->{MATRIX}->[$x+1][$y] > 0)) || | ||
| 60 | (($y > 0) && ($self->{MATRIX}->[$x][$y-1] > 0)) || | ||
| 61 | (($y < $height) && ($self->{MATRIX}->[$x][$y+1] > 0))) | ||
| 62 | { | ||
| 63 | $set = 1; $setpoints ++; | ||
| 64 | $self->{MATRIX}->[$x][$y] = $color_value; | ||
| 65 | } | ||
| 66 | if (($x == 0) || ($x == $width) || ($y == 0) || ($y == $height)) | ||
| 67 | { | ||
| 68 | $set = 1; $setpoints ++; | ||
| 69 | $self->{MATRIX}->[$x][$y] = $color_value; | ||
| 70 | } | ||
| 71 | |||
| 72 | # Random to left or right | ||
| 73 | my $side = $self->getDirection() % 2; | ||
| 74 | my $val = $self->getDirection(); | ||
| 75 | |||
| 76 | my $x_ok = 0; my $y_ok = 0; | ||
| 77 | if (($x - $val > 0) && ($x + $val < $width)) | ||
| 78 | { $x_ok = 1; } | ||
| 79 | if (($y - $val > 0) && ($y + $val < $height)) | ||
| 80 | { $y_ok = 1; } | ||
| 81 | |||
| 82 | if (($dir == 0) && ($side == 0) && ($x_ok == 1)) { $x = $x - $val; $y --; } | ||
| 83 | elsif (($dir == 0) && ($side == 1) && ($x_ok == 1)) { $x = $x + $val; $y --; } | ||
| 84 | elsif (($dir == 1) && ($side == 0) && ($y_ok == 1)) { $x ++; $y = $y - $val; } | ||
| 85 | elsif (($dir == 1) && ($side == 1) && ($y_ok == 1)) { $x ++; $y = $y + $val; } | ||
| 86 | elsif (($dir == 2) && ($side == 0) && ($x_ok == 1)) { $x = $x + $val; $y ++; } | ||
| 87 | elsif (($dir == 2) && ($side == 1) && ($x_ok == 1)) { $x = $x - $val; $y ++; } | ||
| 88 | elsif (($dir == 3) && ($side == 0) && ($y_ok == 1)) { $x --; $y = $y + $val; } | ||
| 89 | elsif (($dir == 3) && ($side == 1) && ($y_ok == 1)) { $x --; $y = $y - $val; } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | $self->createImage(); | ||
| 93 | |||
| 94 | } # generate | ||
| 95 | |||
| 96 | 1; | ||
| 97 | |||
| 98 | __END__ |