Skip to content

Instantly share code, notes, and snippets.

@luser-dr00g
Last active March 27, 2016 05:22
Show Gist options
  • Save luser-dr00g/b5b0532fb83b5a722991 to your computer and use it in GitHub Desktop.
Save luser-dr00g/b5b0532fb83b5a722991 to your computer and use it in GitHub Desktop.
The Chaos Game
/randomseed { (/dev/random)(r)file read pop srand } bind def
/random { % max => 0<num<max
rand 16#7fffffff div mul
} bind def
/genpoly { % n => [ [x0 y0] ... [xn-1 yn-1] ]
2 dict begin
/n exch def
/da 360 n div def
[
0 1 n 1 sub {
da mul
dup cos
exch sin
2 array astore
} for
]
end
} bind def
/2sub { % x1 y1 x0 y0 => dx dy
exch % x1 y1 y0 x0
3 1 roll % x1 x0 y1 y0
sub % x1 x0 dy
3 1 roll % dy x1 x0
sub % dy dx
exch
} bind def
/drawpoint {
gsave
currentpoint currentlinewidth 2 mul
0 360 arc fill
grestore
} def
/iterate {
poly n random cvi get aload pop
currentpoint 2sub
exch frac mul
exch frac mul
rmoveto
drawpoint
} def
/chaos { % n frac iterations => - (create path)
4 dict begin
/iterations exch def
/frac exch def
/n exch def
/poly n genpoly def
0 0 moveto % find random start point
0 0
poly {
aload pop
4 2 roll 2sub
.5 random mul exch
.5 random mul exch
2 copy rmoveto
} forall
rmoveto
iterations //iterate repeat
end
} bind def
/setscale { % n => -
dup dup scale
1 exch div currentlinewidth mul setlinewidth
} bind def
/chaoschart {
0 1 4 { % i
0 1 3 { % i j
gsave
1 index % i j i
2 copy
1 add exch 1 add exch
translate % i j i
.3 .3 scale
%exch 4 mul add 3 add % i n calc n from i and j
%pop pop 3 % all triangles
pop pop n % use global n
frac iterations chaos stroke
/frac frac fracdelta add def
grestore
} for
pop
} bind for
} def
randomseed
currentlinewidth .5 mul setlinewidth
120 setscale
%/frac 3.14159 2 mul 3 div def
%/frac .5 def
%/frac .6199 def
/frac .4 def
/fracdelta .05 def
/iterations 10000 def
/n 5 def
chaoschart
showpage
quit
@luser-dr00g
Copy link
Author

Revision 2 draws different fractional lengths in each small drawing, instead of different polygons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment