-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmill.hs
More file actions
55 lines (46 loc) · 1.43 KB
/
mill.hs
File metadata and controls
55 lines (46 loc) · 1.43 KB
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
{-# LANGUAGE OverloadedStrings #-}
-- Recursive picture animation.
--
-- Inspired by a similar animation (not equal)
-- to be found in the examples of the gloss library.
import Graphics.Web.Processing.Simple
import Graphics.Web.Processing.Html
main :: IO ()
main = writeHtml "processing.js" "mill.pde" "Mill demo" "mill.html" mill
mill :: ProcScript
mill = animateFigure Nothing Nothing 50 (Color 0 0 0 255) millf
speed :: Proc_Float
speed = 0.02
millf :: Proc_Int -> Figure
millf n =
let t = intToFloat n * speed
in FillColor (Color 0 0 0 0)
$ LineColor (Color 255 255 255 255)
$ millUnit 0 250 t
millUnit :: Int -- ^ Recursive level.
-> Proc_Float -- ^ Radius.
-> Proc_Float -- ^ Angle.
-> Figure
-- Recursion stop level.
millUnit 5 _ _ = mempty
-- Recursive call.
millUnit n r alpha =
let parity = even n
r2 = r/2
f a = (r2 * sin a, r2 * cos a)
p0 = (0,0)
p1 = f 0
p2 = f $ 2*pi/3
p3 = f $ 4*pi/3
r' = dist p1 p2 / 2
in Rotate (if parity then alpha else (-2) * alpha)
$ mconcat [
Line [p0,p1] , Line [p0,p2] , Line [p0,p3]
, Circle p1 r' , Circle p2 r' , Circle p3 r'
, Translate p1 $ millUnit (n+1) r' alpha
, Translate p2 $ millUnit (n+1) r' alpha
, Translate p3 $ millUnit (n+1) r' alpha
]
-- | Distance between two points.
dist :: Proc_Point -> Proc_Point -> Proc_Float
dist (a,b) (c,d) = sqrt $ (a - c)^2 + (b - d)^2