Class Interpolator.BezierInterpolator

  • Enclosing class:
    Interpolator

    public static class Interpolator.BezierInterpolator
    extends Interpolator
    Bezier curve interpolator.

    Basically, a cubic Bezier curve is created with start point (0,0) and endpoint (1,1). The other two control points (px1, py1) and (px2, py2) are given by the user, where px1, py1, px1, and px2 are all in the range [0,1].

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean isCurveLinear
      do the input control points form a line with (0,0) and (1,1), i.e., x1 == y1 and x2 == y2 -- if so, then all x(t) == y(t) for the curve
      private static float SAMPLE_INCREMENT
      difference in t used to calculate each of the xSamples values -- power of 2 sample size should provide exact representation of this value and its integer multiples (integer in range of [0..SAMPLE_SIZE]
      private static int SAMPLE_SIZE
      power of 2 sample size for lookup table of x values
      private float x1
      the coordinates of the 2 2D control points for a cubic Bezier curve, with implicit start point (0,0) and end point (1,1) -- each individual coordinate value must be in range [0,1]
      private float x2
      the coordinates of the 2 2D control points for a cubic Bezier curve, with implicit start point (0,0) and end point (1,1) -- each individual coordinate value must be in range [0,1]
      private float[] xSamples
      x values for the bezier curve, sampled at increments of 1/SAMPLE_SIZE -- this is used to find the good initial guess for parameter t, given an x
      private float y1
      the coordinates of the 2 2D control points for a cubic Bezier curve, with implicit start point (0,0) and end point (1,1) -- each individual coordinate value must be in range [0,1]
      private float y2
      the coordinates of the 2 2D control points for a cubic Bezier curve, with implicit start point (0,0) and end point (1,1) -- each individual coordinate value must be in range [0,1]
    • Constructor Summary

      Constructors 
      Constructor Description
      BezierInterpolator​(float px1, float py1, float px2, float py2)
      constructor -- cubic bezier curve will be represented by control points (0,0) (px1,py1) (px2,py2) (1,1) -- px1, py1, px2, py2 all in range [0,1]
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(Object o)  
      private float eval​(float t, float p1, float p2)
      use Bernstein basis to evaluate 1D cubic Bezier curve (quicker and more numerically stable than power basis) -- 1D control coordinates are (0, p1, p2, 1), where p1 and p2 are in range [0,1], and there is no ordering constraint on p1 and p2, i.e., p1 <= p2 does not have to be true
      private float evalDerivative​(float t, float p1, float p2)
      evaluate Bernstein basis derivative of 1D cubic Bezier curve, where 1D control points are (0, p1, p2, 1), where p1 and p2 are in range [0,1], and there is no ordering constraint on p1 and p2, i.e., p1 <= p2 does not have to be true
      private float findTForX​(float x)
      find the parameter t that produces the given x-value for the curve -- uses Newton-Raphson to refine the value as opposed to subdividing until we are within some tolerance
      Point2D getControl1()  
      Point2D getControl2()  
      private float getInitialGuessForT​(float x)
      find an initial good guess for what parameter t might produce the x-value on the Bezier curve -- uses linear interpolation on the x-value sample array that was created on construction
      int hashCode()  
      float interpolate​(float x)
      get the y-value of the cubic bezier curve that corresponds to the x input
    • Field Detail

      • SAMPLE_SIZE

        private static final int SAMPLE_SIZE
        power of 2 sample size for lookup table of x values
        See Also:
        Constant Field Values
      • SAMPLE_INCREMENT

        private static final float SAMPLE_INCREMENT
        difference in t used to calculate each of the xSamples values -- power of 2 sample size should provide exact representation of this value and its integer multiples (integer in range of [0..SAMPLE_SIZE]
        See Also:
        Constant Field Values
      • x1

        private final float x1
        the coordinates of the 2 2D control points for a cubic Bezier curve, with implicit start point (0,0) and end point (1,1) -- each individual coordinate value must be in range [0,1]
      • y1

        private final float y1
        the coordinates of the 2 2D control points for a cubic Bezier curve, with implicit start point (0,0) and end point (1,1) -- each individual coordinate value must be in range [0,1]
      • x2

        private final float x2
        the coordinates of the 2 2D control points for a cubic Bezier curve, with implicit start point (0,0) and end point (1,1) -- each individual coordinate value must be in range [0,1]
      • y2

        private final float y2
        the coordinates of the 2 2D control points for a cubic Bezier curve, with implicit start point (0,0) and end point (1,1) -- each individual coordinate value must be in range [0,1]
      • isCurveLinear

        private final boolean isCurveLinear
        do the input control points form a line with (0,0) and (1,1), i.e., x1 == y1 and x2 == y2 -- if so, then all x(t) == y(t) for the curve
      • xSamples

        private final float[] xSamples
        x values for the bezier curve, sampled at increments of 1/SAMPLE_SIZE -- this is used to find the good initial guess for parameter t, given an x
    • Constructor Detail

      • BezierInterpolator

        public BezierInterpolator​(float px1,
                                  float py1,
                                  float px2,
                                  float py2)
        constructor -- cubic bezier curve will be represented by control points (0,0) (px1,py1) (px2,py2) (1,1) -- px1, py1, px2, py2 all in range [0,1]
        Parameters:
        px1 - is x-coordinate of first control point, in range [0,1]
        py1 - is y-coordinate of first control point, in range [0,1]
        px2 - is x-coordinate of second control point, in range [0,1]
        py2 - is y-coordinate of second control point, in range [0,1]
    • Method Detail

      • getControl1

        public Point2D getControl1()
      • getControl2

        public Point2D getControl2()
      • interpolate

        public float interpolate​(float x)
        get the y-value of the cubic bezier curve that corresponds to the x input
        Specified by:
        interpolate in class Interpolator
        Parameters:
        x - is x-value of cubic bezier curve, in range [0,1]
        Returns:
        corresponding y-value of cubic bezier curve -- in range [0,1]
      • eval

        private float eval​(float t,
                           float p1,
                           float p2)
        use Bernstein basis to evaluate 1D cubic Bezier curve (quicker and more numerically stable than power basis) -- 1D control coordinates are (0, p1, p2, 1), where p1 and p2 are in range [0,1], and there is no ordering constraint on p1 and p2, i.e., p1 <= p2 does not have to be true
        Parameters:
        t - is the paramaterized value in range [0,1]
        p1 - is 1st control point coordinate in range [0,1]
        p2 - is 2nd control point coordinate in range [0,1]
        Returns:
        the value of the Bezier curve at parameter t
      • evalDerivative

        private float evalDerivative​(float t,
                                     float p1,
                                     float p2)
        evaluate Bernstein basis derivative of 1D cubic Bezier curve, where 1D control points are (0, p1, p2, 1), where p1 and p2 are in range [0,1], and there is no ordering constraint on p1 and p2, i.e., p1 <= p2 does not have to be true
        Parameters:
        t - is the paramaterized value in range [0,1]
        p1 - is 1st control point coordinate in range [0,1]
        p2 - is 2nd control point coordinate in range [0,1]
        Returns:
        the value of the Bezier curve at parameter t
      • getInitialGuessForT

        private float getInitialGuessForT​(float x)
        find an initial good guess for what parameter t might produce the x-value on the Bezier curve -- uses linear interpolation on the x-value sample array that was created on construction
        Parameters:
        x - is x-value of cubic bezier curve, in range [0,1]
        Returns:
        a good initial guess for parameter t (in range [0,1]) that gives x
      • findTForX

        private float findTForX​(float x)
        find the parameter t that produces the given x-value for the curve -- uses Newton-Raphson to refine the value as opposed to subdividing until we are within some tolerance
        Parameters:
        x - is x-value of cubic bezier curve, in range [0,1]
        Returns:
        the parameter t (in range [0,1]) that produces x
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object