0

I need to create a random curve like this:

A random curve

In this picture there are 3 examples, marked as follows:

  1. The red oval is beginning of the curve and
  2. The blue oval shows the end.
  3. Between them are points marked with green ovals.

I only can use Point because after making one of these I need to create another small curve for each of the Point in previous curve. I tried with sin function or cos function but it work sonly if x or y point is the same for both start and end point.

Does anyone know how it's possible to this with Graphics2D and Point?

Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
Vuk Vasić
  • 1,398
  • 10
  • 27
  • 1
    Those curves aren't random. They're parametric. Lookup splines, in particular cubic Bézier splines, and perhaps Hermite interpolation. – Kevin A. Naudé Dec 18 '12 at 18:18
  • Well i need to create any type of Curve but Pixel by Pixel ( Point by Point) that is the problem . I can't write a code to make a points for example: Point[] p = {(X1,Y2),(X2,Y2) etc } Every point has a -1 or 1 pixel + last one and together they create a curve. – Vuk Vasić Dec 18 '12 at 18:23
  • Why not just just compute the points in a point-list? Then do whatever you like with them. The algorithm is recursive, but not too difficult. – Kevin A. Naudé Dec 18 '12 at 18:26
  • I don't know how the make algorithm :/ that is the point of this question . i am trying this for 5 days 10 hours a day and i can't get it to work. – Vuk Vasić Dec 18 '12 at 18:27
  • So basically you want to know how spline interpolation works and implement it by yourself? – Simon Dec 18 '12 at 18:29
  • I know how it works i don't know to implement it in java code. from point to another point. – Vuk Vasić Dec 18 '12 at 18:33
  • ok, see edit. Hope it helps – Simon Dec 18 '12 at 18:40

1 Answers1

3

You should use Bezier Curves.

If you to have a y-value for each x-value on your curve, then you could take a look at the apache commons implementation. What you get here is a PolynomialSplineFunction, which you can use to calculate any point on your curve.

SplineInterpolator interpolator = new SplineInterpolator();
PolynomialSplineFunction f 
         = interpolator.interpolate(xValues, yValues); //red and blue dots
f.value(4.0); //gets y-value for  x = 4.0
Simon
  • 9,255
  • 4
  • 37
  • 54
  • I can't use Bezier Curves . Please read the question . I need point by point or pixel by pixel creation of curve . If it was that simple i wouldn't ask. – Vuk Vasić Dec 18 '12 at 18:20
  • Well i need to create any type of Curve but Pixel by Pixel ( Point by Point) that is the problem . I can't write a code to make a points for example: Point[] p = {(X1,Y2),(X2,Y2) etc } Every point has a -1 or 1 pixel + last one and together they create a curve. – Vuk Vasić Dec 18 '12 at 18:24
  • Thanks for help . But this function are not existing in java – Vuk Vasić Dec 18 '12 at 18:53
  • You are right. They are no included in the jdk, but you can download the apache common math library from http://commons.apache.org/math/. – Simon Dec 18 '12 at 18:55
  • Yea . But i can't use it it is a school project . They not allow as to use liberies :/ thank you anyway :) – Vuk Vasić Dec 18 '12 at 18:57
  • @VukVasi It is definitely ok to ask questions about homework/school projects on Stack Overflow. To learn the most from doing so, you can follow the community wiki/guidelines that are published here: http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions -- best wishes – Kevin A. Naudé Dec 18 '12 at 19:14
  • PolynomialSplineFunction is a one dimensional function - putting in the x values for middle curve which are not increasing will have odd effects. You probably need to use a parametric value `v` and express `x` and `y` as two functions of `v` rather than expressing `y` as function of `x` – Pete Kirkham Dec 20 '12 at 10:59