-
Notifications
You must be signed in to change notification settings - Fork 2
/
3d-in-2d.hs
189 lines (162 loc) · 6.22 KB
/
3d-in-2d.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import System.IO ()
import Graphics.Gloss
import Graphics.Gloss.Geometry.Angle
window :: Display
window = InWindow "3D in 2D with Haskell" (600, 450) (20, 20)
type Point3D = (Float, Float, Float)
cube0 :: [Point3D]
cube0 = [( 150.0, 150.0, 150.0),
( 150.0, -150.0, 150.0),
(-150.0, 150.0, 150.0),
(-150.0, -150.0, 150.0),
( 150.0, 150.0, -150.0),
( 150.0, -150.0, -150.0),
(-150.0, 150.0, -150.0),
(-150.0, -150.0, -150.0)]
cube1 :: [Point3D]
cube1 = [( 125.0, 125.0, 125.0),
( 125.0, -125.0, 125.0),
(-125.0, 125.0, 125.0),
(-125.0, -125.0, 125.0),
( 125.0, 125.0, -125.0),
( 125.0, -125.0, -125.0),
(-125.0, 125.0, -125.0),
(-125.0, -125.0, -125.0)]
cube2 :: [Point3D]
cube2 = [( 100.0, 100.0, 100.0),
( 100.0, -100.0, 100.0),
(-100.0, 100.0, 100.0),
(-100.0, -100.0, 100.0),
( 100.0, 100.0, -100.0),
( 100.0, -100.0, -100.0),
(-100.0, 100.0, -100.0),
(-100.0, -100.0, -100.0)]
p0' :: Point3D
p0' = (-150.0, -150.0, 150.0)
p1' :: Point3D
p1' = (-200.0, 50.0, 225.0)
p2' :: Point3D
p2' = (50.0, -200.0, -225.0)
p3' :: Point3D
p3' = (-150.0, -150.0, -150.0)
p0'' :: Point3D
p0'' = (-125.0, -125.0, 125.0)
p1'' :: Point3D
p1'' = (-150.0, 75.0, 175.0)
p2'' :: Point3D
p2'' = (75.0, -150.0, -175.0)
p3'' :: Point3D
p3'' = (-125.0, -125.0, -125.0)
p0''' :: Point3D
p0''' = (-100.0, -100.0, 100.0)
p1''' :: Point3D
p1''' = (-125.0, 75.0, 125.0)
p2''' :: Point3D
p2''' = (75.0, -125.0, -125.0)
p3''' :: Point3D
p3''' = (-100.0, -100.0, -100.0)
rotateX :: Float -> Point3D -> Point3D
rotateX degree (x, y, z) = (x, c*y + s*z, -s*y + c*z)
where radiant = normalizeAngle $ degToRad degree
c = cos radiant
s = sin radiant
rotateZ :: Float -> Point3D -> Point3D
rotateZ degree (x, y, z) = (c*x + s*y, -s*x + c*y, z)
where radiant = normalizeAngle $ degToRad degree
c = cos radiant
s = sin radiant
project :: Point3D -> Point
project (x0, y0, z0) = (projectedX, projectedY)
where (lookAtX, lookAtY, lookAtZ) = (0.0, 0.0, 0.0)
(x, y, z) = (x0 - lookAtX, y0 - lookAtY, z0 - lookAtZ)
(alpha, beta, gamma) = (degToRad 0.0, degToRad 0.0, degToRad 0.0)
(eyeX, eyeY, eyeZ) = (0.0, 0.0, 300.0)
(cosAlpha, sinAlpha) = (cos alpha, sin alpha)
(cosBeta, sinBeta) = (cos beta, sin beta)
(cosGamma, sinGamma) = (cos gamma, sin gamma)
(dx, dy, dz) = (cosBeta*(sinGamma*y + cosGamma*x) - sinBeta*z,
sinAlpha*(cosBeta*z + sinBeta*(sinGamma*y + cosGamma*x)) +
cosAlpha*(cosGamma*y - sinGamma*x),
cosAlpha*(cosBeta*z + sinBeta*(sinGamma*y + cosGamma*x)) -
sinAlpha*(cosGamma*y - sinGamma*x))
projectedX = eyeZ/dz*dx - eyeX
projectedY = eyeZ/dz*dy - eyeY
move :: Point3D -> Point3D -> Point3D
move (dx, dy, dz) (x, y, z) = (x + dx, y + dy, z + dz)
toWire :: Path -> Path
toWire [] = []
toWire xs = [p0, p1, p3, p2, p0, p4, p5, p7, p6, p4]
where p0 = xs !! 0
p1 = xs !! 1
p2 = xs !! 2
p3 = xs !! 3
p4 = xs !! 4
p5 = xs !! 5
p6 = xs !! 6
p7 = xs !! 7
frame :: Float -> Picture
frame seconds = pictures [Color black $ line $ toWire imageSpaceCoords0,
Color black $ line $ toWire imageSpaceCoords1,
Color black $ line $ toWire imageSpaceCoords2,
Color red $ line spline0,
Color green $ line spline1,
Color blue $ line spline2]
where offset = move (0.0, 0.0, 400.0)
rotate' = rotateX (seconds * 35.0) .
rotateZ (seconds * 45.0)
rotate'' = rotateX ((seconds-0.05) * 37.0) .
rotateZ ((seconds-0.05) * 47.0)
rotate''' = rotateX ((seconds-0.09) * 30.0) .
rotateZ ((seconds-0.09) * 40.0)
moved0 = map (offset . rotate') cube0
moved1 = map (offset . rotate'') cube1
moved2 = map (offset . rotate''') cube2
imageSpaceCoords0 = map project moved0
imageSpaceCoords1 = map project moved1
imageSpaceCoords2 = map project moved2
transform' = offset . rotate'
transform'' = offset . rotate''
transform''' = offset . rotate'''
[a, b, c, d] = map transform' [p0', p1', p2', p3']
[p, q, r, s] = map transform'' [p0'', p1'', p2'', p3'']
[i, j, k, l] = map transform''' [p0''', p1''', p2''', p3''']
spline0 = curve cubic 50 a b c d
spline1 = curve cubic 50 p q r s
spline2 = curve cubic 50 i j k l
main :: IO ()
main = animate window white frame
type PointGenerator = (Float ->
Point3D ->
Point3D ->
Point3D ->
Point3D ->
Point3D)
curve :: PointGenerator ->
Int ->
Point3D ->
Point3D ->
Point3D ->
Point3D ->
Path
curve pg res cp0 cp1 cp2 cp3 = map (\t -> project $ pg t cp0 cp1 cp2 cp3) $
take (res + 1) $ iterate (+ step) 0.0
where step = 1.0 / fromIntegral res::Float
cubic :: PointGenerator
cubic t cp0 cp1 cp2 cp3 = (xnew, ynew, znew)
where (x0, y0, z0) = cp0
(x1, y1, z1) = cp1
(x2, y2, z2) = cp2
(x3, y3, z3) = cp3
a = 1.0 - t
xnew = a^(3::Int)*x0 +
3.0*a^(2::Int)*t*x1 +
3.0*a*t^(2::Int)*x2 +
t^(3::Int)*x3
ynew = a^(3::Int)*y0 +
3.0*a^(2::Int)*t*y1 +
3.0*a*t^(2::Int)*y2 +
t^(3::Int)*y3
znew = a^(3::Int)*z0 +
3.0*a^(2::Int)*t*z1 +
3.0*a*t^(2::Int)*z2 +
t^(3::Int)*z3