Cubic interpolation - not so curvy

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
LVL 25
InteractiveMindAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

ozoCommented:
Why doesn't public int x( int i, double x ) look more like public int y( int i, double x )?

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) ;
}
ozoCommented:
public int x( 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) ;
}

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 trial
NovaDenizenCommented:
yeah, your x(i,x) function just does linear interpolation of x coordinates.  When you want to do 2-d cubic interpolation like this, you have to cubically interpolate both the x-coordinate and the y-coordinate.

InteractiveMindAuthor Commented:
Seems so obvious now :)


I last tried this out with just 1D interpolation - and kinda carried my x() function into 2d. Oops.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Math / Science

From novice to tech pro — start learning today.