Mandelbrot.xs

filter: Statementstmt 84.9% 62 / 73 Branchbran 68.7% 22 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.0% 84 / 105 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary lib/Math/Fractal/Mandelbrot.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 double m_limit = 5;
6 double m_epsilon = 0.001;
7 unsigned int m_max_iter = 600;
8 unsigned int m_w = 640;
9 unsigned int m_h = 480;
10 double m_x1 = -2.2;
11 double m_x2 = +1;
12 double m_y1 = -1.1;
13 double m_y2 = +1.1;
14  
15 /* C version (since using point() in XS code fails make test (but passes
16 manual runs huh?) */
17  
18 22 unsigned int _point(unsigned int x1, unsigned int y1)
19 {
20 double za,zb,za1;
21 double za2,zb2;
22 double x,y;
23 unsigned int RETVAL;
24  
25 22 x = (x1 * (m_x2 - m_x1) / m_w) + m_x1;
26 22 y = (y1 * (m_y2 - m_y1) / m_h) + m_y1;
27 22 za = 0;
28 22 zb = 0;
29 22 za2 = 0;
30 22 zb2 = 0;
31 22 RETVAL = 0;
32 66 ▶ while (RETVAL++ < m_max_iter)
Branch: 1 branch
BranchTrueFalseTotal
while (RETVAL++ < m_max_iter) 66 0 66
33 {
34 66 za1 = za2 - zb2 + x;
35 66 zb = 2 * za * zb + y;
36 66 za = za1;
37 66 za2 = za * za; zb2 = zb * zb;
38 66 ▶ if (za2 + zb2 > m_limit)
Branch: 1 branch
BranchTrueFalseTotal
if (za2 + zb2 > m_limit) 22 44 66
39 {
40 22 break;
41 }
42 }
43 22 ▶ if (RETVAL >= m_max_iter)
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL >= m_max_iter) 0 22 22
44 {
45 0 RETVAL = 0;
46 }
47 22 return RETVAL;
48 }
49  
50  
51 MODULE = Math::Fractal::Mandelbrot PACKAGE = Math::Fractal::Mandelbrot
52 PROTOTYPES: ENABLE
53  
54 ##############################################################################
55 # point() - calculate fractal at this point
56 # input: X and Y coordinates of the point
57  
58 unsigned int
59 point(myclass, x1, y1)
60 unsigned int x1
61 unsigned int y1
62 INIT:
63 double za,zb,za1;
64 double za2,zb2;
65 double x,y;
66  
67 CODE:
68 3 x = (x1 * (m_x2 - m_x1) / m_w) + m_x1;
69 3 y = (y1 * (m_y2 - m_y1) / m_h) + m_y1;
70 3 za = 0;
71 3 zb = 0;
72 3 za2 = 0;
73 3 zb2 = 0;
74 3 RETVAL = 0;
75 603 ▶ while (RETVAL++ < m_max_iter)
Branch: 1 branch
BranchTrueFalseTotal
while (RETVAL++ < m_max_iter) 602 1 603
76 {
77 602 za1 = za2 - zb2 + x;
78 602 zb = 2 * za * zb + y;
79 602 za = za1;
80 602 za2 = za * za; zb2 = zb * zb;
81 602 ▶ if (za2 + zb2 > m_limit)
Branch: 1 branch
BranchTrueFalseTotal
if (za2 + zb2 > m_limit) 2 600 602
82 {
83 2 break;
84 }
85 }
86 3 ▶ if (RETVAL >= m_max_iter)
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL >= m_max_iter) 1 2 3
87 {
88 1 RETVAL = 0;
89 }
90 OUTPUT:
91 RETVAL
92  
93 ##############################################################################
94 # hor_line() - calculate fractal at a horizontal stripe
95 # input: X and Y coordinates of the start point
96 # L length of line
97  
98 AV*
99 hor_line(myclass, x1, y1, l)
100 unsigned int x1
101 unsigned int y1
102 unsigned int l
103 INIT:
104 unsigned int x, x2, i, last, iter, same;
105 AV* a2;
106  
107 CODE:
108 1 a2 = (AV*)sv_2mortal((SV*)newAV());
109 1 av_extend (a2, l + 1);
110 1 x2 = x1 + l;
111 1 i = 1; same = 0; last = _point (x1,y1);
112 11 ▶ for (x = x1; x < x2; x++)
Branch: 1 branch
BranchTrueFalseTotal
for (x = x1; x < x2; x++) 10 1 11
113 {
114 10 iter = _point (x,y1);
115 10 av_push( a2, newSViv(iter) );
116 10 ▶ if (iter == last) { same++; } else { break; }
Branch: 1 branch
BranchTrueFalseTotal
if (iter == last) { same++; } else { break; } 10 0 10
117 }
118 1 ▶ for (;x < x2; x++)
Branch: 1 branch
BranchTrueFalseTotal
for (;x < x2; x++) 0 1 1
119 {
120 0 iter = _point (x,y1);
121 0 av_push( a2, newSViv(iter) );
122 }
123 1 av_push( a2, newSViv(same) );
124 1 RETVAL = (AV*)a2;
125 OUTPUT:
126 RETVAL
127  
128 ##############################################################################
129 # ver_line() - calculate fractal at a vertical stripe
130 # input: X and Y coordinates of the start point
131 # L length of line
132  
133 AV*
134 ver_line(myclass, x1, y1, l)
135 unsigned int x1
136 unsigned int y1
137 unsigned int l
138  
139 INIT:
140 unsigned int y, y2, i, last, iter, same;
141 AV* a2;
142  
143 CODE:
144 1 a2 = (AV*)sv_2mortal((SV*)newAV());
145 1 av_extend (a2, l + 1);
146 1 y2 = y1 + l;
147 1 i = 1; same = 0; last = _point (x1,y1);
148 11 ▶ for (y = y1; y < y2; y++)
Branch: 1 branch
BranchTrueFalseTotal
for (y = y1; y < y2; y++) 10 1 11
149 {
150 10 iter = _point (x1,y);
151 10 av_push( a2, newSViv(iter) );
152 10 ▶ if (iter == last) { same++; } else { break; }
Branch: 1 branch
BranchTrueFalseTotal
if (iter == last) { same++; } else { break; } 10 0 10
153 }
154 1 ▶ for (;y < y2; y++)
Branch: 1 branch
BranchTrueFalseTotal
for (;y < y2; y++) 0 1 1
155 {
156 0 iter = _point (x1,y);
157 0 av_push( a2, newSViv(iter) );
158 }
159 1 av_push( a2, newSViv(same) );
160 1 RETVAL = a2;
161 OUTPUT:
162 RETVAL
163  
164  
165 ##############################################################################
166 # set_max_iter() - set maximum iterations
167 # input: new max_iter
168  
169 unsigned int
170 set_max_iter(myclass, new_max_iter)
171 unsigned int new_max_iter
172 CODE:
173 1 m_max_iter = new_max_iter;
174 1 ▶ if (new_max_iter == 0) { m_max_iter = 1; } /* at least 1 */
Branch: 1 branch
BranchTrueFalseTotal
if (new_max_iter == 0) { m_max_iter = 1; } /* at least 1 */ 0 1 1
175 1 ▶ RETVAL = m_max_iter;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = m_max_iter; 0 1 1
176 OUTPUT:
177 RETVAL
178  
179  
180 ##############################################################################
181 # set_limit()
182 # input: new limit
183  
184 double
185 set_limit(myclass, new_limit)
186 double new_limit
187 CODE:
188 1 m_limit = new_limit;
189 1 ▶ RETVAL = m_limit;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = m_limit; 0 1 1
190 OUTPUT:
191 RETVAL
192  
193  
194 ##############################################################################
195 # set_epsilon()
196 # input: new limit
197  
198 double
199 set_epsilon(myclass, new_e)
200 double new_e
201 CODE:
202 1 m_epsilon = new_e;
203 1 ▶ RETVAL = m_epsilon;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = m_epsilon; 0 1 1
204 OUTPUT:
205 RETVAL
206  
207  
208 ##############################################################################
209 # set_bounds()
210 # input: x1,y1, x2,y2, w,h
211  
212 void
213 set_bounds(myclass, nx1, ny1, nx2, ny2, nw, nh)
214 double nx1
215 double ny1
216 double nx2
217 double ny2
218 unsigned int nw
219 unsigned int nh
220 CODE:
221 0 m_x1 = nx1;
222 0 m_y1 = ny1;
223 0 m_x2 = nx2;
224 0 m_y2 = ny2;
225 0 m_w = nw;
226 0 m_h = nh;
227  
↑ Summary lib/Math/Fractal/Mandelbrot.pm »