aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/llgcode/ps/samples/Koch.ps
blob: 539432c372e9d70591887011573e2350725311b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
%%% Start of L-system definition

/STARTK { FK plusK plusK FK plusK plusK 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 minusK FK plusK plusK FK minusK FK }
  ifelse
  pop			% pop the dup'd order
} bind def

/angleK 60 def

/minusK { % rotation to the right 
  angleK neg rotate
} bind def

/plusK { % 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