Search code examples
javamathgraphics2dsplines

Random curve made by points only


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?


Solution

  • 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