DLA/Explode.pm untested

filter: Statementstmt 0.0% 0 / 156 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 272 CC 26 SCAR 59.3
CC
26
Cov
0%
generate
552.0
setStartPosition
20.0
new
2.0
CRAP 376.2
1 package Math::Fractal::DLA::Explode;
2
3 use Math::Fractal::DLA qw(:all);
4 use strict;
5 use warnings;
6 use Exporter;
7 use GD;
8
9 our @ISA = qw(Math::Fractal::DLA Exporter);
10
11 our @EXPORT_OK = qw();
12 our @EXPORT = qw();
13
14 our $VERSION = 0.21;
15
16 sub new
17 {
18 my $param = shift;
19 my $class = ref($param) || $param;
20 my $self = new Math::Fractal::DLA;
21 $self->{TYPE} = "Explode";
22 bless ($self,$class);
23 return $self;
24 } # new
25
26 sub generate
27 {
28 my $self = $_[0];
29
30 # Set the starting point in the matrix
31 # if the value is not available it'll be set to the middle of the image
32 if (($self->{START}->[0]) && ($self->{START}->[1]))
33 {
34 $self->{MATRIX}->[$self->{START}->[0]][$self->{START}->[1]] = 1;
35 }
36 else
37 {
38 $self->{START}->[0] = int($self->{IMG_WIDTH} / 2);
39 $self->{START}->[1] = int($self->{IMG_HEIGHT} / 2);
40 $self->{MATRIX}->[$self->{START}->[0]][$self->{START}->[1]] = 1;
41 }
42
43 $self->addLogMessage("Fractal mode ".$self->{TYPE});
44
45 my $setpoints = 0;
46 my $points = $self->{POINTS};
47 my $color_interval = int($points / $self->{COLORS});
48 my $color_value = 0;
49 my $newpoint = 1;
50 my $width = $self->{IMG_WIDTH}; my $height = $self->{IMG_HEIGHT};
51 my $x_min = 0; my $x_max = $width; my $y_min = 0; my $y_max = $height;
52 if (${$self->{START}}[0] - 2 > 0) { $x_min = ${$self->{START}}[0] - 2; }
53 if (${$self->{START}}[0] + 2 < $width) { $x_max = $x_max = ${$self->{START}}[0] + 2; }
54 if (${$self->{START}}[1] - 2 > 0) { $y_min = $y_min = ${$self->{START}}[1] - 2; }
55 if (${$self->{START}}[1] + 2 < $height) { $y_max = ${$self->{START}}[1] + 2; }
56 $self->addLogMessage("Starting point: ".${$self->{START}}[0]." ".${$self->{START}}[1]);
57 my $rand_width = $x_max - $x_min;
58 my $rand_height = $y_max - $y_min;
59 my $direction = -1;
60 my $steps = 500;
61
62 $self->addLogMessage("Starting area:");
63 $self->addLogMessage("min X: ".$x_min." max X: ".$x_max);
64 $self->addLogMessage("min Y: ".$y_min." max Y: ".$y_max);
65
66 while ($setpoints < $points)
67 {
68 if ((($setpoints % $color_interval) == 0) && ($newpoint == 1))
69 { $color_value ++; $self->addLogMessage("New color $color_value after $setpoints points"); }
70 $newpoint = 0;
71 $rand_width = $x_max - $x_min;
72 $rand_height = $y_max - $y_min;
73 my $x = 0; my $y = 0;
74
75 $direction++;
76 if ($direction == 4) { $direction = 0; }
77 if ($direction == 0)
78 { $x = $x_min + sprintf("%.0f",rand($rand_width)); $y = $y_min; $steps = $rand_height; }
79 elsif ($direction == 1)
80 { $y = $y_min + sprintf("%.0f",rand($rand_height)); $x = $x_max; $steps = $rand_width; }
81 elsif ($direction == 2)
82 { $x = $x_min + sprintf("%.0f",rand($rand_width)); $y = $y_max; $steps = $rand_height; }
83 elsif ($direction == 3)
84 { $y = $y_min + sprintf("%.0f",rand($rand_height)); $x = $x_min; $steps = $rand_width; }
85 if ($self->{MATRIX}->[$x][$y] > 0) { $setpoints ++; next; }
86
87 for (my $i = 0; $i < $steps; $i ++)
88 {
89 if (($self->{MATRIX}->[$x-1][$y] > 0) ||
90 ($self->{MATRIX}->[$x+1][$y] > 0) ||
91 ($self->{MATRIX}->[$x][$y-1] > 0) ||
92 ($self->{MATRIX}->[$x][$y+1] > 0))
93 {
94 $self->{MATRIX}->[$x][$y] = $color_value;
95 $setpoints ++;
96 $newpoint = 1;
97 last;
98 }
99 else
100 {
101 if (($direction == 0) && ($y_max > ($y + 1))) { $y = $y + 1; }
102 elsif ($direction == 1) { $x = $x - 1; }
103 elsif ($direction == 2) { $y = $y - 1; }
104 elsif ($direction == 3) { $x = $x + 1; }
105 }
106 }
107
108 if (($x == $x_min) && ($x_min > 0)) { $x_min = $x_min - 1; }
109 elsif (($x == $x_max) && ($x_max < $width - 1)) { $x_max = $x_max + 1; }
110 elsif (($y == $y_min) && ($y_min > 0)) { $y_min = $y_min - 1; }
111 elsif (($y == $y_max) && ($y_max < $height - 1)) { $y_max = $y_max + 1; }
112 }
113 if ($self->{DEBUG}) { $self->drawRectangle(x_min => $x_min, x_max => $x_max,y_min => $y_min, y_max => $y_max); }
114 $self->createImage();
115
116 } # generate
117
118 # Set the start position for the fractal
119 # Parameter: x => x-axis, y => y => y-axis
120 sub setStartPosition
121 {
122 my $self = shift;
123 my %coordinate = @_;
124 foreach my $coord (keys %coordinate)
125 {
126 if ($coordinate{$coord} !~ /^\d+$/) { $self->exitOnError("Parameter $coord not valid"); }
127 }
128
129 if ($coordinate{x} > $self->{IMG_WIDTH}) { $self->exitOnError("Parameter x is outside of image"); }
130 if ($coordinate{y} > $self->{IMG_HEIGHT}) { $self->exitOnError("Parameter y is outside of image"); }
131 @{$self->{START}} = ($coordinate{x},$coordinate{y});
132 return 1;
133 } # setStartPosition
134
135 # Draws a rectangle
136 # Parameter: x_min => xxx, x_max => xxx, y_min => xxx, y_max => xxx
137 sub drawRectangle
138 {
139 my $self = shift;
140 my %para = @_;
141 $self->addLogMessage("Area: x_min: ".$para{x_min}." x_max: ".$para{x_max}." y_min: ".$para{y_min}." y_max: ".$para{y_max});
142 for (my $a = $para{x_min}; $a <= $para{x_max}; $a++)
143 {
144 $self->{MATRIX}->[$a][$para{y_min}] = 1;
145 $self->{MATRIX}->[$a][$para{y_max}] = 1;
146 }
147 for (my $a = $para{y_min}; $a <= $para{y_max}; $a++)
148 {
149 $self->{MATRIX}->[$para{x_min}][$a] = 1;
150 $self->{MATRIX}->[$para{x_max}][$a] = 1;
151 }
152 } # drawRectangle
153
154 1;
155
156 __END__