-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathqbezier.cpp
More file actions
42 lines (34 loc) · 1.32 KB
/
qbezier.cpp
File metadata and controls
42 lines (34 loc) · 1.32 KB
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
#include "qbezier.h"
//http://www.codeproject.com/Articles/25237/Bezier-Curves-Made-Simple
//Check the general case. Notice I missed binominial coefficient when coding
QBezier::QBezier(QVector<QPointF> points, int steps)
{
int n = points.size()-1;
for(double t=0; t<=1; t+=1.0/steps) // t = [0,1] !!! not [0, 1) brainfail. I was thinking why the hell there are spaces.
{
QPointF newP;
for(int i=0; i<=n; i++)
{
newP.setX( newP.x() + points[i].x()*qPow(1.0-t,n-i)*qPow(t,i)*double(binomialCoefficient(n, i)) );
newP.setY( newP.y() + points[i].y()*qPow(1.0-t,n-i)*qPow(t,i)*double(binomialCoefficient(n, i)) );
}
/*QPointF p0 = points.at(0);
QPointF p1 = points.at(1);
QPointF p2 = points.at(2);
newP.setX( qPow((1.0-t),2.0)*p0.x()+2*t*(1.0-t)*p1.x()+qPow(t,2.0)*p2.x() );
newP.setY( qPow((1.0-t),2.0)*p0.y()+2*t*(1.0-t)*p1.y()+qPow(t,2.0)*p2.y() );*/
//this->setPoint(point++, newP);
myPolygon.push_back( newP );
}
//qDebug() << "Bezier: " << myPolygon << n;
}
int QBezier::binomialCoefficient(int n, int i)
{
return (factorial(n) / ( factorial(i)*factorial(n-i) ) );
}
int QBezier::factorial(int n)
{
if(n == 0)
return 1;
return n*factorial(n-1);
}