diff --git a/src/main/java/pythagoras/f/AbstractCircle.java b/src/main/java/pythagoras/f/AbstractCircle.java new file mode 100644 index 0000000..71e507c --- /dev/null +++ b/src/main/java/pythagoras/f/AbstractCircle.java @@ -0,0 +1,62 @@ +// +// Pythagoras - a collection of geometry classes +// http://github.com/samskivert/pythagoras + +package pythagoras.f; + +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) { + return Points.distanceSq(x(), y(), c.x(), c.y()) < (radius() + c.radius()); + } + + @Override // from ICircle + public boolean contains (IPoint p) { + return Points.distanceSq(x(), y(), p.x(), p.y()) < radius(); + } + + @Override // from ICircle + public boolean contains (float x, float y) { + return Points.distanceSq(x(), y(), x, y) < radius(); + } + + @Override // from ICircle + public Circle offset (float x, float y) { + return new Circle(x() + x, y() + y, radius()); + } + + @Override // from ICircle + public Circle offset (float x, float 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()); + } +} diff --git a/src/main/java/pythagoras/f/Circle.java b/src/main/java/pythagoras/f/Circle.java new file mode 100644 index 0000000..1c2e39d --- /dev/null +++ b/src/main/java/pythagoras/f/Circle.java @@ -0,0 +1,79 @@ +// +// Pythagoras - a collection of geometry classes +// http://github.com/samskivert/pythagoras + +package pythagoras.f; + +import java.io.Serializable; + +/** + * Represents a circle on a plane. + */ +public class Circle extends AbstractCircle implements Serializable +{ + /** The x-coordinate of the circle. */ + public float x; + + /** The y-coordinate of the circle. */ + public float y; + + /** The radius of the circle. */ + public float radius; + + /** + * Constructs a circle at (0, 0) with radius 0 + */ + public Circle () { + } + + /** + * Constructs a circle with the specified properties + */ + public Circle (float x, float y, float radius) { + set(x, y, radius); + } + + /** + * Constructs a circle with the specified properties + */ + public Circle (IPoint p, float 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 (float x, float y, float radius) { + this.x = x; + this.y = y; + this.radius = radius; + return this; + } + + @Override + public float x () { + return x; + } + + @Override + public float y () { + return y; + } + + @Override + public float radius () { + return radius; + } +} diff --git a/src/main/java/pythagoras/f/ICircle.java b/src/main/java/pythagoras/f/ICircle.java new file mode 100644 index 0000000..a1a9b92 --- /dev/null +++ b/src/main/java/pythagoras/f/ICircle.java @@ -0,0 +1,42 @@ +// +// Pythagoras - a collection of geometry classes +// http://github.com/samskivert/pythagoras + +package pythagoras.f; + +/** + * Provides read-only access to a {@link Circle}. + */ +public interface ICircle +{ + /** Returns this circle's x-coordinate. */ + float x (); + + /** Returns this circle's y-coordinate. */ + float y (); + + /** Returns this circle's radius. */ + float 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 (float x, float y); + + /** Translates the circle by the specified offset. + * @return a new Circle containing the result. + */ + Circle offset (float x, float 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 (float x, float y, Circle result); + + /** Returns a mutable copy of this circle. */ + Circle clone (); +}