Created
August 24, 2019 18:14
-
-
Save rberenguel/d565f7efbcf6046ba57b6d9f68131033 to your computer and use it in GitHub Desktop.
Koch snowflake in PostScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%!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