1 /** This file is part of Shapes.
3 ** Shapes is free software: you can redistribute it and/or modify
4 ** it under the terms of the GNU General Public License as published by
5 ** the Free Software Foundation, either version 3 of the License, or
8 ** Shapes is distributed in the hope that it will be useful,
9 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ** GNU General Public License for more details.
13 ** You should have received a copy of the GNU General Public License
14 ** along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 ** Copyright 2008, 2014 Henrik Tidefelt
20 ##lookin ..Shapes..Geometry
22 |** We begin with two useful general-purpose abstractions.
24 rotateAbout: \ z a → [shift z]()[rotate a]()[shift ~z]
26 accumulateGraphics2D: \ lst fun → [lst.foldl \ p e → ( p & [fun e] )
30 |** It is now straight-forward to implement the Koch curve (at some finite depth).
36 len: [Numeric..Math..abs pth]
38 sl2: [pth 2 * len / 3]
44 --[koch [rotateAbout sl1.p 60°][]seg2 depth]
45 --[koch [rotateAbout sl2.p ~60°][]seg2 depth]
50 |** Here's our first simple application to a straight line:
52 IO..•page << Traits..@width:0.3bp | [Graphics..stroke [koch (0cm,0cm)--(15cm,10cm) '6]]
55 |** As a more advanced application, we start from a circle, and draw also the curves at intermediate depths, in successively darker shades of gray:
58 c: [shift (5cm,~10cm)][][Geometry..circle 5cm]
62 \ i → ( Traits..@stroking:[Layout..mediate i*(1/end) 0.9*Traits..BW..WHITE Traits..BW..BLACK]
64 [Graphics..stroke [koch c i]] ) ]