Class BezierUtil
java.lang.Object
de.tomatengames.util.BezierUtil
Provides utilities to work with cubic Bézier curves.
- Since:
- 1.2
-
Method Summary
Modifier and TypeMethodDescriptionstatic doubleeval(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 parametersp1, c1, c2, p2at the specifiedxposition.static doubleevalDeCasteljau(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 parametersp1, c1, c2, p2att.
-
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 parametersp1, c1, c2, p2att.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)]withp1=[p1.x, p1.y], etc. To calculateP(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=0returnsp1andt=1returnsp2.p1- The start point of the curve.c1- The control point ofp1.c2- The control point ofp2.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 parametersp1, c1, c2, p2at the specifiedxposition.This method first tries to calculate the parameter
tfor the specifiedxposition numerically. If the calculatedtimplies ax(t)withabs(x(t)-x) <= eps, this step is complete. Theny(t)is calculated and returned.The parameters must meet the following requirements:
p1must be beforep2(p1x < p2x)c1xmust be in the range ofp1xandp2x(p1x <= c1x <= p2x)c2xmust be in the range ofp1xandp2x(p1x <= c2x <= p2x)xmust be in the range ofp1xandp2x(p1x <= x <= p2x)
- Parameters:
x- The x position that should be evaluated. Must be in the range ofp1xandp2x.eps- The maximum error ofxto abort numerical methods. Negative values have the same effect as0. It is recommended to choose a value greater than0to improve performance. Note that it is not guaranteed that the error is less thaneps. In special cases, e.g.eps=0, if it is not possible or involves a disproportionate effort to find a fittingt,epsmay be ignored.p1x- Thexposition of the start point. Must be less thanp2x.p1y- Theyposition of the start point.c1x- Thexposition of the control point ofp1. Must be in the range ofp1xandp2x.c1y- Theyposition of the control point ofp1.c2x- Thexposition of the control point ofp2. Must be in the range ofp1xandp2x.c2y- Theyposition of the control point ofp2.p2x- Thexposition of the end point. Must be greater thanp1x.p2y- Theyposition of the end point.- Returns:
- The
yposition of the curve at the specifiedxposition.
-