-
Notifications
You must be signed in to change notification settings - Fork 2
/
curve.monkey
144 lines (123 loc) · 3.24 KB
/
curve.monkey
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
Strict
Import horizon.vector2d
Import horizon.application
Class Curve
Field points:IntMap<Vector2D>
Field controlling? = False
Field controlPoint:Vector2D
Field pointCount%
Field numberOfSegments% = 300
Field segmentSize#
Field pos# = 0
Field speed# = 1.0
Method New()
pointCount = 0
points = New IntMap<Vector2D>
segmentSize = 1.0 / numberOfSegments
End
Method AddPoint:Void(x#, y#)
points.Set(pointCount, New Vector2D(x,y))
pointCount += 1
End
Method GetPoint:Vector2D(value%)
Return points.Get(value)
End
Method DrawPoints:Void()
SetColor($AA, $22, $22)
For Local p := Eachin points.Values()
DrawOval(p.x - 5, p.y - 5, 10, 10)
Next
End
Method CalculateSharedPoints:Void()
For Local i% = 2 To pointCount-2
points.Get(i-1).x = points.Get(i-1).x + ((points.Get(i+1).x - points.Get(i-1).x) * 0.5)
points.Get(i-1).y = points.Get(i-1).y + ((points.Get(i+1).y - points.Get(i-1).y) * 0.5)
Next
End
Method DrawLines:Void()
SetColor($88, $88, $88)
Local lastPoint:Vector2D = Null
For Local p := Eachin points.Values()
If (lastPoint)
DrawLine(lastPoint.x, lastPoint.y, p.x, p.y)
End
lastPoint = p
Next
End
Method Update:Void()
Local mx%, my%
mx = Application.GetInstance().VirtualMouseX()
my = Application.GetInstance().VirtualMouseY()
If (Not controlling)
If (MouseDown())
For Local p := Eachin points.Values()
If (mx > p.x - 15 And my > p.y - 15 And mx < p.x + 15 And my < p.y + 15)
controlling = True
controlPoint = p
End
Next
Else
controlling = False
End
End
If (controlling)
Local ox% = controlPoint.x
Local oy% = controlPoint.y
controlPoint.x = mx
controlPoint.y = my
Local dx% = controlPoint.x - ox
Local dy% = controlPoint.y - oy
For Local i% = 2 To pointCount
If (i Mod 2 = 0)
If (points.Get(i) = controlPoint)
'MoveCurve(dx, dy, i)
End
End
End
If (Not MouseDown()) Then controlling = False
End
End
Method DrawSegments:Void()
Local pp# = 0
Local ox% = -900
Local oy% = 0
Local firstPoint? = True
While (pp < ((pointCount-1) / 2) - 0.1)
pp += segmentSize * 10
Local p:Vector2D = GetPosition(pp)
If (ox <> -900) Then DrawLine(ox, oy, p.x, p.y)
ox = p.x
oy = p.y
Wend
End
Method MoveCurve:Void(dx%, dy%, point%)
For Local i% = 1 To pointCount-2
If (i <> point)
points.Get(i).x += dx
points.Get(i).y += dy
End
Next
End
Method ToString$()
Local s$
For Local p:Vector2D = Eachin points.Values()
s += "curve.AddPoint(" + Int(p.x) + ", " + Int(p.y) + ")~n"
Next
Return s
End
Method GetPosition:Vector2D(pos#)
Local point% = (Floor(pos) Mod ((pointCount-1) / 2) * 2)
pos = pos Mod 1
Local p1:Vector2D = GetPoint(point)
Local p2:Vector2D = GetPoint(point + 1)
Local p3:Vector2D = GetPoint(point + 2)
Local p12x# = (p2.x-p1.x) * pos
Local p12y# = (p2.y-p1.y) * pos
Local p23x# = (p3.x-p2.x) * pos
Local p23y# = (p3.y-p2.y) * pos
Local p:Vector2D = New Vector2D
p.x = (((p2.x+p23x) - (p1.x+p12x)) * pos) + p1.x + p12x
p.y = (((p2.y+p23y) - (p1.y+p12y)) * pos) + p1.y + p12y
Return p
End
End