-
Notifications
You must be signed in to change notification settings - Fork 4
/
BezierCurve.cs
289 lines (224 loc) · 8.16 KB
/
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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// MIT License
//
// Copyright (c) 2018 Guney Ozsan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// ---------------------------------------------------------------------
//
// Reference for mathematical formulas: "Bézier curve" from https://en.wikipedia.org/wiki/B%C3%A9zier_curve
using System;
// Use this if .NET and comment out Unity 3D (using UnityEngine):
// using System.Numerics;
// Use this if Unity 3D and comment out .NET (using System.Numerics):
using UnityEngine;
// Notation follows the Mathematical convention.
public static class BezierCurve
{
public static Vector2 Linear(Vector2 p0, Vector2 p1, float t)
{
// polynomial form (recursive)
return p0 + t*(p1 - p0);
// explicit form
// return (1 - t)*p0 + t*p1;
}
public static Vector2 Quadratic(Vector2 p0, Vector2 p1, Vector2 p2, float t)
{
// polynomial form (recursive)
return (1 - t)*(p0 + t*(p1 - p0)) + t*(p1 + t*(p2 - p1));
// Alternative recursive function:
// return (1 - t)*Linear(p0, p1, t) + t*Linear(p1, p2, t);
// explicit form
// return (1 - t)*(1 - t)*p0 + 2*(1 - t)*t*p1 + t*t*p2;
}
public static Vector2 Cubic(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t)
{
// polynomial form (recursive)
return (1 - t)*((1 - t)*(p0 + t*(p1 - p0)) + t*(p1 + t*(p2 - p1))) + t*((1 - t)*(p1 + t*(p2 - p1)) + t*(p2 + t*(p3 - p2)));
// Alternative recursive function:
// return (1 - t)*Quadratic(p0, p1, p2, t) + t*Quadratic(p1, p2, p3, t);
// explicit form
// return (1 - t)*(1 - t)*(1 - t)*p0 + 3*(1 - t)*(1 - t)*t*p1 + 3*(1 - t)*t*t*p2 + t*t*t*p3;
}
// Recursive definition
public static Vector2 Recursive(Vector2[] p, float t)
{
Vector2 bt = p[0];
if (p.Length > 1)
{
Vector2[] p1pn = new Vector2[p.Length - 1];
Array.Copy(p, 1, p1pn, 0, p.Length - 1);
// The following should be like this but skipped for optimization
// Vector2[] p0pnMinus1 = Array.Resize(ref p, p.Length - 1);
// bt = (1 - t)*Recursive(p0pnMinus1 , t) + t*Recursive(p1pn, t);
Array.Resize(ref p, p.Length - 1);
bt = (1 - t)*Recursive(p, t) + t*Recursive(p1pn, t);
}
return bt;
}
// Explicit definition
public static Vector2 General(Vector2[] p, float t)
{
Vector2 bt = Vector2.zero;
int n = p.Length - 1;
for (int i = 0; i <= n; i++)
{
bt += Combination(n, i)*Power(1 - t, n - i)*Power(t, i)*p[i];
}
return bt;
}
// Polynomial form (good for repetitive use).
// Prior to using this, calculate polynomial coefficients using PolynomialCoefficients() method,
// keep them in a Vector2 array c[] and pass the array here.
// If you really need an efficient algorithm, you can make a dictionary of PolynomialCoefficients.
public static Vector2 Polynomial(Vector2[] p, float t, Vector2[] c)
{
Vector2 bt = Vector2.zero;
for (int j = 0; j < p.Length; j++)
{
bt += Power(t, j)*c[j];
}
return bt;
}
public static class FirstDerivative
{
public static Vector2 Linear(Vector2 p0, Vector2 p1, float t)
{
return p1 - p0;
}
public static Vector2 Quadratic(Vector2 p0, Vector2 p1, Vector2 p2, float t)
{
return 2*(1 - t)*(p1 - p0) + 2*t*(p2 - p1);
}
public static Vector2 Cubic(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t)
{
return 3*(1 - t)*(1 - t)*(p1 - p0) + 6*(1 - t)*t*(p2 - p1) + 3*t*t*(p3 - p2);
}
}
public static class SecondDerivative
{
public static Vector2 Quadratic(Vector2 p0, Vector2 p1, Vector2 p2, float t)
{
return 2*(p2 - 2*p1 + p0);
}
public static Vector2 Cubic(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t)
{
return 6*(1 - t)*(p2 - 2*p1 + p0) + 6*t*(p3 - 2*p2 + p1);
}
}
// Parametric derivative order version for flexible use, less optimal.
public static class Derivative
{
public static Vector2 Linear(Vector2 p0, Vector2 p1, float t, int order)
{
switch (order)
{
case 1:
return p1 - p0;
break;
default:
return Vector2.zero;
}
}
public static Vector2 Quadratic(Vector2 p0, Vector2 p1, Vector2 p2, float t, int order)
{
switch (order)
{
case 2:
return 2*(p2 - 2*p1 + p0);
break;
case 1:
return 2*(1 - t)*(p1 - p0) + 2*t*(p2 - p1);
break;
default:
return Vector2.zero;
}
}
public static Vector2 Cubic(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t, int order)
{
switch (order)
{
case 3:
return 6*(p3 - 3*p2 + 3*p1 - p0);
break;
case 2:
return 6*(1 - t)*(p2 - 2*p1 + p0) + 6*t*(p3 - 2*p2 + p1);
break;
case 1:
return 3*(1 - t)*(1 - t)*(p1 - p0) + 6*(1 - t)*t*(p2 - p1) + 3*t*t*(p3 - p2);
break;
default:
return Vector2.zero;
}
}
}
// If you really need an efficient algorithm, you can make a dictionary of PolynomialCoefficients.
public static Vector2[] PolynomialCoefficients(Vector2[] p)
{
Vector2[] c = new Vector2[p.Length];
int n = p.Length - 1;
for (int j = 0; j <= n; j++)
{
c[j] = Vector2.one;
// Pi part
int pi = 1;
for (int m = 0; m <= j - 1; m++)
{
pi *= n - m;
}
// Sigma part
Vector2 sigma = Vector2.zero;
for (int i = 0; i <= j; i++)
{
sigma += (Power(-1, i + j)*p[i]) / (Factorial(i)*Factorial(j - i));
}
c[j] = pi*sigma;
}
return c;
}
public static int Combination(int n, int i)
{
return Factorial(n) / (Factorial(i)*Factorial(n - i));
}
public static int Factorial(int n)
{
int y = 1;
for (int i = 1; i <= n; i++)
{
y *= i;
}
return y;
}
public static float Power(float b, int n)
{
if (n == 0)
{
return 1;
}
else
{
float y = b;
for (int i = 1; i <= n - 1; i++)
{
y *= y;
}
return y;
}
}
}