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
|
struct Point
{
Point(double X, double Y) : x(X), y(Y) {}
double x,y;
void normalize()
{
double Length = sqrt((x * x) + (y * y));
if(Length != 0)
{
x /= Length;
y /= Length;
}
}
};
Point DeriveBezier(double t, Point &P1, Point &P2, Point& P3) //Derivative of quadratic Bezier
{
Point pt;
pt.x = ((pow(1-t,1) * 2) * (P2.x - P1.x)) + ((pow(1-t,0) * t) * (P3.x - P2.x));
pt.y = ((pow(1-t,1) * 2) * (P2.y - P1.y)) + ((pow(1-t,0) * t) * (P3.y - P2.y));
return pt;
}
Point Bezier(double t, Point &P1, Point &P2, Point& P3) // Creating quadratic Bezier
{
Point pt;
pt.x = (pow(1-t,2) * P1.x) + (2 * pow(1-t, 1) * t) * P2.x + (pow(t,2) * P3.x);
pt.y = (pow(1-t,2) * P1.y) + (2 * pow(1-t, 1) * t) * P2.y + (pow(t,2) * P3.y);
return pt;
}
void Create()
{
Point BezierPoint, BezierPoint2;
int W = 25;
//Doesn't need to be such a small number, just for testing
for(double t = 0; t < 1; t += 0.001)
{
Point Slope = DeriveBezier(t,P1,P2,P3); // Slope of current Point
Point Slope2 = DeriveBezier(t + 0.001,P1,P2,P3); // Slope of next point
//Get Normal to slope, then normalize
Slope = NormalVec(Slope);
Slope.normalize();
Slope2 = NormalVec(Slope2);
Slope2.normalize();
BezierPoint = Bezier(t, P1, P2, P3); // Point on graph
BezierPoint2 = Bezier(t + 0.001, P1, P2, P3); // Next point
glBegin(GL_QUADS);
glVertex2f(BezierPoint.x, BezierPoint.y);
glVertex2f(BezierPoint.x + Slope.x * W, BezierPoint.y + Slope.y * W);
glVertex2f(BezierPoint2.x + Slope2.x * W, BezierPoint2.y + Slope2.y * W);
glVertex2f(BezierPoint2.x, BezierPoint2.y);
glEnd();
glBegin(GL_QUADS);
glVertex2f(BezierPoint.x, BezierPoint.y);
glVertex2f(BezierPoint.x + Slope.x *- W, BezierPoint.y + Slope.y * -W);
glVertex2f(BezierPoint2.x + Slope2.x * -W, BezierPoint2.y + Slope2.y * -W);
glVertex2f(BezierPoint2.x, BezierPoint2.y);
glEnd();
Apply_Surface(BezierPoint.x, BezierPoint.y, Square, NULL); // Curve with no offset applied
}
}
|