FileDocCategorySizeDatePackage
Spiral.javaAPI DocExample9029Sat Jan 24 10:44:36 GMT 2004je3.graphics

Spiral

public class Spiral extends Object implements Shape
This Shape implementation represents a spiral curve

Fields Summary
double
centerX
double
centerY
double
startRadius
double
startAngle
double
endRadius
double
endAngle
double
outerRadius
int
angleDirection
Shape
approximation
Constructors Summary
public Spiral(double centerX, double centerY, double startRadius, double startAngle, double endRadius, double endAngle)
The constructor. It takes arguments for the center of the shape, the start point, and the end point. The start and end points are specified in terms of angle and radius. The spiral curve is formed by varying the angle and radius smoothly between the two end points.

	// Save the parameters that describe the spiral
	this.centerX = centerX; 	this.centerY = centerY;
	this.startRadius = startRadius;	this.startAngle = startAngle;
	this.endRadius = endRadius;	this.endAngle = endAngle;

	// figure out the maximum radius, and the spiral direction
	this.outerRadius = Math.max(startRadius, endRadius);
	if (startAngle < endAngle) angleDirection = 1;
	else angleDirection = -1;
	
	if ((startRadius < 0) || (endRadius < 0))
	    throw new IllegalArgumentException("Spiral radii must be >= 0");

	// Here's how we approximate the inside of the spiral
	approximation = new Ellipse2D.Double(centerX-outerRadius,
					     centerY-outerRadius, 
					     outerRadius*2, outerRadius*2);
    
Methods Summary
public booleancontains(java.awt.geom.Point2D p)
These methods use a circle approximation to determine whether a point or rectangle is inside the spiral. We could be more clever than this.

 return approximation.contains(p); 
public booleancontains(java.awt.geom.Rectangle2D r)

 return approximation.contains(r);
public booleancontains(double x, double y)

	return approximation.contains(x,y);
    
public booleancontains(double x, double y, double w, double h)

	return approximation.contains(x, y, w, h);
    
public java.awt.RectanglegetBounds()
The bounding box of a Spiral is the same as the bounding box of a circle with the same center and the maximum radius

	return new Rectangle((int)(centerX-outerRadius),
			     (int)(centerY-outerRadius),
			     (int)(outerRadius*2), (int)(outerRadius*2));
    
public java.awt.geom.Rectangle2DgetBounds2D()
Same as getBounds(), but with floating-point coordinates

	return new Rectangle2D.Double(centerX-outerRadius, centerY-outerRadius,
				      outerRadius*2, outerRadius*2);
    
public java.awt.geom.PathIteratorgetPathIterator(java.awt.geom.AffineTransform at)
This method is the heart of all Shape implementations. It returns a PathIterator that describes the shape in terms of the line and curve segments that comprise it. Our iterator implementation approximates the shape of the spiral using line segments only. We pass in a "flatness" argument that tells it how good the approximation must be. (smaller numbers mean a better approximation).

	return new SpiralIterator(at, outerRadius/500.0);
    
public java.awt.geom.PathIteratorgetPathIterator(java.awt.geom.AffineTransform at, double flatness)
Return a PathIterator that describes the shape in terms of line segments only, with an approximation quality specified by flatness.

	return new SpiralIterator(at, flatness);
    
public booleanintersects(double x, double y, double w, double h)
These methods determine whether the specified rectangle intersects the spiral. We use our circle approximation. The Shape interface explicitly allows approximations to be used for these methods.

	return approximation.intersects(x, y, w, h);
    
public booleanintersects(java.awt.geom.Rectangle2D r)

	return approximation.intersects(r);