programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Liutauras Vilda
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Paul Clapham
• Knute Snortum
• Rob Spoor
Saloon Keepers:
• Tim Moores
• Ron McLeod
• Piet Souris
• Stephan van Hulst
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Ganesh Patekar

# drawing a curve from points

Greenhorn
Posts: 10
I have a little question about how to make curves out of points.

My function has many points and I have to get them to one curve.
At the moment, it is just necessary to put a line(/path) from point to point.

I thought about using the GeneralPath - class, but I don't know how to use it.

Maybe someone can tell me which class to use is the best for this problem?

author and iconoclast
Posts: 24203
43
This question would get a lot more attention in the Swing/AWT forum, so I'm going to move it there.

Ranch Hand
Posts: 40
Hi,

I am a relative newcomer to this sort of stuff, but here's what I'm thinking:

First, I don't know of any class that would do what you want to do.

However, here is what I would start with: You have a collection of points, probably in two arrays, int xPoints[] and int yPoints[], or maybe just in one array, points coords[]. If you want to make a smooth looking curve with this set of points, you need to focus on what determines the slope of the curve. That leaves you with two options: (1) you could try to use the drawArc method in Graphics class, fitting it to your points, or (2) you could draw your own curve based on the slope of the curve at the last endpoint and then the slope at the next endpoint.

I think the second strategy would be more precise, yet slower and more tedious to code, whereas the first may be easier to implement, but you won't have as much control.

If you could be more specific, that would be helpful.

Hope I haven't been too rambling.

Ryan Smith
Ranch Hand
Posts: 40
Oh, I just re-read your post, and all you need is lines connecting the points! Ignore the above post in that case!

What I would do is use Graphics's drawLine method, implemented in a for loop.

For example:

[ July 31, 2004: Message edited by: Ryan Smith ]

Ranch Hand
Posts: 1535
Here's one way to connect the points with a curve. There may be many other or better ways. This takes five clicks on the drawing surface to get started.

The challenge is to figure out where/how to locate the control point for the quadTo method. With curveTo you have two control points to figure out.

[ August 01, 2004: Message edited by: Craig Wood ]

Greenhorn
Posts: 10
thanks, but I think this is not exactly what I need...

To the first answer from Ryan Smith :

The second post from you could maybe work, if I alter it.

I have already a curve that connects the Points together with
lines. So far is everything fine.

The problem is that you have to switch in the program how to display the curve.
So I have also to display the curve with rounded lines, and also as stairs.

To the answer from Craig Wood :

This seems to be a better idea (to work with a Path and the quadTo/curveTo- method) , but your code does not round the lines in the way I need it.
As I said, the problem is to connect the points in a way, that there is a bended line, that displays the increase/decrease of the data.

I think your curves are something like the Bezier-curves....

If I would know how to get the right control-points for my curve, the problem would be solved, I think...
[ August 02, 2004: Message edited by: Raschin Ghanad ]

 It is sorta covered in the JavaRanch Style Guide.