• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

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
0
InteractiveMind
Asked:
InteractiveMind
  • 2
1 Solution
 
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) ;
}
0
 
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) ;
}
0
 
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.

0
 
InteractiveMindAuthor Commented:
Seems so obvious now :)


I last tried this out with just 1D interpolation - and kinda carried my x() function into 2d. Oops.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now