Forum Stats

  • 3,872,756 Users
  • 2,266,468 Discussions
  • 7,911,296 Comments

Discussions

Help with Bezier curve algorithm, n amount of points?

807599
807599 Member Posts: 49,986
edited Nov 23, 2006 3:10AM in New To Java
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 );
}
«1

Comments

  • 807599
    807599 Member Posts: 49,986
    i bet google knows!

    http://64.233.161.104/search?q=cache:7PVVDJ5XUmQJ:www.people.nnov.ru/fractal/Splines/Bezier.htm+Bezier+curve+algorithm+java&hl=en&gl=us&ct=clnk&cd=1
  • 807599
    807599 Member Posts: 49,986
    I have searched google for days for an easy way that makes sense to me. thats why I posted here. I know how to do it by hand, just cant figure out logically to put it as code.
  • 807599
    807599 Member Posts: 49,986
    i dont know anyhting about the Bezier curve algorithm, but the link i gave you has source code that does it. what is wrong with the source it provides?
  • 807599
    807599 Member Posts: 49,986
    You say you want to do one thing and then ask for help in translating a piece of code which does something different. Do you want to do Bezier curves with n control points, or find n points on a cubic (i.e. 4 control point) curve?
  • 807599
    807599 Member Posts: 49,986
    sorry about that. Yeah, I want to write up a hard coded app that can take n amount of control points.

    just the normal bezier curve. We are supposed to test with 4 control points then 5, 6, 7, 8, 9. I guess he wants us to see how smoother it looks.

    thanks again.
  • 807599
    807599 Member Posts: 49,986
    Please give your working definition of a control point, because I'm still not certain what you want - it seems that you may be using the term differently from all the literature.
  • 807599
    807599 Member Posts: 49,986
    here is what I need to do..

    "Use the Bezier curve algortihm to approximate a curve for the control points. Your driver program should draw the curves for many different cases of control points"

    I am using the term incorrectly--I guess.

    thanks again.
  • 807599
    807599 Member Posts: 49,986
    Synthesising your various posts then, am I correct in understanding the following to be your task?

    1. Write code to draw an approximation to a cubic Bezier curve by evaluating the positions of n points equally spaced in parameter space.
    2. Write test cases demonstrating it for various numbers of interpolated points and various interesting/degenerate curves.

    If I am, then the code you posted will do half of step 1 with very minor changes. Replace the typedef struct with a class having two fields, the pointers cp with Point2D[]s having 4 elements, the pointer curve with a Point2D[], and the argument numberOfPoints with curve.length. Allocate result a new Point2D() rather than merely declaring it, and I think the rest works.

    For the rendering, which would be the actual thing you claim in post 1 to be a problem (being the part which OpenGL would have played), a GeneralPath on which you call moveto followed by a series of lineto will be perfectly suited. In fact you could then optimise the code to ditch curve and simply start with a new GeneralPath().
  • 807599
    807599 Member Posts: 49,986
    yes you are correct. I tried to make the struct a class but the interpreter doesn't like the point2d[].

    I think my problem is, How do I use a class within a class.

    is there an easier way to do this than from the code above?

    thanks
This discussion has been closed.