-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBezierCurve.cs
39 lines (30 loc) · 993 Bytes
/
BezierCurve.cs
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
namespace JFUtils;
[Serializable]
public class BezierCurve
{
public Vector3[] Points;
public BezierCurve() { Points = new Vector3[4]; }
public BezierCurve(Vector3[] Points) { this.Points = Points; }
public Vector3 StartPosition => Points[0];
public Vector3 EndPosition => Points[3];
// Equations from: https://en.wikipedia.org/wiki/B%C3%A9zier_curve
public Vector3 GetSegment(float Time)
{
Time = Clamp01(Time);
var time = 1 - Time;
return time * time * time * Points[0]
+ 3 * time * time * Time * Points[1]
+ 3 * time * Time * Time * Points[2]
+ Time * Time * Time * Points[3];
}
public Vector3[] GetSegments(int Subdivisions)
{
var segments = new Vector3[Subdivisions];
for (var i = 0; i < Subdivisions; i++)
{
var time = (float)i / Subdivisions;
segments[i] = GetSegment(time);
}
return segments;
}
}