FileDocCategorySizeDatePackage
CubicCurve2D.javaAPI DocJava SE 5 API56277Fri Aug 26 14:56:52 BST 2005java.awt.geom

CubicCurve2D

public abstract class CubicCurve2D extends Object implements Shape, Cloneable
The CubicCurve2D class defines a cubic parametric curve segment in (x,  y) coordinate space.

This class is only the abstract superclass for all objects which store a 2D cubic curve segment. The actual storage representation of the coordinates is left to the subclass.

version
1.29, 12/19/03
author
Jim Graham

Fields Summary
private static final int
BELOW
private static final int
LOWEDGE
private static final int
INSIDE
private static final int
HIGHEDGE
private static final int
ABOVE
Constructors Summary
protected CubicCurve2D()
This is an abstract class that cannot be instantiated directly. Type-specific implementation subclasses are available for instantiation and provide a number of formats for storing the information necessary to satisfy the various accessor methods below.

see
java.awt.geom.CubicCurve2D.Float
see
java.awt.geom.CubicCurve2D.Double

    
Methods Summary
public java.lang.Objectclone()
Creates a new object of the same class as this object.

return
a clone of this instance.
exception
OutOfMemoryError if there is not enough memory.
see
java.lang.Cloneable
since
1.2

	try {
	    return super.clone();
	} catch (CloneNotSupportedException e) {
	    // this shouldn't happen, since we are Cloneable
	    throw new InternalError();
	}
    
public booleancontains(double x, double y)
Tests if a specified coordinate is inside the boundary of the shape.

param
x, y the specified coordinate to be tested
return
true if the coordinate is inside the boundary of the shape; false otherwise.

	// We count the "Y" crossings to determine if the point is
	// inside the curve bounded by its closing line.
	int crossings = 0;
	double x1 = getX1();
	double y1 = getY1();
	double x2 = getX2();
	double y2 = getY2();
	// First check for a crossing of the line connecting the endpoints
	double dy = y2 - y1;
	if ((dy > 0.0 && y >= y1 && y <= y2) ||
	    (dy < 0.0 && y <= y1 && y >= y2))
	{
	    if (x < x1 + (y - y1) * (x2 - x1) / dy) {
		crossings++;
	    }
	}
	// Solve the Y parametric equation for intersections with y
	double ctrlx1 = getCtrlX1();
	double ctrly1 = getCtrlY1();
	double ctrlx2 = getCtrlX2();
	double ctrly2 = getCtrlY2();
	boolean include0 = ((y2 - y1) * (ctrly1 - y1) >= 0);
	boolean include1 = ((y1 - y2) * (ctrly2 - y2) >= 0);
	double eqn[] = new double[4];
	double res[] = new double[4];
	fillEqn(eqn, y, y1, ctrly1, ctrly2, y2);
	int roots = solveCubic(eqn, res);
	roots = evalCubic(res, roots,
			  include0, include1, eqn,
			  x1, ctrlx1, ctrlx2, x2);
	while (--roots >= 0) {
	    if (x < res[roots]) {
		crossings++;
	    }
	}
	return ((crossings & 1) == 1);
    
public booleancontains(java.awt.geom.Point2D p)
Tests if a specified Point2D is inside the boundary of the shape.

param
p the specified Point2D to be tested
return
true if the p is inside the boundary of the shape; false otherwise.

	return contains(p.getX(), p.getY());
    
public booleancontains(double x, double y, double w, double h)
Tests if the interior of the shape entirely contains the specified set of rectangular coordinates.

param
x, y the coordinates of the upper left corner of the specified rectangular shape
param
w the width of the specified rectangular shape
param
h the height of the specified rectangular shape
return
true if the shape entirely contains the specified set of rectangular coordinates; false otherwise.

	// Assertion: Cubic curves closed by connecting their
	// endpoints form either one or two convex halves with
	// the closing line segment as an edge of both sides.
	if (!(contains(x, y) &&
	      contains(x + w, y) &&
	      contains(x + w, y + h) &&
	      contains(x, y + h))) {
	    return false;
	}
	// Either the rectangle is entirely inside one of the convex
	// halves or it crosses from one to the other, in which case
	// it must intersect the closing line segment.
	Rectangle2D rect = new Rectangle2D.Double(x, y, w, h);
	return !rect.intersectsLine(getX1(), getY1(), getX2(), getY2());
    
public booleancontains(java.awt.geom.Rectangle2D r)
Tests if the interior of the shape entirely contains the specified Rectangle2D.

param
r the specified Rectangle2D to be tested
return
true if the shape entirely contains the specified Rectangle2D; false otherwise.

	return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
    
private static intevalCubic(double[] vals, int num, boolean include0, boolean include1, double[] inflect, double c1, double cp1, double cp2, double c2)

	int j = 0;
	for (int i = 0; i < num; i++) {
	    double t = vals[i];
	    if ((include0 ? t >= 0 : t > 0) &&
		(include1 ? t <= 1 : t < 1) &&
		(inflect == null ||
		 inflect[1] + (2*inflect[2] + 3*inflect[3]*t)*t != 0))
	    {
		double u = 1 - t;
		vals[j++] = c1*u*u*u + 3*cp1*t*u*u + 3*cp2*t*t*u + c2*t*t*t;
	    }
	}
	return j;
    
private static voidfillEqn(double[] eqn, double val, double c1, double cp1, double cp2, double c2)

	eqn[0] = c1 - val;
	eqn[1] = (cp1 - c1) * 3.0;
	eqn[2] = (cp2 - cp1 - cp1 + c1) * 3.0;
	eqn[3] = c2 + (cp1 - cp2) * 3.0 - c1;
	return;
    
private static doublefindZero(double t, double target, double[] eqn)

	double slopeqn[] = {eqn[1], 2*eqn[2], 3*eqn[3]};
	double slope;
	double origdelta = 0;
	double origt = t;
	while (true) {
	    slope = solveEqn(slopeqn, 2, t);
	    if (slope == 0) {
		// At a local minima - must return
		return t;
	    }
	    double y = solveEqn(eqn, 3, t);
	    if (y == 0) {
		// Found it! - return it
		return t;
	    }
	    // assert(slope != 0 && y != 0);
	    double delta = - (y / slope);
	    // assert(delta != 0);
	    if (origdelta == 0) {
		origdelta = delta;
	    }
	    if (t < target) {
		if (delta < 0) return t;
	    } else if (t > target) {
		if (delta > 0) return t;
	    } else { /* t == target */
		return (delta > 0
			? (target + java.lang.Double.MIN_VALUE)
			: (target - java.lang.Double.MIN_VALUE));
	    }
	    double newt = t + delta;
	    if (t == newt) {
		// The deltas are so small that we aren't moving...
		return t;
	    }
	    if (delta * origdelta < 0) {
		// We have reversed our path.
		int tag = (origt < t
			   ? getTag(target, origt, t)
			   : getTag(target, t, origt));
		if (tag != INSIDE) {
		    // Local minima found away from target - return the middle
		    return (origt + t) / 2;
		}
		// Local minima somewhere near target - move to target
		// and let the slope determine the resulting t.
		t = target;
	    } else {
		t = newt;
	    }
	}
    
private static voidfixRoots(double[] res, double[] eqn)

	final double EPSILON = 1E-5;
	for (int i = 0; i < 3; i++) {
	    double t = res[i];
	    if (Math.abs(t) < EPSILON) {
		res[i] = findZero(t, 0, eqn);
	    } else if (Math.abs(t - 1) < EPSILON) {
		res[i] = findZero(t, 1, eqn);
	    }
	}
    
public java.awt.RectanglegetBounds()
Returns the bounding box of the shape.

return
a {@link Rectangle} that is the bounding box of the shape.

	return getBounds2D().getBounds();
    
public abstract java.awt.geom.Point2DgetCtrlP1()
Returns the first control point.

return
a Point2D that is the first control point of the CubicCurve2D.

public abstract java.awt.geom.Point2DgetCtrlP2()
Returns the second control point.

return
a Point2D that is the second control point of the CubicCurve2D.

public abstract doublegetCtrlX1()
Returns the X coordinate of the first control point in double precision.

return
the X coordinate of the first control point of the CubicCurve2D.

public abstract doublegetCtrlX2()
Returns the X coordinate of the second control point in double precision.

return
the X coordinate of the second control point of the CubicCurve2D.

public abstract doublegetCtrlY1()
Returns the Y coordinate of the first control point in double precision.

return
the Y coordinate of the first control point of the CubicCurve2D.

public abstract doublegetCtrlY2()
Returns the Y coordinate of the second control point in double precision.

return
the Y coordinate of the second control point of the CubicCurve2D.

public static doublegetFlatness(double x1, double y1, double ctrlx1, double ctrly1, double ctrlx2, double ctrly2, double x2, double y2)
Returns the flatness of the cubic curve specified by the indicated controlpoints. The flatness is the maximum distance of a controlpoint from the line connecting the endpoints.

param
x1, y1 the first specified coordinates that specify the start point of a CubicCurve2D
param
ctrlx1, ctrly1 the second specified coordinates that specify the first control point of a CubicCurve2D
param
ctrlx2, ctrly2 the third specified coordinates that specify the second control point of a CubicCurve2D
param
x2, y2 the fourth specified coordinates that specify the end point of a CubicCurve2D
return
the flatness of the CubicCurve2D represented by the specified coordinates.

	return Math.sqrt(getFlatnessSq(x1, y1, ctrlx1, ctrly1,
				       ctrlx2, ctrly2, x2, y2));
    
public static doublegetFlatness(double[] coords, int offset)
Returns the flatness of the cubic curve specified by the controlpoints stored in the indicated array at the indicated index. The flatness is the maximum distance of a controlpoint from the line connecting the endpoints.

param
coords an array containing coordinates
param
offset the index of coords at which to begin setting the endpoints and controlpoints of this curve to the coordinates contained in coords
return
the flatness of the CubicCurve2D specified by the coordinates in coords at the specified offset.

	return getFlatness(coords[offset + 0], coords[offset + 1],
			   coords[offset + 2], coords[offset + 3],
			   coords[offset + 4], coords[offset + 5],
			   coords[offset + 6], coords[offset + 7]);
    
public doublegetFlatness()
Returns the flatness of this curve. The flatness is the maximum distance of a controlpoint from the line connecting the endpoints.

return
the flatness of this curve.

	return getFlatness(getX1(), getY1(), getCtrlX1(), getCtrlY1(),
			   getCtrlX2(), getCtrlY2(), getX2(), getY2());
    
public static doublegetFlatnessSq(double x1, double y1, double ctrlx1, double ctrly1, double ctrlx2, double ctrly2, double x2, double y2)
Returns the square of the flatness of the cubic curve specified by the indicated controlpoints. The flatness is the maximum distance of a controlpoint from the line connecting the endpoints.

param
x1, y1 the first specified coordinates that specify the start point of a CubicCurve2D
param
ctrlx1, ctrly1 the second specified coordinates that specify the first control point of a CubicCurve2D
param
ctrlx2, ctrly2 the third specified coordinates that specify the second control point of a CubicCurve2D
param
x2, y2 the fourth specified coordinates that specify the end point of a CubicCurve2D
return
the square of the flatness of the CubicCurve2D represented by the specified coordinates.

	return Math.max(Line2D.ptSegDistSq(x1, y1, x2, y2, ctrlx1, ctrly1),
			Line2D.ptSegDistSq(x1, y1, x2, y2, ctrlx2, ctrly2));
			
    
public static doublegetFlatnessSq(double[] coords, int offset)
Returns the square of the flatness of the cubic curve specified by the controlpoints stored in the indicated array at the indicated index. The flatness is the maximum distance of a controlpoint from the line connecting the endpoints.

param
coords an array containing coordinates
param
offset the index of coords at which to begin setting the endpoints and controlpoints of this curve to the coordinates contained in coords
return
the square of the flatness of the CubicCurve2D specified by the coordinates in coords at the specified offset.

	return getFlatnessSq(coords[offset + 0], coords[offset + 1],
			     coords[offset + 2], coords[offset + 3],
			     coords[offset + 4], coords[offset + 5],
			     coords[offset + 6], coords[offset + 7]);
    
public doublegetFlatnessSq()
Returns the square of the flatness of this curve. The flatness is the maximum distance of a controlpoint from the line connecting the endpoints.

return
the square of the flatness of this curve.

	return getFlatnessSq(getX1(), getY1(), getCtrlX1(), getCtrlY1(),
			     getCtrlX2(), getCtrlY2(), getX2(), getY2());
    
public abstract java.awt.geom.Point2DgetP1()
Returns the start point.

return
a Point2D that is the start point of the CubicCurve2D.

public abstract java.awt.geom.Point2DgetP2()
Returns the end point.

return
a Point2D that is the end point of the CubicCurve2D.

public java.awt.geom.PathIteratorgetPathIterator(java.awt.geom.AffineTransform at)
Returns an iteration object that defines the boundary of the shape. The iterator for this class is not multi-threaded safe, which means that this CubicCurve2D class does not guarantee that modifications to the geometry of this CubicCurve2D object do not affect any iterations of that geometry that are already in process.

param
at an optional AffineTransform to be applied to the coordinates as they are returned in the iteration, or null if untransformed coordinates are desired
return
the PathIterator object that returns the geometry of the outline of this CubicCurve2D, one segment at a time.

	return new CubicIterator(this, at);
    
public java.awt.geom.PathIteratorgetPathIterator(java.awt.geom.AffineTransform at, double flatness)
Return an iteration object that defines the boundary of the flattened shape. The iterator for this class is not multi-threaded safe, which means that this CubicCurve2D class does not guarantee that modifications to the geometry of this CubicCurve2D object do not affect any iterations of that geometry that are already in process.

param
at an optional AffineTransform to be applied to the coordinates as they are returned in the iteration, or null if untransformed coordinates are desired
param
flatness the maximum amount that the control points for a given curve can vary from colinear before a subdivided curve is replaced by a straight line connecting the endpoints
return
the PathIterator object that returns the geometry of the outline of this CubicCurve2D, one segment at a time.

	return new FlatteningPathIterator(getPathIterator(at), flatness);
    
private static intgetTag(double coord, double low, double high)


    /*
     * Determine where coord lies with respect to the range from
     * low to high.  It is assumed that low <= high.  The return
     * value is one of the 5 values BELOW, LOWEDGE, INSIDE, HIGHEDGE,
     * or ABOVE.
     */
             
	if (coord <= low) {
	    return (coord < low ? BELOW : LOWEDGE);
	}
	if (coord >= high) {
	    return (coord > high ? ABOVE : HIGHEDGE);
	}
	return INSIDE;
    
public abstract doublegetX1()
Returns the X coordinate of the start point in double precision.

return
the X coordinate of the start point of the CubicCurve2D.

public abstract doublegetX2()
Returns the X coordinate of the end point in double precision.

return
the X coordinate of the end point of the CubicCurve2D.

public abstract doublegetY1()
Returns the Y coordinate of the start point in double precision.

return
the Y coordinate of the start point of the CubicCurve2D.

public abstract doublegetY2()
Returns the Y coordinate of the end point in double precision.

return
the Y coordinate of the end point of the CubicCurve2D.

public booleanintersects(double x, double y, double w, double h)
Tests if the shape intersects the interior of a specified set of rectangular coordinates.

param
x, y the coordinates of the upper left corner of the specified rectangular area
param
w the width of the specified rectangular area
param
h the height of the specified rectangular area
return
true if the shape intersects the interior of the specified rectangular area; false otherwise.

	// Trivially reject non-existant rectangles
	if (w < 0 || h < 0) {
	    return false;
	}

	// Trivially accept if either endpoint is inside the rectangle
	// (not on its border since it may end there and not go inside)
	// Record where they lie with respect to the rectangle.
	//     -1 => left, 0 => inside, 1 => right
	double x1 = getX1();
	double y1 = getY1();
	int x1tag = getTag(x1, x, x+w);
	int y1tag = getTag(y1, y, y+h);
	if (x1tag == INSIDE && y1tag == INSIDE) {
	    return true;
	}
	double x2 = getX2();
	double y2 = getY2();
	int x2tag = getTag(x2, x, x+w);
	int y2tag = getTag(y2, y, y+h);
	if (x2tag == INSIDE && y2tag == INSIDE) {
	    return true;
	}

	double ctrlx1 = getCtrlX1();
	double ctrly1 = getCtrlY1();
	double ctrlx2 = getCtrlX2();
	double ctrly2 = getCtrlY2();
	int ctrlx1tag = getTag(ctrlx1, x, x+w);
	int ctrly1tag = getTag(ctrly1, y, y+h);
	int ctrlx2tag = getTag(ctrlx2, x, x+w);
	int ctrly2tag = getTag(ctrly2, y, y+h);

	// Trivially reject if all points are entirely to one side of
	// the rectangle.
	if (x1tag < INSIDE && x2tag < INSIDE &&
	    ctrlx1tag < INSIDE && ctrlx2tag < INSIDE)
	{
	    return false;	// All points left
	}
	if (y1tag < INSIDE && y2tag < INSIDE &&
	    ctrly1tag < INSIDE && ctrly2tag < INSIDE)
	{
	    return false;	// All points above
	}
	if (x1tag > INSIDE && x2tag > INSIDE &&
	    ctrlx1tag > INSIDE && ctrlx2tag > INSIDE)
	{
	    return false;	// All points right
	}
	if (y1tag > INSIDE && y2tag > INSIDE &&
	    ctrly1tag > INSIDE && ctrly2tag > INSIDE)
	{
	    return false;	// All points below
	}

	// Test for endpoints on the edge where either the segment
	// or the curve is headed "inwards" from them
	// Note: These tests are a superset of the fast endpoint tests
	//       above and thus repeat those tests, but take more time
	//       and cover more cases
	if (inwards(x1tag, x2tag, ctrlx1tag) &&
	    inwards(y1tag, y2tag, ctrly1tag))
	{
	    // First endpoint on border with either edge moving inside
	    return true;
	}
	if (inwards(x2tag, x1tag, ctrlx2tag) &&
	    inwards(y2tag, y1tag, ctrly2tag))
	{
	    // Second endpoint on border with either edge moving inside
	    return true;
	}

	// Trivially accept if endpoints span directly across the rectangle
	boolean xoverlap = (x1tag * x2tag <= 0);
	boolean yoverlap = (y1tag * y2tag <= 0);
	if (x1tag == INSIDE && x2tag == INSIDE && yoverlap) {
	    return true;
	}
	if (y1tag == INSIDE && y2tag == INSIDE && xoverlap) {
	    return true;
	}

	// We now know that both endpoints are outside the rectangle
	// but the 4 points are not all on one side of the rectangle.
	// Therefore the curve cannot be contained inside the rectangle,
	// but the rectangle might be contained inside the curve, or
	// the curve might intersect the boundary of the rectangle.

	double[] eqn = new double[4];
	double[] res = new double[4];
	if (!yoverlap) {
	    // Both y coordinates for the closing segment are above or
	    // below the rectangle which means that we can only intersect
	    // if the curve crosses the top (or bottom) of the rectangle
	    // in more than one place and if those crossing locations
	    // span the horizontal range of the rectangle.
	    fillEqn(eqn, (y1tag < INSIDE ? y : y+h), y1, ctrly1, ctrly2, y2);
	    int num = solveCubic(eqn, res);
	    num = evalCubic(res, num, true, true, null,
			    x1, ctrlx1, ctrlx2, x2);
	    // odd counts imply the crossing was out of [0,1] bounds
	    // otherwise there is no way for that part of the curve to
	    // "return" to meet its endpoint
	    return (num == 2 &&
		    getTag(res[0], x, x+w) * getTag(res[1], x, x+w) <= 0);
	}

	// Y ranges overlap.  Now we examine the X ranges
	if (!xoverlap) {
	    // Both x coordinates for the closing segment are left of
	    // or right of the rectangle which means that we can only
	    // intersect if the curve crosses the left (or right) edge
	    // of the rectangle in more than one place and if those
	    // crossing locations span the vertical range of the rectangle.
	    fillEqn(eqn, (x1tag < INSIDE ? x : x+w), x1, ctrlx1, ctrlx2, x2);
	    int num = solveCubic(eqn, res);
	    num = evalCubic(res, num, true, true, null,
			    y1, ctrly1, ctrly2, y2);
	    // odd counts imply the crossing was out of [0,1] bounds
	    // otherwise there is no way for that part of the curve to
	    // "return" to meet its endpoint
	    return (num == 2 &&
		    getTag(res[0], y, y+h) * getTag(res[1], y, y+h) <= 0);
	}

	// The X and Y ranges of the endpoints overlap the X and Y
	// ranges of the rectangle, now find out how the endpoint
	// line segment intersects the Y range of the rectangle
	double dx = x2 - x1;
	double dy = y2 - y1;
	double k = y2 * x1 - x2 * y1;
	int c1tag, c2tag;
	if (y1tag == INSIDE) {
	    c1tag = x1tag;
	} else {
	    c1tag = getTag((k + dx * (y1tag < INSIDE ? y : y+h)) / dy, x, x+w);
	}
	if (y2tag == INSIDE) {
	    c2tag = x2tag;
	} else {
	    c2tag = getTag((k + dx * (y2tag < INSIDE ? y : y+h)) / dy, x, x+w);
	}
	// If the part of the line segment that intersects the Y range
	// of the rectangle crosses it horizontally - trivially accept
	if (c1tag * c2tag <= 0) {
	    return true;
	}

	// Now we know that both the X and Y ranges intersect and that
	// the endpoint line segment does not directly cross the rectangle.
	//
	// We can almost treat this case like one of the cases above
	// where both endpoints are to one side, except that we may
	// get one or three intersections of the curve with the vertical
	// side of the rectangle.  This is because the endpoint segment
	// accounts for the other intersection in an even pairing.  Thus,
	// with the endpoint crossing we end up with 2 or 4 total crossings.
	//
	// (Remember there is overlap in both the X and Y ranges which
	//  means that the segment itself must cross at least one vertical
	//  edge of the rectangle - in particular, the "near vertical side"
	//  - leaving an odd number of intersections for the curve.)
	//
	// Now we calculate the y tags of all the intersections on the
	// "near vertical side" of the rectangle.  We will have one with
	// the endpoint segment, and one or three with the curve.  If
	// any pair of those vertical intersections overlap the Y range
	// of the rectangle, we have an intersection.  Otherwise, we don't.

	// c1tag = vertical intersection class of the endpoint segment
	//
	// Choose the y tag of the endpoint that was not on the same
	// side of the rectangle as the subsegment calculated above.
	// Note that we can "steal" the existing Y tag of that endpoint
	// since it will be provably the same as the vertical intersection.
	c1tag = ((c1tag * x1tag <= 0) ? y1tag : y2tag);

	// Now we have to calculate an array of solutions of the curve
	// with the "near vertical side" of the rectangle.  Then we
	// need to sort the tags and do a pairwise range test to see
	// if either of the pairs of crossings spans the Y range of
	// the rectangle.
	//
	// Note that the c2tag can still tell us which vertical edge
	// to test against.
	fillEqn(eqn, (c2tag < INSIDE ? x : x+w), x1, ctrlx1, ctrlx2, x2);
	int num = solveCubic(eqn, res);
	num = evalCubic(res, num, true, true, null, y1, ctrly1, ctrly2, y2);

	// Now put all of the tags into a bucket and sort them.  There
	// is an intersection iff one of the pairs of tags "spans" the
	// Y range of the rectangle.
	int tags[] = new int[num+1];
	for (int i = 0; i < num; i++) {
	    tags[i] = getTag(res[i], y, y+h);
	}
	tags[num] = c1tag;
	Arrays.sort(tags);
	return ((num >= 1 && tags[0] * tags[1] <= 0) ||
		(num >= 3 && tags[2] * tags[3] <= 0));
    
public booleanintersects(java.awt.geom.Rectangle2D r)
Tests if the shape intersects the interior of a specified Rectangle2D.

param
r the specified Rectangle2D to be tested
return
true if the shape intersects the interior of the specified Rectangle2D; false otherwise.

	return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
    
private static booleaninwards(int pttag, int opt1tag, int opt2tag)

	switch (pttag) {
	case BELOW:
	case ABOVE:
	default:
	    return false;
	case LOWEDGE:
	    return (opt1tag >= INSIDE || opt2tag >= INSIDE);
	case INSIDE:
	    return true;
	case HIGHEDGE:
	    return (opt1tag <= INSIDE || opt2tag <= INSIDE);
	}
    
public abstract voidsetCurve(double x1, double y1, double ctrlx1, double ctrly1, double ctrlx2, double ctrly2, double x2, double y2)
Sets the location of the endpoints and controlpoints of this curve to the specified double coordinates.

param
x1, y1 the first specified coordinates used to set the start point of this CubicCurve2D
param
ctrlx1, ctrly1 the second specified coordinates used to set the first control point of this CubicCurve2D
param
ctrlx2, ctrly2 the third specified coordinates used to set the second control point of this CubicCurve2D
param
x2, y2 the fourth specified coordinates used to set the end point of this CubicCurve2D

public voidsetCurve(double[] coords, int offset)
Sets the location of the endpoints and controlpoints of this curve to the double coordinates at the specified offset in the specified array.

param
coords a double array containing coordinates
param
offset the index of coords at which to begin setting the endpoints and controlpoints of this curve to the coordinates contained in coords

	setCurve(coords[offset + 0], coords[offset + 1],
		 coords[offset + 2], coords[offset + 3],
		 coords[offset + 4], coords[offset + 5],
		 coords[offset + 6], coords[offset + 7]);
    
public voidsetCurve(java.awt.geom.Point2D p1, java.awt.geom.Point2D cp1, java.awt.geom.Point2D cp2, java.awt.geom.Point2D p2)
Sets the location of the endpoints and controlpoints of this curve to the specified Point2D coordinates.

param
p1 the first specified Point2D used to set the start point of this curve
param
cp1 the second specified Point2D used to set the first control point of this curve
param
cp2 the third specified Point2D used to set the second control point of this curve
param
p2 the fourth specified Point2D used to set the end point of this curve

	setCurve(p1.getX(), p1.getY(), cp1.getX(), cp1.getY(),
		 cp2.getX(), cp2.getY(), p2.getX(), p2.getY());
    
public voidsetCurve(java.awt.geom.Point2D[] pts, int offset)
Sets the location of the endpoints and controlpoints of this curve to the coordinates of the Point2D objects at the specified offset in the specified array.

param
pts an array of Point2D objects
param
offset the index of pts at which to begin setting the endpoints and controlpoints of this curve to the points contained in pts

	setCurve(pts[offset + 0].getX(), pts[offset + 0].getY(),
		 pts[offset + 1].getX(), pts[offset + 1].getY(),
		 pts[offset + 2].getX(), pts[offset + 2].getY(),
		 pts[offset + 3].getX(), pts[offset + 3].getY());
    
public voidsetCurve(java.awt.geom.CubicCurve2D c)
Sets the location of the endpoints and controlpoints of this curve to the same as those in the specified CubicCurve2D.

param
c the specified CubicCurve2D

	setCurve(c.getX1(), c.getY1(), c.getCtrlX1(), c.getCtrlY1(),
		 c.getCtrlX2(), c.getCtrlY2(), c.getX2(), c.getY2());
    
public static intsolveCubic(double[] eqn)
Solves the cubic whose coefficients are in the eqn array and places the non-complex roots back into the same array, returning the number of roots. The solved cubic is represented by the equation:
eqn = {c, b, a, d}
dx^3 + ax^2 + bx + c = 0
A return value of -1 is used to distinguish a constant equation that might be always 0 or never 0 from an equation that has no zeroes.

param
eqn an array containing coefficients for a cubic
return
the number of roots, or -1 if the equation is a constant.

	return solveCubic(eqn, eqn);
    
public static intsolveCubic(double[] eqn, double[] res)
Solve the cubic whose coefficients are in the eqn array and place the non-complex roots into the res array, returning the number of roots. The cubic solved is represented by the equation: eqn = {c, b, a, d} dx^3 + ax^2 + bx + c = 0 A return value of -1 is used to distinguish a constant equation, which may be always 0 or never 0, from an equation which has no zeroes.

param
eqn the specified array of coefficients to use to solve the cubic equation
param
res the array that contains the non-complex roots resulting from the solution of the cubic equation
return
the number of roots, or -1 if the equation is a constant

	// From Numerical Recipes, 5.6, Quadratic and Cubic Equations
	double d = eqn[3];
	if (d == 0.0) {
	    // The cubic has degenerated to quadratic (or line or ...).
	    return QuadCurve2D.solveQuadratic(eqn, res);
	}
	double a = eqn[2] / d;
	double b = eqn[1] / d;
	double c = eqn[0] / d;
	int roots = 0;
	double Q = (a * a - 3.0 * b) / 9.0;
	double R = (2.0 * a * a * a - 9.0 * a * b + 27.0 * c) / 54.0;
	double R2 = R * R;
	double Q3 = Q * Q * Q;
	a = a / 3.0;
	if (R2 < Q3) {
	    double theta = Math.acos(R / Math.sqrt(Q3));
	    Q = -2.0 * Math.sqrt(Q);
	    if (res == eqn) {
		// Copy the eqn so that we don't clobber it with the
		// roots.  This is needed so that fixRoots can do its
		// work with the original equation.
		eqn = new double[4];
		System.arraycopy(res, 0, eqn, 0, 4);
	    }
	    res[roots++] = Q * Math.cos(theta / 3.0) - a;
	    res[roots++] = Q * Math.cos((theta + Math.PI * 2.0)/ 3.0) - a;
	    res[roots++] = Q * Math.cos((theta - Math.PI * 2.0)/ 3.0) - a;
	    fixRoots(res, eqn);
	} else {
	    boolean neg = (R < 0.0);
	    double S = Math.sqrt(R2 - Q3);
	    if (neg) {
		R = -R;
	    }
	    double A = Math.pow(R + S, 1.0 / 3.0);
	    if (!neg) {
		A = -A;
	    }
	    double B = (A == 0.0) ? 0.0 : (Q / A);
	    res[roots++] = (A + B) - a;
	}
	return roots;
    
private static doublesolveEqn(double[] eqn, int order, double t)

	double v = eqn[order];
	while (--order >= 0) {
	    v = v * t + eqn[order];
	}
	return v;
    
public voidsubdivide(java.awt.geom.CubicCurve2D left, java.awt.geom.CubicCurve2D right)
Subdivides this cubic curve and stores the resulting two subdivided curves into the left and right curve parameters. Either or both of the left and right objects may be the same as this object or null.

param
left the cubic curve object for storing for the left or first half of the subdivided curve
param
right the cubic curve object for storing for the right or second half of the subdivided curve

	subdivide(this, left, right);
    
public static voidsubdivide(java.awt.geom.CubicCurve2D src, java.awt.geom.CubicCurve2D left, java.awt.geom.CubicCurve2D right)
Subdivides the cubic curve specified by the src parameter and stores the resulting two subdivided curves into the left and right curve parameters. Either or both of the left and right objects may be the same as the src object or null.

param
src the cubic curve to be subdivided
param
left the cubic curve object for storing the left or first half of the subdivided curve
param
right the cubic curve object for storing the right or second half of the subdivided curve

	double x1 = src.getX1();
	double y1 = src.getY1();
	double ctrlx1 = src.getCtrlX1();
	double ctrly1 = src.getCtrlY1();
	double ctrlx2 = src.getCtrlX2();
	double ctrly2 = src.getCtrlY2();
	double x2 = src.getX2();
	double y2 = src.getY2();
	double centerx = (ctrlx1 + ctrlx2) / 2.0;
	double centery = (ctrly1 + ctrly2) / 2.0;
	ctrlx1 = (x1 + ctrlx1) / 2.0;
	ctrly1 = (y1 + ctrly1) / 2.0;
	ctrlx2 = (x2 + ctrlx2) / 2.0;
	ctrly2 = (y2 + ctrly2) / 2.0;
	double ctrlx12 = (ctrlx1 + centerx) / 2.0;
	double ctrly12 = (ctrly1 + centery) / 2.0;
	double ctrlx21 = (ctrlx2 + centerx) / 2.0;
	double ctrly21 = (ctrly2 + centery) / 2.0;
	centerx = (ctrlx12 + ctrlx21) / 2.0;
	centery = (ctrly12 + ctrly21) / 2.0;
	if (left != null) {
	    left.setCurve(x1, y1, ctrlx1, ctrly1,
			  ctrlx12, ctrly12, centerx, centery);
	}
	if (right != null) {
	    right.setCurve(centerx, centery, ctrlx21, ctrly21,
			   ctrlx2, ctrly2, x2, y2);
	}
    
public static voidsubdivide(double[] src, int srcoff, double[] left, int leftoff, double[] right, int rightoff)
Subdivides the cubic curve specified by the coordinates stored in the src array at indices srcoff through (srcoff + 7) and stores the resulting two subdivided curves into the two result arrays at the corresponding indices. Either or both of the left and right arrays may be null or a reference to the same array as the src array. Note that the last point in the first subdivided curve is the same as the first point in the second subdivided curve. Thus, it is possible to pass the same array for left and right and to use offsets, such as rightoff equals (leftoff + 6), in order to avoid allocating extra storage for this common point.

param
src the array holding the coordinates for the source curve
param
srcoff the offset into the array of the beginning of the the 6 source coordinates
param
left the array for storing the coordinates for the first half of the subdivided curve
param
leftoff the offset into the array of the beginning of the the 6 left coordinates
param
right the array for storing the coordinates for the second half of the subdivided curve
param
rightoff the offset into the array of the beginning of the the 6 right coordinates

	double x1 = src[srcoff + 0];
	double y1 = src[srcoff + 1];
	double ctrlx1 = src[srcoff + 2];
	double ctrly1 = src[srcoff + 3];
	double ctrlx2 = src[srcoff + 4];
	double ctrly2 = src[srcoff + 5];
	double x2 = src[srcoff + 6];
	double y2 = src[srcoff + 7];
	if (left != null) {
	    left[leftoff + 0] = x1;
	    left[leftoff + 1] = y1;
	}
	if (right != null) {
	    right[rightoff + 6] = x2;
	    right[rightoff + 7] = y2;
	}
	x1 = (x1 + ctrlx1) / 2.0;
	y1 = (y1 + ctrly1) / 2.0;
	x2 = (x2 + ctrlx2) / 2.0;
	y2 = (y2 + ctrly2) / 2.0;
	double centerx = (ctrlx1 + ctrlx2) / 2.0;
	double centery = (ctrly1 + ctrly2) / 2.0;
	ctrlx1 = (x1 + centerx) / 2.0;
	ctrly1 = (y1 + centery) / 2.0;
	ctrlx2 = (x2 + centerx) / 2.0;
	ctrly2 = (y2 + centery) / 2.0;
	centerx = (ctrlx1 + ctrlx2) / 2.0;
	centery = (ctrly1 + ctrly2) / 2.0;
	if (left != null) {
	    left[leftoff + 2] = x1;
	    left[leftoff + 3] = y1;
	    left[leftoff + 4] = ctrlx1;
	    left[leftoff + 5] = ctrly1;
	    left[leftoff + 6] = centerx;
	    left[leftoff + 7] = centery;
	}
	if (right != null) {
	    right[rightoff + 0] = centerx;
	    right[rightoff + 1] = centery;
	    right[rightoff + 2] = ctrlx2;
	    right[rightoff + 3] = ctrly2;
	    right[rightoff + 4] = x2;
	    right[rightoff + 5] = y2;
	}