/** * Bezier curve drawing * * Michael Sherman * msherman@dsbox.com */ #define STEP 0.1 #define EPSILON 0.0001 extern void DrawLine(int x1, int y1, int x2, int y2); typedef struct pnt { int x; int y; int z; } Point; /** * Do matrix multiplication to get the point on the curve by the parameter * u by multiplying the [ u^3 u^2 u 1 ] vector my the Bezier basis matrix, * then multiply by the matrix of control points given by [p0 p1 p2 p3]^T. */ Point Q(float u, Point p0, Point p1, Point p2, Point p3) { Point temp; float S1, S2, S3, S4; S1 = -(u*u*u) + 3*(u*u) - 3*(u) + 1; S2 = 3*(u*u*u) - 6*(u*u) + 3*(u); S3 = -3*(u*u*u) + 3*(u*u); S4 = (u*u*u); temp.x = S1*p0.x + S2*p1.x + S3*p2.x + S4*p3.x; temp.y = S1*p0.y + S2*p1.y + S3*p2.y + S4*p3.y; /** * Do the same thing with temp.z if you need 3 dimenions: * temp.z = S1*p0.z + S2*p1.z + S3*p2.z + S4*p3.z; */ return temp; } /** * Draw the curve */ void DrawCurve() { float u; Point p0, p1, p2, p3; Point new_p, old_p; /** * p0 = control point 1 * p1 = control point 2 * p2 = control point 3 * p3 = control point 4 */ u=0.0; new_p = p0; while (u <= 1.0 + EPSILON) { old_p = new_p; new_p = Q(u, p0, p1, p2, p3); DrawLine(old_p.x, old_p.y, new_p.x, new_p.y); u += STEP; } } /* * Wow, that was easy! * End of spline.c */