-
Notifications
You must be signed in to change notification settings - Fork 1
/
Entity.hs
167 lines (137 loc) · 3.76 KB
/
Entity.hs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
module Entity where
import HVG.Type
import HVG.Context2D
import HVG.ContextState
import Control.Monad
import Data.Monoid
{- sepSeries
0
1/2
1/4 3/4
1/8 3/8 5/8 7/8
1/16 3/16 5/16 7/16 9/16 11/16 13/16 15/16
...
-}
sepSeries :: [Double]
sepSeries = 0 : inners where
inners = 0.5 : mix halfs (map (+ 0.5) halfs)
halfs = map (/ 2) inners
mix (a:as) (b:bs) = a : b : mix as bs
box :: Double -> Double -> Double -> Double -> Int -> [Builder ()] -> Builder ()
box x y w h level bodies = local $ do
applyTransform (translateMatrix x y)
setSize (Size w h)
tran <- getTransform
addDraw $ do
strokeStyle "#000"
transform tran
lineWidth 2
forM_ [1 .. level - 1] $ \i -> do
beginPath
moveTo (Point 0 (h / fromIntegral level * fromIntegral i))
lineTo (Point w (h / fromIntegral level * fromIntegral i))
stroke
lineWidth 3
strokeRect (Point 0 0) (Size w h)
let
center = Point (w / 2) (h / 2)
link = map pt2link $ join $ flip map sepSeries $ \d ->
[ Point (w * d) 0
, Point w (h * d)
, Point (w * (1 - d)) h
, Point 0 (h * (1 - d))
]
pt2link p =
LinkPoint
(movePoint tran p)
(pointDistance center p)
addLink link
forM_ (zip [1..] bodies) $ \(i, body) -> do
setSize (Size w (h / fromIntegral level))
body
applyTransform (translateMatrix 0 (h / fromIntegral level))
ellipse :: Double -> Double -> Double -> Double -> Builder () -> Builder ()
ellipse x y w h body = local $ do
applyTransform (translateMatrix x y)
tran <- getTransform
let centerTran = tran <> translateMatrix (w/2) (h/2)
setSize (Size w h)
addDraw $ do
strokeStyle "#000"
transform centerTran
lineWidth 3
beginPath
moveTo (Point 0 (h/2))
bezierCurveTo
(Point (0.552284749*w/2) (h/2))
(Point (w/2) (0.552284749*h/2))
(Point (w/2) 0)
bezierCurveTo
(Point (w/2) (-0.552284749*h/2))
(Point (0.552284749*w/2) (-h/2))
(Point 0 (-h/2))
bezierCurveTo
(Point (-0.552284749*w/2) (-h/2))
(Point (-w/2) (-0.552284749*h/2))
(Point (-w/2) 0)
bezierCurveTo
(Point (-w/2) (0.552284749*h/2))
(Point (-0.552284749*w/2) (h/2))
(Point 0 (h/2))
stroke
let
sepToPoint ratio =
let
arg = ratio * 2 * 3.14159265358979323846
in
movePoint
centerTran
( Point
(w/2 * cos arg)
(h/2 * sin arg)
)
link = map (\p -> LinkPoint p 0) $ map sepToPoint sepSeries
addLink link
body
textTop :: String -> Builder ()
textTop str = local $ do
tran <- getTransform
Size w h <- getSize
addDraw $ do
transform tran
textBaseline TextMiddle
textAlign TextCenter
font "15px sans-serif"
fillStyle "#000"
fillText str (Point (w / 2) 15) Nothing
text :: String -> Builder ()
text str = local $ do
tran <- getTransform
Size w h <- getSize
addDraw $ do
transform tran
textBaseline TextMiddle
textAlign TextCenter
font "15px sans-serif"
fillStyle "#000"
fillText str (Point (w / 2) (h / 2)) Nothing
link :: String -> String -> Builder ()
link aName bName = fork $ do
aLink <- queryLink aName
bLink <- queryLink bName
let
bestLinkPair n aLink bLink =
fst $ foldl
(\(best, bestDis) (cha, chaDis) -> if bestDis <= chaDis then (best, bestDis) else (cha, chaDis))
((Point 0 0, Point 0 0), 1/0)
[((a, b), aCost + pointDistance a b + bCost) | LinkPoint a aCost <- take n aLink, LinkPoint b bCost <- take n bLink]
(aEnd, bEnd) = bestLinkPair 64 aLink bLink
addDraw $ do
transform identityMatrix
strokeStyle "#000"
lineWidth 1
beginPath
moveTo aEnd
lineTo bEnd
stroke
addLink [LinkPoint (interpolatePoint aEnd bEnd 0.5) 0]