Version of circle code specialized on double.

This commit is contained in:
Michael Bayne
2011-09-09 10:08:49 -07:00
parent 2753a9523d
commit c6321e4573
3 changed files with 184 additions and 0 deletions
@@ -0,0 +1,65 @@
//
// Pythagoras - a collection of geometry classes
// http://github.com/samskivert/pythagoras
package pythagoras.d;
import pythagoras.util.Platform;
/**
* Provides most of the implementation of {@link ICircle}, obtaining only the location and radius
* from the derived class.
*/
public abstract class AbstractCircle implements ICircle
{
@Override // from ICircle
public boolean intersects (ICircle c) {
double maxDist = radius() + c.radius();
return Points.distanceSq(x(), y(), c.x(), c.y()) < (maxDist * maxDist);
}
@Override // from ICircle
public boolean contains (IPoint p) {
double r = radius();
return Points.distanceSq(x(), y(), p.x(), p.y()) < r * r;
}
@Override // from ICircle
public boolean contains (double x, double y) {
double r = radius();
return Points.distanceSq(x(), y(), x, y) < r * r;
}
@Override // from ICircle
public Circle offset (double x, double y) {
return new Circle(x() + x, y() + y, radius());
}
@Override // from ICircle
public Circle offset (double x, double y, Circle result) {
result.set(x() + x, y() + y, radius());
return result;
}
@Override // from ICircle
public Circle clone () {
return new Circle(this);
}
@Override
public boolean equals (Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof AbstractCircle) {
AbstractCircle c = (AbstractCircle)obj;
return x() == c.x() && y() == c.y() && radius() == c.radius();
}
return false;
}
@Override
public int hashCode () {
return Platform.hashCode(x()) ^ Platform.hashCode(y()) ^ Platform.hashCode(radius());
}
}
+79
View File
@@ -0,0 +1,79 @@
//
// Pythagoras - a collection of geometry classes
// http://github.com/samskivert/pythagoras
package pythagoras.d;
import java.io.Serializable;
/**
* Represents a circle on a plane.
*/
public class Circle extends AbstractCircle implements Serializable
{
/** The x-coordinate of the circle. */
public double x;
/** The y-coordinate of the circle. */
public double y;
/** The radius of the circle. */
public double radius;
/**
* Constructs a circle at (0, 0) with radius 0
*/
public Circle () {
}
/**
* Constructs a circle with the specified properties
*/
public Circle (double x, double y, double radius) {
set(x, y, radius);
}
/**
* Constructs a circle with the specified properties
*/
public Circle (IPoint p, double radius) {
this(p.x(), p.y(), radius);
}
/**
* Constructs a circle with properties equal to the supplied circle.
*/
public Circle (ICircle c) {
this(c.x(), c.y(), c.radius());
}
/** Sets the properties of this circle to be equal to those of the supplied circle.
* @return a reference to this this, for chaining. */
public Circle set (ICircle c) {
return set(c.x(), c.y(), c.radius());
}
/** Sets the properties of this circle to the supplied values.
* @return a reference to this this, for chaining. */
public Circle set (double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
return this;
}
@Override
public double x () {
return x;
}
@Override
public double y () {
return y;
}
@Override
public double radius () {
return radius;
}
}
+40
View File
@@ -0,0 +1,40 @@
//
// Pythagoras - a collection of geometry classes
// http://github.com/samskivert/pythagoras
package pythagoras.d;
/**
* Provides read-only access to a {@link Circle}.
*/
public interface ICircle
{
/** Returns this circle's x-coordinate. */
double x ();
/** Returns this circle's y-coordinate. */
double y ();
/** Returns this circle's radius. */
double radius ();
/** Returns true if this circle intersects the supplied circle. */
boolean intersects (ICircle c);
/** Returns true if this circle contains the supplied point. */
boolean contains (IPoint p);
/** Returns true if this circle contains the specified point. */
boolean contains (double x, double y);
/** Translates the circle by the specified offset.
* @return a new Circle containing the result. */
Circle offset (double x, double y);
/** Translates the circle by the specified offset and stores the result in the supplied object.
* @return a reference to the result, for chaining. */
Circle offset (double x, double y, Circle result);
/** Returns a mutable copy of this circle. */
Circle clone ();
}