Repository navigation
Expand file tree
/
Copy pathMaths.lua
More file actions
120 lines (100 loc) · 2.5 KB
/
Copy pathMaths.lua
File metadata and controls
120 lines (100 loc) · 2.5 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
120
sqrt,abs,sin,cos,tan,pi,min,max,floor=math.sqrt,math.abs,math.sin,math.cos,math.tan,math.pi,math.min,math.max,math.floor
rand,seed=math.random,math.randomseed
function round(a)
return floor(a+0.5)
end
function remap( x, t1, t2, s1, s2 )
local f = ( x - t1 ) / ( t2 - t1 )
return f * ( s2 - s1 ) + s1
end
function clamp(x, l, h)
if x<l then return l elseif x>h then return h else return x end
end
function lerp(a,b,r)
return a*(1-r)+b*r
end
function cuberp(a,b,c,d,t)
local A=d-c-a+b
local B=a-b-A
local C=c-a
local D=b
local T=t*t
return A*t*T+B*T+C*t+D
end
function overlap(x,y,x0,x1,y0,y1)
if x<x0 or x>x1 or y<y0 or y>y1 then return false end
return true
end
function distance(x0,y0,x1,y1)
local dx=x0-x1
local dy=y0-y1
return sqrt(dx*dx+dy*dy)
end
function rad2deg( r )
return r/math.pi*180
end
function deg2rad( d )
return d/180*math.pi
end
function cubicBezier(t, p0, p1, p2, p3)
return (1 - t)^3*p0 + 3*(1 - t)^2*t*p1 + 3*(1 - t)*t^2*p2 + t^3*p3
end
function cubicBezier2(t,x0,y0,x1,y1,x2,y2,x3,y3)
return cubicBezier(t,x0,x1,x2,x3),cubicBezier(t,y0,y1,y2,y3)
end
-- ---------------------------------------------------------------------
-- Vector2
-- ---------------------------------------------------------------------
function V2Sub(v1,v2) return {v1[1]-v2[1],v1[2]-v2[2]} end
function V2Dot(v1,v2) return v1[1]*v2[1]+v1[2]*v2[2] end
function V2SqLength(v) return v[1]*v[1]+v[2]*v[2] end
-- ---------------------------------------------------------------------
-- CatmullRom
-- https://iquilezles.org/articles/minispline/
-- keys format : spline ={0,x0,y0,1,x1,y1,2,x2,y2,3,x3,y3}
-- ---------------------------------------------------------------------
CatmullRomCoefs = {
{ -1, 2,-1, 0},
{ 3,-5, 0, 2},
{ -3, 4, 1, 0},
{ 1,-1, 0, 0} }
function CatmullRom(keys, dim, t)
-- init result
local v = {} -- out
for i=1,dim do
v[i] = 0
end
local size = dim + 1;
local num =floor(#keys/size)
-- find key
local k = 0
while k<num and keys[1+k*size]<t do
k=k+1;
end
-- interpolant
local h
if k<=0 then
h=0
elseif k>=num then
h=1
elseif k>0 then
local t1=keys[1+(k-1)*size]
local t2=keys[1+k*size]
h=(t-t1)/(t2-t1)
end
-- add basis functions
for i=1,4 do
local kn = k+i-3;
if kn<0 then
kn=0
elseif kn>(num-1) then
kn=num-1
end
local co=CatmullRomCoefs[i]
local b = 0.5*(((co[1]*h + co[2])*h + co[3])*h + co[4]);
for j=1,dim do
v[j] = v[j]+ b*keys[kn*size+j+1]
end
end
return v
end