Learn how to a build a cloud-first strategyRegister Now

x
Solved

Cubic interpolation - not so curvy

Posted on 2006-04-20
Medium Priority
246 Views
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
Question by:InteractiveMind
• 2

LVL 85

Expert Comment

ID: 16501713
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

LVL 85

Accepted Solution

ozo earned 2000 total points
ID: 16501753
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

LVL 22

Expert Comment

ID: 16501808
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

LVL 25

Author Comment

ID: 16502333
Seems so obvious now :)

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

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Win a Jar of Candy Corn: A Scientific Approach! I love mathematics. If you love mathematics also, you may enjoy this tip on how to use math to win your own jar of candy corn and to impress your friends. As I said, I love math, but I gu…
Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, an…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses
Course of the Month20 days, 19 hours left to enroll