Appendix B: Software
271
DPP=PP(2,:)-PP(1,:);
PPP(2,:)=0.5*(PP(1,:)+PP(2,:))-[DPP(2),-DPP(1)]/(2*sqrt(3));
Running the koch.m script produces Fig. 9.6, but the reader is encouraged to play
with the position of the additional point to explore variations of the snowflake.
The synthetic stock charts, shown in Figs. 9.7 and 9.8, rely on the function
nextiter4_random() to iteratively calculate intermediary points. As input it
receives the start and end point of a line segment in the array p, as well as the control
points pa and pb that describe where and how the intermediate points are displaced.
The variable scramble controls whether the three line segments should be randomly scrambled. The function returns the coordinates of the intermediate points in
the variable q. Inside the function first the additional points are calculated and then
the output array q is filled. Note that two points are doubled, which is required, if we
intend to scramble the order of the line segments. Scrambling is controlled by the
variable scramble and if it is unity, we first determine a random sequence of the
numbers 1, 2, and 3 that we use to reshuffle the order of the segments. Note that the
horizontal coordinate of the first and last point must agree with those of the initially
provided coordinates in p.
function q=nextiter4_random(p,pa,pb,scramble)
q=zeros(6,2);
dp=p(2,:)-p(1,:);
q(1,:)=p(1,:);
q(2,:)=p(1,:)+[pa(1)*dp(1),pa(2)*dp(2)];
q(3,:)=q(2,:);
q(4,:)=p(1,:)+[pb(1)*dp(1),pb(2)*dp(2)];
q(5,:)=q(4,:);
q(6,:)=p(2,:);
if scramble
qq=q;
a=1+floor(3*rand);
b=1+floor(3*rand);
while (a==b)
b=1+floor(3*rand);
end
c=(6/a)/b;
dax=qq(2*a,1)-qq(2*a-1,1);
dbx=qq(2*b,1)-qq(2*b-1,1);
q(1,1)=p(1,1);
q(1,2)=qq(2*a-1,2);
q(2,1)=p(1,1)+dax;
q(2,2)=qq(2*a,2);
q(3,1)=q(2,1);
q(3,2)=qq(2*b-1,2);
q(4,1)=q(2,1)+dbx;
271
DPP=PP(2,:)-PP(1,:);
PPP(2,:)=0.5*(PP(1,:)+PP(2,:))-[DPP(2),-DPP(1)]/(2*sqrt(3));
Running the koch.m script produces Fig. 9.6, but the reader is encouraged to play
with the position of the additional point to explore variations of the snowflake.
The synthetic stock charts, shown in Figs. 9.7 and 9.8, rely on the function
nextiter4_random() to iteratively calculate intermediary points. As input it
receives the start and end point of a line segment in the array p, as well as the control
points pa and pb that describe where and how the intermediate points are displaced.
The variable scramble controls whether the three line segments should be randomly scrambled. The function returns the coordinates of the intermediate points in
the variable q. Inside the function first the additional points are calculated and then
the output array q is filled. Note that two points are doubled, which is required, if we
intend to scramble the order of the line segments. Scrambling is controlled by the
variable scramble and if it is unity, we first determine a random sequence of the
numbers 1, 2, and 3 that we use to reshuffle the order of the segments. Note that the
horizontal coordinate of the first and last point must agree with those of the initially
provided coordinates in p.
function q=nextiter4_random(p,pa,pb,scramble)
q=zeros(6,2);
dp=p(2,:)-p(1,:);
q(1,:)=p(1,:);
q(2,:)=p(1,:)+[pa(1)*dp(1),pa(2)*dp(2)];
q(3,:)=q(2,:);
q(4,:)=p(1,:)+[pb(1)*dp(1),pb(2)*dp(2)];
q(5,:)=q(4,:);
q(6,:)=p(2,:);
if scramble
qq=q;
a=1+floor(3*rand);
b=1+floor(3*rand);
while (a==b)
b=1+floor(3*rand);
end
c=(6/a)/b;
dax=qq(2*a,1)-qq(2*a-1,1);
dbx=qq(2*b,1)-qq(2*b-1,1);
q(1,1)=p(1,1);
q(1,2)=qq(2*a-1,2);
q(2,1)=p(1,1)+dax;
q(2,2)=qq(2*a,2);
q(3,1)=q(2,1);
q(3,2)=qq(2*b-1,2);
q(4,1)=q(2,1)+dbx;
