Class BezierUtil

java.lang.Object
de.tomatengames.util.BezierUtil

public class BezierUtil extends Object
Provides utilities to work with cubic Bézier curves.
Since:
1.2
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    eval(double x, double eps, double p1x, double p1y, double c1x, double c1y, double c2x, double c2y, double p2x, double p2y)
    Evaluates the cubic Bézier curve defined by the parameters p1, c1, c2, p2 at the specified x position.
    static double
    evalDeCasteljau(double t, double p1, double c1, double c2, double p2)
    Runs the De-Casteljau algorithm to evaluate the cubic Bézier curve defined by the parameters p1, c1, c2, p2 at t.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • evalDeCasteljau

      public static double evalDeCasteljau(double t, double p1, double c1, double c2, double p2)
      Runs the De-Casteljau algorithm to evaluate the cubic Bézier curve defined by the parameters p1, c1, c2, p2 at t.

      Note that this function only calculates one dimension of the Bézier curve. In general, Bézier curves are defined as 2-dimensional curves P(t)=[P.x(t), P.y(t)] with p1=[p1.x, p1.y], etc. To calculate P(t), this method can be called twice with the corresponding parameters of the respective dimension.

      P.x(t) = evalDeCasteljau(t, p1.x, c1.x, c2.x, p2.x)
      P.y(t) = evalDeCasteljau(t, p1.y, c1.y, c2.y, p2.y)
      
      Parameters:
      t - The point at which the curve is to be evaluated. Must be in the range from 0 to 1. t=0 returns p1 and t=1 returns p2.
      p1 - The start point of the curve.
      c1 - The control point of p1.
      c2 - The control point of p2.
      p2 - The end point of the curve.
      Returns:
      The evaluated point on the curve.
    • eval

      public static double eval(double x, double eps, double p1x, double p1y, double c1x, double c1y, double c2x, double c2y, double p2x, double p2y)
      Evaluates the cubic Bézier curve defined by the parameters p1, c1, c2, p2 at the specified x position.

      This method first tries to calculate the parameter t for the specified x position numerically. If the calculated t implies a x(t) with abs(x(t)-x) <= eps, this step is complete. Then y(t) is calculated and returned.

      The parameters must meet the following requirements:

      • p1 must be before p2 (p1x < p2x)
      • c1x must be in the range of p1x and p2x (p1x <= c1x <= p2x)
      • c2x must be in the range of p1x and p2x (p1x <= c2x <= p2x)
      • x must be in the range of p1x and p2x (p1x <= x <= p2x)
      If any of these requirements is not met, the result is undefined.
      Parameters:
      x - The x position that should be evaluated. Must be in the range of p1x and p2x.
      eps - The maximum error of x to abort numerical methods. Negative values have the same effect as 0. It is recommended to choose a value greater than 0 to improve performance. Note that it is not guaranteed that the error is less than eps. In special cases, e.g. eps=0, if it is not possible or involves a disproportionate effort to find a fitting t, eps may be ignored.
      p1x - The x position of the start point. Must be less than p2x.
      p1y - The y position of the start point.
      c1x - The x position of the control point of p1. Must be in the range of p1x and p2x.
      c1y - The y position of the control point of p1.
      c2x - The x position of the control point of p2. Must be in the range of p1x and p2x.
      c2y - The y position of the control point of p2.
      p2x - The x position of the end point. Must be greater than p1x.
      p2y - The y position of the end point.
      Returns:
      The y position of the curve at the specified x position.