-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfns.jl
228 lines (180 loc) · 4.57 KB
/
fns.jl
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using Luxor
function mag(p::Point)
return sqrt(p.x^2+p.y^2)
end
function perpendicular(p::Point)
return Point(-p.y,p.x)
end
function perp(p::Point)
return Point(-p.y,p.x)
end
function map(x,x0,x1,y0,y1)
return (x-x0)/(x1-x0)*(y1-y0)+y0
end
function mapto01(x,x0,x1)
return (x-x0)/(x1-x0)
end
function mapfrom01(x,y0,y1)
return x*(y1-y0)+y0
end
function wavg(x0,x1,w=.5)
return (1-w)*x0+w*x1
end
function step(x,x0=0)
return x>x0 ? 1 : 0
end
function normalize(p)
len = sqrt(p.x^2 + p.y^2)
if len != 0
p = Point(p.x / len, p.y / len)
end
return p
end
function rotate(p,angle)
return Point(p.x*cos(angle)-p.y*sin(angle),p.x*sin(angle)+p.y*cos(angle))
end
function Base.:/(p1::Point, p2::Point)
return Point(p1.x / p2.x, p1.y / p2.y)
end
function Base.:-(num::Number, p::Point)
return Point(num - p.x, num - p.y)
end
function Base.:-(p1::Point, num::Number)
return Point(p1.x - num, p1.y - num)
end
function Base.isless(num::Number, p::Point)
return num < p.x && num < p.y
end
function Base.isless(p::Point, num::Number)
return p.x < num && p.y < num
end
function Base.abs(p::Point)
return Point(abs(p.x), abs(p.y))
end
function Base.isnan(p::Point)
return isnan(p.x) || isnan(p.y)
end
function Base.min(p1::Point, p2::Point)
return Point(min(p1.x, p2.x), min(p1.y, p2.y))
end
function Base.max(p1::Point, p2::Point)
return Point(max(p1.x, p2.x), max(p1.y, p2.y))
end
function randF(min,max)
return min+rand()*(max-min)
end
function randF(max)
return randF(0,max)
end
function randI(min,max)
return floor(randF(min,max))
end
function randI(max)
return randI(0,max)
end
function linspace(start, stop, len)
return range(start, stop=stop, length=len)
end
function meshgrid(x,y)
return [Point(x,yi) for yi in y],[Point(xi,y) for xi in x]
end
function meshgrid(x_points::Int, y_points::Int, x_range::Tuple{Float64, Float64}, y_range::Tuple{Float64, Float64})
x_lin = range(x_range[1], stop=x_range[2], length=x_points)
y_lin = range(y_range[1], stop=y_range[2], length=y_points)
x_grid = repeat(reshape(x_lin, 1, :), y_points, 1)
y_grid = repeat(y_lin, 1, x_points)
return x_grid, y_grid
end
function truncate(x)
return x>0 ? floor(x) : ceil(x)
end
function Base.round(x,step)
return round(x/step)*step
end
function parabola(x,root0=0,root1=1)
a = 1 / ((0.5 * (root0 + root1) - root0) * (0.5 * (root0 + root1) - root1))
return a*(x-root0)*(x-root1)/((root0-root1)^2)
end
function limit(x,x0,x1)
return min(max(x,x0),x1)
end
function limitMag(x,x0)
return limit(x,-x0,x0)
end
function norm(p::Point)
return normalize(p)
end
function cross(p1::Point,p2::Point)
return p1.x*p2.y-p1.y*p2.x
end
function dot(p1::Point,p2::Point)
return p1.x*p2.x+p1.y*p2.y
end
function sP(x,p) #Symmetric Power
return abs(x)^p*sign(x)
end
function heading(p::Point)
return atan(p.y,p.x)
end
function modulo(x,y)
return x-y*floor(x/y)
end
function Base.round(x::Point; digits::Integer=0)
return Point(round(x.x; digits=digits), round(x.y; digits=digits))
end
function smoothStep(x)
if x<0
return 0
elseif x>1
return 1
else
return 3*x^2-2*x^3
end
end
# function scaledEase(x,c=0)
# if c>0
# return x/(c*(1-x)+1)
# else
# return 1-(1-x)/(-c*(x)+1)
# end
# end
function scaledEase(x,c=0,x0=0,x1=1,y0=0,y1=1)
x = mapto01(x,x0,x1)
if c>0
return mapfrom01(x/(c*(1-x)+1),y0,y1)
else
return mapfrom01(1-(1-x)/(-c*(x)+1),y0,y1)
end
# return mapfrom01(scaledEase(mapto01(x,x0,x1),c),y0,y1)
end
function scaledStep(x,c=0,x0=0,x1=1,y0=0,y1=1)
x = mapto01(x,x0,x1)
y =0
if x>.5
y= scaledEase(x,c,.5,1,.5,1)
else
y= scaledEase(x,c,.5,0,.5,0)
end
y = mapfrom01(y,y0,y1)
# return mapfrom01(scaledStep(mapto01(x,x0,x1),c),y0,y1)
end
# function scaledStep(x,c=0)
# if x>.5
# # return mapfrom01(scaledEase(mapto01(x,.5,1),c),.5,1)
# return scaledEase(x,c,.5,1,.5,1)
# else
# # return mapfrom01(scaledEase(mapto01(x,.5,0),c),.5,0)
# return scaledEase(x,c,.5,0,.5,0)
# end
# end
function limitL(p::Point,min,max)
L=mag(p)
L==0 && return p
if L>max
return p*max/L
elseif L<min
return p*min/L
else
return p
end
end