I've created a set of control points, and have used cubic interpolation to fit a curve to it. You can see the result here:

http://www.robsdot.co.uk/cubicInterp.gif

As is clear in the image, the 'curve' isn't very curvy. There are some sharp turns at some of the control points.

(Where the red points are the endpoints of the curve).

This is how I render the lines (Java btw);

for ( int i=1; i<cs.size()-2; i++ )

{

for ( double u=0.0; u<=0.9; u+=0.1 )

{

int x = cs.x( i, u ) ;

int y = cs.y( i, u ) ;

int x2 = cs.x( i, u+0.1 ) ;

int y2 = cs.y( i, u+0.1 ) ;

g.drawLine( x, y, x2, y2 ) ;

}

}

Where the 'cs' instance merely wraps round an ArrayList, and the x() and y() methods are:

public int y( int i, double x )

{

int P0, P1, P2, P3 ;

P0 = get( i-1 ).y ;

P1 = get( i ).y ;

P2 = get( i+1 ).y ;

P3 = get( i+2 ).y ;

int P = (P3 - P2) - (P0 - P1) ;

int Q = (P0 - P1) - P ;

int R = P2 - P0 ;

int S = P1 ;

return (int ) (P*x*x*x + Q*x*x + R*x + S) ;

}

public int x( int i, double x )

{

return (int ) (get( i ).x + x * (get( i+1 ).x-get( i ).x) ) ;

}

And the get(int i) method returns a Point instance of the control point 'i'.

Can anyone spot a problem?

Why is it not c-u-r-v-y ?

Cheers

http://www.robsdot.co.uk/cubicInterp.gif

As is clear in the image, the 'curve' isn't very curvy. There are some sharp turns at some of the control points.

(Where the red points are the endpoints of the curve).

This is how I render the lines (Java btw);

for ( int i=1; i<cs.size()-2; i++ )

{

for ( double u=0.0; u<=0.9; u+=0.1 )

{

int x = cs.x( i, u ) ;

int y = cs.y( i, u ) ;

int x2 = cs.x( i, u+0.1 ) ;

int y2 = cs.y( i, u+0.1 ) ;

g.drawLine( x, y, x2, y2 ) ;

}

}

Where the 'cs' instance merely wraps round an ArrayList, and the x() and y() methods are:

public int y( int i, double x )

{

int P0, P1, P2, P3 ;

P0 = get( i-1 ).y ;

P1 = get( i ).y ;

P2 = get( i+1 ).y ;

P3 = get( i+2 ).y ;

int P = (P3 - P2) - (P0 - P1) ;

int Q = (P0 - P1) - P ;

int R = P2 - P0 ;

int S = P1 ;

return (int ) (P*x*x*x + Q*x*x + R*x + S) ;

}

public int x( int i, double x )

{

return (int ) (get( i ).x + x * (get( i+1 ).x-get( i ).x) ) ;

}

And the get(int i) method returns a Point instance of the control point 'i'.

Can anyone spot a problem?

Why is it not c-u-r-v-y ?

Cheers

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

{

int P0, P1, P2, P3 ;

P0 = get( i-1 ).x ;

P1 = get( i ).x ;

P2 = get( i+1 ).x ;

P3 = get( i+2 ).x;

int P = (P3 - P2) - (P0 - P1) ;

int Q = (P0 - P1) - P ;

int R = P2 - P0 ;

int S = P1 ;

return (int ) (P*x*x*x + Q*x*x + R*x + S) ;

}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialI last tried this out with just 1D interpolation - and kinda carried my x() function into 2d. Oops.

Math / Science

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

public int y( int i, double x )

{

int P0, P1, P2, P3 ;

P0 = get( i-1 ).x ;

P1 = get( i ).x ;

P2 = get( i+1 ).x ;

P3 = get( i+2 ).x;

int P = (P3 - P2) - (P0 - P1) ;

int Q = (P0 - P1) - P ;

int R = P2 - P0 ;

int S = P1 ;

return (int ) (P*x*x*x + Q*x*x + R*x + S) ;

}