-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.moon
More file actions
120 lines (87 loc) · 2.2 KB
/
system.moon
File metadata and controls
120 lines (87 loc) · 2.2 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
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
{graphics: g, :timer, :mouse, :keyboard} = love
export *
barycentric_coords = (x1, y1, x2, y2, x3,y3, px, py) ->
det = (y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3)
b1 = (y2 - y3) * (px - x3) + (x3 - x2) * (py - y3)
b2 = (y3 - y1) * (px - x3) + (x1 - x3) * (py - y3)
b1 = b1 / det
b2 = b2 / det
b3 = 1 - b1 - b2
b1, b2, b3
pt_in_tri = (...) ->
b1, b2, b3 = barycentric_coords ...
return false if b1 < 0 or b2 < 0 or b3 < 0
true
class Quad
a: 255
-- clockwise from top left
new: (x1, y1, x2, y2, x3, y3, x4, y4) =>
@[1] = x1
@[2] = y1
@[3] = x2
@[4] = y2
@[5] = x3
@[6] = y3
@[7] = x4
@[8] = y4
draw: (top_r, top_g, top_b, bot_r=top_r, bot_g=top_g, bot_b=top_b) =>
g.setColor top_r, top_g, top_b, @a
g.triangle "fill",
@[1], @[2],
@[3], @[4],
@[5], @[6]
g.setColor bot_r, bot_g, bot_b, @a
g.triangle "fill",
@[1], @[2],
@[5], @[6],
@[7], @[8]
-- g.setColor 255,100,100
-- g.point @[1], @[2]
-- g.setColor 100,255,100
-- g.point @[3], @[4]
-- g.setColor 100,100,255
-- g.point @[5], @[6]
-- g.setColor 100,255,255
-- g.point @[7], @[8]
-- g.setColor 255,255,255
contains_pt: (x,y) =>
return true if pt_in_tri @[1], @[2],
@[3], @[4],
@[5], @[6],
x,y
return true if pt_in_tri @[1], @[2],
@[5], @[6],
@[7], @[8],
x,y
false
__tostring: =>
"vec2d<(%f, %f), (%f, %f), (%f, %f), (%f, %f)>"\format unpack @
class TrapSystem
new: (@skew=0) =>
-- project to Quad
project_box: (box) =>
ax, ay, bx, by = box\unpack2!
x1, y1 = @project ax, ay
x2, y2 = @project bx, ay
x3, y3 = @project bx, by
x4, y4 = @project ax, by
Quad x1, y1, x2, y2, x3, y3, x4, y4
-- form system to euclidean
project: (x, y) =>
x * (@skew * y + 1), y * (1 - @skew^0.3)
-- euclidean to system
unproject: (x, y) =>
sy = y / (1 - @skew^0.3)
x / (@skew * sy + 1), sy
class SystemTest
skew: 0.003
draw: =>
system = TrapSystem @skew
g.push!
g.translate g.getWidth!/2, g.getHeight!/2
g.scale 3, 3
for y=-100, 100, 10
for x=-100, 100, 10
g.point system\project x,y
g.pop!
nil