math.geom2d.spline
Class BezierCurve2D

java.lang.Object
  extended by java.awt.geom.CubicCurve2D
      extended by java.awt.geom.CubicCurve2D.Double
          extended by math.geom2d.spline.BezierCurve2D
All Implemented Interfaces:
java.awt.Shape, java.io.Serializable, java.lang.Cloneable, ContinuousCurve2D, ContinuousOrientedCurve2D, Curve2D, OrientedCurve2D, SmoothCurve2D, Shape2D

public class BezierCurve2D
extends java.awt.geom.CubicCurve2D.Double
implements SmoothCurve2D, ContinuousOrientedCurve2D

An extension of the Bezier curve provided in java.awt.geom, with support for SmoothCurve2D and OrientedCurve2D.

Author:
Legland
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.awt.geom.CubicCurve2D
java.awt.geom.CubicCurve2D.Double, java.awt.geom.CubicCurve2D.Float
 
Field Summary
 
Fields inherited from class java.awt.geom.CubicCurve2D.Double
ctrlx1, ctrlx2, ctrly1, ctrly2, x1, x2, y1, y2
 
Fields inherited from interface math.geom2d.curve.ContinuousCurve2D
CIRCLE, CLOSED_EDGE, LOOP, OPEN_EDGE
 
Fields inherited from interface math.geom2d.Shape2D
ACCURACY, defaultClipWindow, EMPTY_SET
 
Constructor Summary
BezierCurve2D()
           
BezierCurve2D(double x1, double y1, double xctrl1, double yctrl1, double xctrl2, double yctrl2, double x2, double y2)
          Build a new Bezier curve of degree 3 by specifying position of extreme points and position of 2 control points.
BezierCurve2D(java.awt.geom.Point2D p1, java.awt.geom.Point2D ctrl1, java.awt.geom.Point2D ctrl2, java.awt.geom.Point2D p2)
          Build a new Bezier curve of degree 3 by specifying position of extreme points and position of 2 control points.
BezierCurve2D(java.awt.geom.Point2D p1, Vector2D v1, java.awt.geom.Point2D p2, Vector2D v2)
          Build a new Bezier curve of degree 3 by specifying position and tangent of first and last points.
 
Method Summary
 java.awt.geom.GeneralPath appendPath(java.awt.geom.GeneralPath path)
          Append the path of the curve to the given path.
 CurveSet2D<? extends BezierCurve2D> clip(Box2D box)
          Clip the circle arc by a box.
 Polyline2D getAsPolyline(int n)
          Creates a polyline with n line segments approximating the Bezier curve.
 Box2D getBoundingBox()
          Returns the bounding box of the shape.
 Shape2D getClippedShape(Box2D box)
           
 java.util.Collection<ContinuousCurve2D> getContinuousCurves()
          Returns the collection of continuous curves which constitute this curve.
 double getCurvature(double t)
          returns the curvature of the Curve.
 double getDistance(double x, double y)
          Compute approximated distance, computed on a polyline.
 double getDistance(java.awt.geom.Point2D p)
          get the distance of the shape to the given point, or the distance of point to the frontier of the shape in the case of a plain shape.
 Point2D getFirstPoint()
          Get the first point of the curve.
 java.awt.geom.GeneralPath getInnerPath()
           
 java.util.Collection<Point2D> getIntersections(StraightObject2D line)
          Use approximation, by replacing Bezier curve with a polyline.
 Point2D getLastPoint()
          Get the last point of the curve.
 double[][] getParametric()
          Returns the matrix of parametric representation of the line.
 Point2D getPoint(double t)
          Gets the point from a parametric representation of the curve.
 Point2D getPoint(double t, Point2D point)
          Same as getPoint(t), but gives the point as a parameter.
 double getPosition(Point2D point)
          Compute position by approximating cubic spline with a polyline.
 BezierCurve2D getReverseCurve()
          Returns the bezier curve given by control points taken in reverse order.
 double getSignedDistance(double x, double y)
          The same as getSignedDistance(Point2D), but by passing 2 double as arguments.
 double getSignedDistance(java.awt.geom.Point2D point)
          Get the signed distance of the curve to the given point : this distance is positive if the point lies outside the shape, and is negative if the point lies inside the shape.
 java.util.Collection<? extends SmoothCurve2D> getSmoothPieces()
          Returns a set of smooth curves.
 BezierCurve2D getSubCurve(double t0, double t1)
          Computes subcurve.
 double getT0()
          returns 0, as Bezier curve is parametrized between 0 and 1.
 double getT1()
          returns 1, as Bezier curve is parametrized between 0 and 1.
 Vector2D getTangent(double t)
           
 double getWindingAngle(java.awt.geom.Point2D point)
          Use winding angle of approximated polyline
 boolean isBounded()
          return true, a cubic Bezier Curve is always bounded.
 boolean isClosed()
          The cubic curve is never closed.
 boolean isInside(java.awt.geom.Point2D pt)
          return true if the point is 'inside' the domain bounded by the curve.
 double project(Point2D point)
          Compute position by approximating cubic spline with a polyline.
 BezierCurve2D transform(AffineTransform2D trans)
          Returns the Bezier Curve transformed by the given AffineTransform2D.
 
Methods inherited from class java.awt.geom.CubicCurve2D.Double
getBounds2D, getCtrlP1, getCtrlP2, getCtrlX1, getCtrlX2, getCtrlY1, getCtrlY2, getP1, getP2, getX1, getX2, getY1, getY2, setCurve
 
Methods inherited from class java.awt.geom.CubicCurve2D
clone, contains, contains, contains, contains, getBounds, getFlatness, getFlatness, getFlatness, getFlatnessSq, getFlatnessSq, getFlatnessSq, getPathIterator, getPathIterator, intersects, intersects, setCurve, setCurve, setCurve, setCurve, solveCubic, solveCubic, subdivide, subdivide, subdivide
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.awt.Shape
contains, contains, contains, contains, getBounds, getBounds2D, getPathIterator, getPathIterator, intersects, intersects
 

Constructor Detail

BezierCurve2D

public BezierCurve2D()

BezierCurve2D

public BezierCurve2D(java.awt.geom.Point2D p1,
                     java.awt.geom.Point2D ctrl1,
                     java.awt.geom.Point2D ctrl2,
                     java.awt.geom.Point2D p2)
Build a new Bezier curve of degree 3 by specifying position of extreme points and position of 2 control points. The resulting curve is totally containe in the convex polygon formed by the 4 control points.

Parameters:
p1 - first point
ctrl1 - first control point
ctrl2 - second control point
p2 - last point

BezierCurve2D

public BezierCurve2D(java.awt.geom.Point2D p1,
                     Vector2D v1,
                     java.awt.geom.Point2D p2,
                     Vector2D v2)
Build a new Bezier curve of degree 3 by specifying position and tangent of first and last points.

Parameters:
p1 - first point
v1 - first tangent vector
p2 - position of last point
v2 - last tangent vector

BezierCurve2D

public BezierCurve2D(double x1,
                     double y1,
                     double xctrl1,
                     double yctrl1,
                     double xctrl2,
                     double yctrl2,
                     double x2,
                     double y2)
Build a new Bezier curve of degree 3 by specifying position of extreme points and position of 2 control points. The resulting curve is totally containe in the convex polygon formed by the 4 control points.

Method Detail

getParametric

public double[][] getParametric()
Returns the matrix of parametric representation of the line. Result has the form :

[ x0 dx dx2 dx3]

[ y0 dy dy2 dy3]

Coefficients are from the parametric equation : x(t) = x0 + dx*t + dx2*t^2 + dx3*t^3 y(t) = y0 + dy*t + dy2*t^2 + dy3*t^3


getAsPolyline

public Polyline2D getAsPolyline(int n)
Creates a polyline with n line segments approximating the Bezier curve.

Specified by:
getAsPolyline in interface ContinuousCurve2D
Parameters:
n - number of line segments of polyline
Returns:
a polyline with n line segments.

getTangent

public Vector2D getTangent(double t)
Specified by:
getTangent in interface SmoothCurve2D

getCurvature

public double getCurvature(double t)
returns the curvature of the Curve.

Specified by:
getCurvature in interface SmoothCurve2D

getSmoothPieces

public java.util.Collection<? extends SmoothCurve2D> getSmoothPieces()
Description copied from interface: ContinuousCurve2D
Returns a set of smooth curves.

Specified by:
getSmoothPieces in interface ContinuousCurve2D

isClosed

public boolean isClosed()
The cubic curve is never closed.

Specified by:
isClosed in interface ContinuousCurve2D

getWindingAngle

public double getWindingAngle(java.awt.geom.Point2D point)
Use winding angle of approximated polyline

Specified by:
getWindingAngle in interface OrientedCurve2D
Parameters:
point - a point of the plane
Returns:
a signed angle
See Also:
math.geom2d.OrientedCurve2D#getWindingAngle(java.awt.geom.Point2D)

isInside

public boolean isInside(java.awt.geom.Point2D pt)
return true if the point is 'inside' the domain bounded by the curve. Uses a polyline approximation.

Specified by:
isInside in interface OrientedCurve2D
Parameters:
pt - a point in the plane
Returns:
true if the point is on the left side of the curve.

getSignedDistance

public double getSignedDistance(java.awt.geom.Point2D point)
Description copied from interface: OrientedCurve2D
Get the signed distance of the curve to the given point : this distance is positive if the point lies outside the shape, and is negative if the point lies inside the shape. In this case, absolute value of distance is equals to the distance to the border of the shape.

Specified by:
getSignedDistance in interface OrientedCurve2D
Parameters:
point - a point of the plane
Returns:
the signed distance to the curve

getSignedDistance

public double getSignedDistance(double x,
                                double y)
Description copied from interface: OrientedCurve2D
The same as getSignedDistance(Point2D), but by passing 2 double as arguments.

Specified by:
getSignedDistance in interface OrientedCurve2D
Parameters:
x - x-coord of a point
y - y-coord of a point
Returns:
the signed distance of the point (x,y) to the curve
See Also:
math.geom2d.OrientedCurve2D#getSignedDistance(java.awt.geom.Point2D)

getT0

public double getT0()
returns 0, as Bezier curve is parametrized between 0 and 1.

Specified by:
getT0 in interface Curve2D

getT1

public double getT1()
returns 1, as Bezier curve is parametrized between 0 and 1.

Specified by:
getT1 in interface Curve2D

getIntersections

public java.util.Collection<Point2D> getIntersections(StraightObject2D line)
Use approximation, by replacing Bezier curve with a polyline.

Specified by:
getIntersections in interface Curve2D
See Also:
math.geom2d.Curve2D#getIntersections(math.geom2d.StraightObject2D)

getPoint

public Point2D getPoint(double t)
Description copied from interface: Curve2D
Gets the point from a parametric representation of the curve. If the parameter lies outside the definition range, the parameter corresponding to the closest bound is used instead. This method can be used to draw an approximated outline of a curve, by selecting multiple values for t and drawing lines between them.

Specified by:
getPoint in interface Curve2D
See Also:
math.geom2d.Curve2D#getPoint(double)

getPoint

public Point2D getPoint(double t,
                        Point2D point)
Description copied from interface: Curve2D
Same as getPoint(t), but gives the point as a parameter. This avoids repetitive memory allocations.

Specified by:
getPoint in interface Curve2D
See Also:
math.geom2d.Curve2D#getPoint(double, math.geom2d.Point2D)

getFirstPoint

public Point2D getFirstPoint()
Get the first point of the curve.

Specified by:
getFirstPoint in interface Curve2D
Returns:
the first point of the curve

getLastPoint

public Point2D getLastPoint()
Get the last point of the curve.

Specified by:
getLastPoint in interface Curve2D
Returns:
the last point of the curve.

getPosition

public double getPosition(Point2D point)
Compute position by approximating cubic spline with a polyline.

Specified by:
getPosition in interface Curve2D
Parameters:
point - a point belonging to the curve
Returns:
the position of the point on the curve

project

public double project(Point2D point)
Compute position by approximating cubic spline with a polyline.

Specified by:
project in interface Curve2D
Parameters:
point - a point to project
Returns:
the position of the closest orthogonal projection

getReverseCurve

public BezierCurve2D getReverseCurve()
Returns the bezier curve given by control points taken in reverse order.

Specified by:
getReverseCurve in interface ContinuousCurve2D
Specified by:
getReverseCurve in interface ContinuousOrientedCurve2D
Specified by:
getReverseCurve in interface Curve2D
Specified by:
getReverseCurve in interface OrientedCurve2D
Specified by:
getReverseCurve in interface SmoothCurve2D

getSubCurve

public BezierCurve2D getSubCurve(double t0,
                                 double t1)
Computes subcurve. If t1
Specified by:
getSubCurve in interface ContinuousCurve2D
Specified by:
getSubCurve in interface ContinuousOrientedCurve2D
Specified by:
getSubCurve in interface Curve2D
Specified by:
getSubCurve in interface SmoothCurve2D
Parameters:
t0 - position of the start of the sub-curve
t1 - position of the end of the sub-curve
Returns:
the portion of original curve comprised between t0 and t1.

getDistance

public double getDistance(java.awt.geom.Point2D p)
Description copied from interface: Shape2D
get the distance of the shape to the given point, or the distance of point to the frontier of the shape in the case of a plain shape.

Specified by:
getDistance in interface Shape2D
See Also:
Shape2D.getDistance(java.awt.geom.Point2D)

getDistance

public double getDistance(double x,
                          double y)
Compute approximated distance, computed on a polyline.

Specified by:
getDistance in interface Shape2D
See Also:
Shape2D.getDistance(double, double)

isBounded

public boolean isBounded()
return true, a cubic Bezier Curve is always bounded.

Specified by:
isBounded in interface Shape2D

getClippedShape

public Shape2D getClippedShape(Box2D box)
See Also:
math.geom2d.Shape2D#getClippedShape(math.geom2d.Box2D)

clip

public CurveSet2D<? extends BezierCurve2D> clip(Box2D box)
Clip the circle arc by a box. The result is an instance of ContinuousOrientedCurveSet2D, which contains only instances of EllipseArc2D. If the ellipse arc is not clipped, the result is an instance of ContinuousOrientedCurveSet2D which contains 0 curves.

Specified by:
clip in interface ContinuousCurve2D
Specified by:
clip in interface ContinuousOrientedCurve2D
Specified by:
clip in interface Curve2D
Specified by:
clip in interface OrientedCurve2D
Specified by:
clip in interface SmoothCurve2D
Specified by:
clip in interface Shape2D
Parameters:
box - the clipping box
Returns:
the clipped shape

getBoundingBox

public Box2D getBoundingBox()
Description copied from interface: Shape2D
Returns the bounding box of the shape.

Specified by:
getBoundingBox in interface Shape2D
Returns:
the bounding box of the shape.

transform

public BezierCurve2D transform(AffineTransform2D trans)
Returns the Bezier Curve transformed by the given AffineTransform2D. This is simply done by transforming control points of the curve.

Specified by:
transform in interface ContinuousCurve2D
Specified by:
transform in interface ContinuousOrientedCurve2D
Specified by:
transform in interface Curve2D
Specified by:
transform in interface OrientedCurve2D
Specified by:
transform in interface SmoothCurve2D
Specified by:
transform in interface Shape2D
Parameters:
trans - an affine transform
Returns:
the transformed shape

appendPath

public java.awt.geom.GeneralPath appendPath(java.awt.geom.GeneralPath path)
Description copied from interface: ContinuousCurve2D
Append the path of the curve to the given path.

Specified by:
appendPath in interface ContinuousCurve2D
Parameters:
path - a path to modify
Returns:
the modified path

getInnerPath

public java.awt.geom.GeneralPath getInnerPath()

getContinuousCurves

public java.util.Collection<ContinuousCurve2D> getContinuousCurves()
Description copied from interface: Curve2D
Returns the collection of continuous curves which constitute this curve.

Specified by:
getContinuousCurves in interface Curve2D
Returns:
a collection of continuous curves.