-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_3.cpp
48 lines (40 loc) · 1.15 KB
/
01_3.cpp
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
#include "01_3.h"
static Point2D deCasteljau(Point2D* points, int numPoints, float t);
/*
Given an array of points, evaluate a point of the Bézier curve in t.
*/
static Point2D deCasteljau(Point2D* points, int numPoints, float t)
{
// Auxiliary array (since we edit it in each iteration, we don't want to overwrite and lose the initial array)
Point2D pointArrayAux[MAX_NUM_PTS] = { 0.0f, 0.0f };
// Copy to the auxiliary array
for (int i = 0; i < numPoints; i++)
{
pointArrayAux[i] = points[i];
}
// "Depth" loop
for (int i = 1; i < numPoints; i++)
{
// Points loop
for (int j = 0; j < numPoints - 1; j++)
{
pointArrayAux[j] = lerp(pointArrayAux[j], pointArrayAux[j + 1], t);
}
}
return pointArrayAux[0];
}
/*
Given numCtrl control points, return numCurve points
representing the ends of the segments forming a curve
obtained using "de Casteljau algorithm".
*/
void getCurve_deCasteljau(Point2D* ctrlPts, int numCtrl, Point2D* curvePts, int* numCurve)
{
if (numCtrl < 2)
return;
*numCurve = MIN(100, MAX(MAX_NUM_PTS, numCtrl * 2));
for (int i = 0; i < *numCurve; i++)
{
curvePts[i] = deCasteljau(ctrlPts, numCtrl, (float)i / *numCurve);
}
}