#include <iostream>
#include "Point2d.h"
#include "Collection.h"
#include "Coordinates2d.h"
#include "graphics.h"
#define PRECISION 0.01
Point2d Approximate(double t, Point2d const & pt1, Point2d const & pt2)
{
double x = pt1.x * (1-t) + pt2.x * t;
double y = pt1.y * (1-t) + pt2.y * t;
return Point2d(x, y);
}
Collection<Point2d> GetBezierPoints(Collection<Point2d> const & points)
{
Collection<Point2d> bezierPoints;
for(double t = 0 ; t < 1 ; t = t + PRECISION)
{
Collection<Point2d> temp1 = points;
while(temp1.size()>1)
{
Collection<Point2d> temp2;
for(int i = 0 ; i<temp1.size()-1 ; i++)
{
Point2d pt1 = temp1[i];
Point2d pt2 = temp1[i+1];
temp2.Add(Approximate(t, pt1, pt2));
}
temp1 = temp2;
}
bezierPoints.Add(temp1[0]);//finally only one point will be left
}
return bezierPoints;
}
int main()
{
Coordinates2d::ShowWindow("De Casteljau's algorithm");
Collection<Point2d> points;
points.Add(Point2d(0,0));
points.Add(Point2d(80,300));
points.Add(Point2d(120,-150));
points.Add(Point2d(200,0));
Collection<Point2d> list = GetBezierPoints(points);
Coordinates2d::Draw(list);
Coordinates2d::Wait();
return 0;
}
NOTE: I am in love with my Collection<T>
class, which is a wrapper around std::vector
.
PRECISION
constant, which would be better as aconst double
to give it an actual type. \$\endgroup\$Collection<>
that you would want to use it over astd::vector<>
? \$\endgroup\$