# New to Java

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

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 is the starting point, or P0 in the above diagram
cp is the first control point, or P1 in the above diagram
cp is the second control point, or P2 in the above diagram
cp 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.x - cp.x);
bx = 3.0 * (cp.x - cp.x) - cx;
ax = cp.x - cp.x - cx - bx;

cy = 3.0 * (cp.y - cp.y);
by = 3.0 * (cp.y - cp.y) - cy;
ay = cp.y - cp.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.x;
result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp.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 );
}``````