# New to Java

## Help with Bezier curve algorithm, n amount of points?

Nov 16 2006 — edited Nov 23 2006
Hello,

I have tried to follow my graphics book but its way to complicated. The book uses openGL. I want to do it in java.

I need to use n amount of control points. I am going to use an applet, and basically hard code the control points in the program. Nothing fancy.

I tried to change this c code to java format, but I cant get it to work at all.
Does any one have an algorithm already written, or help me improve this one.

thanks.
``````/*
Code to generate a cubic Bezier curve
*/

typedef struct
{
float x;
float y;
}
Point2D;

/*
cp is a 4 element array where:
cp[0] is the starting point, or P0 in the above diagram
cp[1] is the first control point, or P1 in the above diagram
cp[2] is the second control point, or P2 in the above diagram
cp[3] is the end point, or P3 in the above diagram
t is the parameter value, 0 <= t <= 1
*/

Point2D PointOnCubicBezier( Point2D* cp, float t )
{
float   ax, bx, cx;
float   ay, by, cy;
float   tSquared, tCubed;
Point2D result;

/* calculate the polynomial coefficients */

cx = 3.0 * (cp[1].x - cp[0].x);
bx = 3.0 * (cp[2].x - cp[1].x) - cx;
ax = cp[3].x - cp[0].x - cx - bx;

cy = 3.0 * (cp[1].y - cp[0].y);
by = 3.0 * (cp[2].y - cp[1].y) - cy;
ay = cp[3].y - cp[0].y - cy - by;

/* calculate the curve point at parameter value t */

tSquared = t * t;
tCubed = tSquared * t;

result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;

return result;
}

/*
ComputeBezier fills an array of Point2D structs with the curve
points generated from the control points cp. Caller must
allocate sufficient memory for the result, which is
<sizeof(Point2D) numberOfPoints>
*/

void ComputeBezier( Point2D* cp, int numberOfPoints, Point2D* curve ) {
float   dt;
int   i;

dt = 1.0 / ( numberOfPoints - 1 );

for( i = 0; i < numberOfPoints; i++)
curve[i] = PointOnCubicBezier( cp, i*dt );
}``````