Given a spline, how do I find Y value for a given X?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
blueser
Posts: 23
Joined: Wed Sep 14, 2005 5:05 pm

Given a spline, how do I find Y value for a given X?

Post by blueser » Thu Jun 12, 2008 12:54 pm

Hi,

we're using splines on some of our plots, and this is working beautifully so far (this new feature arrived just in time with 1.0.9 ;-)). However, now we will need to find what's the Y-value corresponding to a specific X-value given a spline curve, where this X-value doesn't belong to one of the control points.

Is it possible at all (even if I need to extend XYSplineRenderer)?

Thks,

Andre

RichardWest
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Re: Given a spline, how do I find Y value for a given X?

Post by RichardWest » Thu Jun 12, 2008 7:01 pm

blueser wrote:Is it possible at all (even if I need to extend XYSplineRenderer)?
I cannot think of an easy way even if you were to extend the XYSlineRenderer. One of the demo applications shows how you can get the position of the mouse cursor over the plot both in Java2D pixel-space and in JFreeChart data-space. The demo source code would have come with the developer's guide, but I believe some forum posts have examples of how to explicity do this. Not an elegent solution, but it would work.
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA

krheinwald
Posts: 15
Joined: Thu Mar 27, 2003 10:06 am

Re: Given a spline, how do I find Y value for a given X?

Post by krheinwald » Sat Jun 14, 2008 11:30 am

blueser wrote:we need to find what's the Y-value corresponding to a specific X-value given a spline curve, where this X-value doesn't belong to one of the control points.
My first approach would be to copy the code for drawPrimaryLineAsPath(...), throw out all the drawing related stuff. Check whether your X is within <precision> to the temporary points in the most inner loop and return the calculated Y.

I think you can skip the most inner loop for x > X for improved performance.

Don't forget to feed back your code!

HTH,
Klaus

Locked