Class CubicCurves

java.lang.Object
org.jhotdraw8.geom.CubicCurves

public class CubicCurves extends Object
Provides utility methods for Bézier curves.
Author:
Werner Randelshofer
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    arcLength(double[] p, int offset, double epsilon)
    Computes the arc length s.
    static double
    arcLength(double[] p, int offset, double t, double epsilon)
    Computes the arc length s from time 0 to time t using an integration method.
    eval(double[] a, int offsetA, double t)
    Evaluates the given curve at the specified time.
    eval(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, double t)
    Evaluates the given curve at the specified time.
    getArcLengthIntegrand(double[] v, int offset)
    From paper.js, src/path/Curve.js, MIT License.
    static double
    invArcLength(double[] p, int offset, double s, double epsilon)
    Computes time t at the given arc length s.
    static double
    invArcLength(double[] p, int offset, double s, double totalArcLength, double epsilon)
    Computes time t at the given arc length s.
    static double @Nullable []
    merge(double[] a, int offsetA, double[] b, int offsetB, double tolerance)
    Tries to merge two bézier curves.
    static double @Nullable []
    merge(double x0, double y0, double x01, double y01, double x012, double y012, double x0123, double y0123, double x123, double y123, double x23, double y23, double x3, double y3, double tolerance)
    Tries to merge two bézier curves.
    static void
    split(double[] p, int o, double t, double[] f, int fo, double[] s, int so)
     
    static void
    split(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, double t, double[] left, double[] right)
    Splits the provided bezier curve into two parts.
    static void
    split(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, double t, @Nullable DoubleConsumer8 first, @Nullable DoubleConsumer8 second)
    Splits the provided bezier curve into two parts at the specified parameter value t.
    split(CubicCurve2D.Double source, double t)
    Splits the provided bezier curve into two parts at the specified parameter value t.
    static void
    Splits the provided bezier curve into two parts at the specified parameter value t.
    static void
    splitCubicCurveTo(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, double t, @Nullable DoubleConsumer6 left, @Nullable DoubleConsumer6 right)
    Splits the provided bezier curve into two parts at the specified parameter value t.
    static void
    subCurve(double[] q, int qOffset, double ta, double tb, double[] segment, int segmentOffset)
    Extracts the specified segment [ta,tb] from the given cubic curve.
    static double[]
     

    Methods inherited from class java.lang.Object

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

    • eval

      public static PointAndDerivative eval(double[] a, int offsetA, double t)
      Evaluates the given curve at the specified time.
      Parameters:
      a - points of the curve
      offsetA - index of the first point in array a
      t - the time
      Returns:
      the point at time t
    • eval

      public static PointAndDerivative eval(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, double t)
      Evaluates the given curve at the specified time.
      Parameters:
      x0 - point P0 of the curve
      y0 - point P0 of the curve
      x1 - point P1 of the curve
      y1 - point P1 of the curve
      x2 - point P2 of the curve
      y2 - point P2 of the curve
      x3 - point P3 of the curve
      y3 - point P3 of the curve
      t - the time
      Returns:
      the point at time t
    • merge

      public static double @Nullable [] merge(double[] a, int offsetA, double[] b, int offsetB, double tolerance)
      Tries to merge two bézier curves. Returns the new control point.
      Parameters:
      tolerance - distance (radius) at which the joined point may be off from x0123,y0123.
      Returns:
      the control points of the new curve (x0,y0)(x1,y1)(x2,y2)(x3,y3), null if merging failed
    • merge

      public static double @Nullable [] merge(double x0, double y0, double x01, double y01, double x012, double y012, double x0123, double y0123, double x123, double y123, double x23, double y23, double x3, double y3, double tolerance)
      Tries to merge two bézier curves. Returns the new control point.
      Parameters:
      x0 - point P0 of the first curve
      y0 - point P0 of the first curve
      x01 - point P1 of the first curve
      y01 - point P1 of the first curve
      x012 - point P2 of the first curve
      y012 - point P2 of the first curve
      x0123 - point P3 of the first curve or point p0 of the second curve respectively
      y0123 - point P3 of the first curve or point p0 of the second curve respectively
      x123 - point P1 of the second curve
      y123 - point P1 of the second curve
      x23 - point P2 of the second curve
      y23 - point P2 of the second curve
      x3 - point P3 of the second curve
      y3 - point P3 of the second curve
      tolerance - distance (radius) at which the joined point may be off from x0123,y0123.
      Returns:
      the control points of the new curve (x0,y0)(x1,y1)(x2,y2)(x3,y3), null if merging failed
    • split

      Splits the provided bezier curve into two parts at the specified parameter value t.

      Reference:

      Stackoverflow, Splitting a bezier curve, Copyright Jonathan, CC BY-SA 4.0 license
      stackoverflow.com
      .
    • split

      public static void split(CubicCurve2D.Double source, double t, CubicCurve2D.Double left, CubicCurve2D.Double right)
      Splits the provided bezier curve into two parts at the specified parameter value t.

      Reference:

      Stackoverflow, Splitting a bezier curve, Copyright Jonathan, CC BY-SA 4.0 license
      stackoverflow.com
    • splitCubicCurveTo

      public static void splitCubicCurveTo(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, double t, @Nullable DoubleConsumer6 left, @Nullable DoubleConsumer6 right)
      Splits the provided bezier curve into two parts at the specified parameter value t.

      Reference:

      Stackoverflow, Splitting a bezier curve, Copyright Jonathan, CC BY-SA 4.0 license
      stackoverflow.com
      Parameters:
      x0 - point P0 of the curve
      y0 - point P0 of the curve
      x1 - point P1 of the curve
      y1 - point P1 of the curve
      x2 - point P2 of the curve
      y2 - point P2 of the curve
      x3 - point P3 of the curve
      y3 - point P3 of the curve
      t - where to split
      left - if not null, accepts the curve from x1,y1 to t
      right - if not null, accepts the curve from t to x4,y4
    • split

      public static void split(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, double t, @Nullable DoubleConsumer8 first, @Nullable DoubleConsumer8 second)
      Splits the provided bezier curve into two parts at the specified parameter value t.

      Reference:

      Stackoverflow, Splitting a bezier curve, Copyright Jonathan, CC BY-SA 4.0 license
      stackoverflow.com
      Parameters:
      x0 - point P0 of the curve
      y0 - point P0 of the curve
      x1 - point P1 of the curve
      y1 - point P1 of the curve
      x2 - point P2 of the curve
      y2 - point P2 of the curve
      x3 - point P3 of the curve
      y3 - point P3 of the curve
      t - where to split
      first - if not null, accepts the curve from x1,y1 to t
      second - if not null, accepts the curve from t to x4,y4
    • split

      public static void split(double[] p, int o, double t, double[] f, int fo, double[] s, int so)
    • split

      public static void split(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, double t, double[] left, double[] right)
      Splits the provided bezier curve into two parts.

      Reference:

      Stackoverflow, Splitting a bezier curve, Copyright Jonathan, CC BY-SA 4.0 license
      stackoverflow.com
      Parameters:
      x0 - point P0 of the curve
      y0 - point P0 of the curve
      x1 - point P1 of the curve
      y1 - point P1 of the curve
      x2 - point P2 of the curve
      y2 - point P2 of the curve
      x3 - point P3 of the curve
      y3 - point P3 of the curve
      t - where to split
      left - if not null, accepts the curve from x1,y1 to t
      right - if not null, accepts the curve from t to x4,y4
    • subCurve

      public static void subCurve(double[] q, int qOffset, double ta, double tb, double[] segment, int segmentOffset)
      Extracts the specified segment [ta,tb] from the given cubic curve.
      Parameters:
      q - the cubic bezier curve
      qOffset - the index of the first curve parameter in q
      ta - where to split from
      tb - where to split to
      segment - the output array
      segmentOffset - the index of the first curve parameter in segment
    • getArcLengthIntegrand

      public static ToDoubleFunction<Double> getArcLengthIntegrand(double[] v, int offset)
      From paper.js, src/path/Curve.js, MIT License.

      References:

      paper.js. Copyright (c) 2011 - 2020 Jürg Lehni & Jonathan Puckey. MIT License.
      github.com
      Parameters:
      v - a cubic bezier curve
      offset - offset into array v
      Returns:
      the arc length integrand of a cubic Bézier curve
    • toArray

      public static double[] toArray(CubicCurve2D.Double c)
    • arcLength

      public static double arcLength(double[] p, int offset, double epsilon)
      Computes the arc length s.
      Parameters:
      p - points of the curve
      offset - index of the first point in array p
      epsilon -
      Returns:
      the arc length
    • arcLength

      public static double arcLength(double[] p, int offset, double t, double epsilon)
      Computes the arc length s from time 0 to time t using an integration method.
      Parameters:
      p - points of the curve
      offset - index of the first point in array p
      t - the time
      epsilon - the error tolerance
      Returns:
      the arc length
    • invArcLength

      public static double invArcLength(double[] p, int offset, double s, double epsilon)
      Computes time t at the given arc length s.
      Parameters:
      p - points of the curve
      offset - index of the first point in array p
      s - arc length
      epsilon -
      Returns:
      t at s
    • invArcLength

      public static double invArcLength(double[] p, int offset, double s, double totalArcLength, double epsilon)
      Computes time t at the given arc length s.
      Parameters:
      p - points of the curve
      offset - index of the first point in array p
      s - arc length
      totalArcLength - the total arc length of the curve
      epsilon -
      Returns:
      t at s