Skip to content

Instantly share code, notes, and snippets.

@rberenguel rberenguel/Koch.ps
Created Aug 24, 2019

Embed
What would you like to do?
Koch snowflake in PostScript
%!PS-Adobe-2.0
%%% Start of L-system definition
/STARTK { FK +K +K FK +K +K FK} def
/FK {
dup 0 eq
{ DK } % if the recursion order ends, draw forward
{
1 sub % recurse
4 {dup} repeat % dup the number of parameters (order) needed.
FK -K FK +K +K FK -K FK }
ifelse
pop % pop the dup'd order
} bind def
/angleK 60 def
/-K { % rotation to the right
angleK neg rotate
} bind def
/+K { % rotation to the left
angleK rotate
} bind def
%%% End of L-System definition
/DK { sizeK 3 orderK exp div 0 rlineto } bind def
/thicknessK {1 orderK dup mul div} bind def
%%% Scaling factors
/orderK 3 def
/sizeK 300 def
%%% Draws a Koch's snowflake of radius 180 at 0 0
/Koch180 {
gsave
newpath
thicknessK setlinewidth
200 300 60 cos mul add
neg
200 100 60 sin mul add
neg
translate
200 200 moveto
orderK orderK orderK STARTK
stroke
closepath
grestore
} def % receives nothing
%%% Draws an arbitrary Koch's snowflake
/Koch {
/orderK exch store
gsave
3 1 roll
translate
180 div dup scale
rand 360 mod rotate
Koch180
grestore
} def % Receives x y size order
%%% Sample, bounded by an arc
400 400 100 3 Koch
newpath
400 400
100 0 360 arc
stroke
closepath
showpage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.