Skip to content

Instantly share code, notes, and snippets.

@sjhalayka
Last active June 6, 2018 16:40
Show Gist options
  • Save sjhalayka/d86f5802355dc9bde73fb72dce950d58 to your computer and use it in GitHub Desktop.
Save sjhalayka/d86f5802355dc9bde73fb72dce950d58 to your computer and use it in GitHub Desktop.
// https://stackoverflow.com/questions/785097/how-do-i-implement-a-bézier-curve-in-c
vector_3 getBezierPoint(vector<vector_3> points, float t )
{
int i = points.size() - 1;
while (i > 0)
{
for (int k = 0; k < i; k++)
{
points[k].x = points[k].x + t * ( points[k+1].x - points[k].x );
points[k].y = points[k].y + t * ( points[k+1].y - points[k].y );
points[k].z = points[k].z + t * ( points[k+1].z - points[k].z );
}
i--;
}
return points[0];
}
// ...
vector<vector_3> pos;
vector_3 v0(-0.5, 0.5, 0);
vector_3 v1( 0.5, 0.5, 0);
vector_3 v2( 0.5, -0.5, 0);
vector_3 v3( -1.0, -1.0, 0);
pos.push_back(v0);
pos.push_back(v1);
pos.push_back(v2);
pos.push_back(v3);
vector<vector_3> v;
vector<vector_3> deriv;
for(float t = 0; t <= 1; t += 0.1)
{
vector_3 vline = getBezierPoint(pos, t);
v.push_back(vline);
if(t == 1)
deriv.push_back(getBezierPoint(pos, t - 0.001));
else
deriv.push_back(getBezierPoint(pos, t + 0.001));
}
glLineWidth(2.0f);
glBegin(GL_LINE_STRIP);
for(size_t i = 0; i < v.size(); i++)
glVertex3f(v[i].x, v[i].y, v[i].z);
glEnd();
glColor3f(1, 0.5, 0);
glBegin(GL_LINES);
for(size_t i = 0; i < deriv.size(); i++)
{
vector_3 line = deriv[i] - v[i];
line.normalize();
glVertex3f(v[i].x, v[i].y, v[i].z);
glVertex3f(v[i].x + line.x, v[i].y + line.y, v[i].z + line.z);
}
glEnd();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment