From f2e7e7da8828a2a691f3b444a6760df8672f67f9 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 11 May 2012 10:52:35 -0700 Subject: [PATCH] Added XY, made IPoint.subtract return Point. XY provides a common super-interface for IPoint and IVector for APIs that don't care if you have a point or vector, and just want it's x/y coordinates. Returning a vector when subtracting two points was clever and all, but turns out to be annoying. More often, you're just adjusting a point and don't want to switch to vector-baesd math. If we want an easy way to obtain a vector that is the difference of two points we can add Vectors.subtract() or a new constructor. --- src/main/java/pythagoras/d/AbstractPoint.java | 8 ++++---- src/main/java/pythagoras/d/IPoint.java | 16 +++++----------- src/main/java/pythagoras/d/IVector.java | 8 +------- src/main/java/pythagoras/d/Point.java | 10 ++++++++-- src/main/java/pythagoras/d/Vector.java | 4 ++-- src/main/java/pythagoras/d/XY.java | 19 +++++++++++++++++++ src/main/java/pythagoras/f/AbstractPoint.java | 8 ++++---- src/main/java/pythagoras/f/IPoint.java | 16 +++++----------- src/main/java/pythagoras/f/IVector.java | 8 +------- src/main/java/pythagoras/f/Point.java | 10 ++++++++-- src/main/java/pythagoras/f/Vector.java | 4 ++-- src/main/java/pythagoras/f/XY.java | 19 +++++++++++++++++++ 12 files changed, 78 insertions(+), 52 deletions(-) create mode 100644 src/main/java/pythagoras/d/XY.java create mode 100644 src/main/java/pythagoras/f/XY.java diff --git a/src/main/java/pythagoras/d/AbstractPoint.java b/src/main/java/pythagoras/d/AbstractPoint.java index d1ebe4b..cf9c279 100644 --- a/src/main/java/pythagoras/d/AbstractPoint.java +++ b/src/main/java/pythagoras/d/AbstractPoint.java @@ -58,17 +58,17 @@ public abstract class AbstractPoint implements IPoint } @Override // from IPoint - public Vector subtract (double x, double y) { - return subtract(x, y, new Vector()); + public Point subtract (double x, double y) { + return subtract(x, y, new Point()); } @Override - public Vector subtract (double x, double y, Vector result) { + public Point subtract (double x, double y, Point result) { return result.set(x() - x, y() - y); } @Override - public Vector subtract (IPoint other, Vector result) { + public Point subtract (IPoint other, Point result) { return subtract(other.x(), other.y(), result); } diff --git a/src/main/java/pythagoras/d/IPoint.java b/src/main/java/pythagoras/d/IPoint.java index 1a9a13d..cf7b4c8 100644 --- a/src/main/java/pythagoras/d/IPoint.java +++ b/src/main/java/pythagoras/d/IPoint.java @@ -7,14 +7,8 @@ package pythagoras.d; /** * Provides read-only access to a {@link Point}. */ -public interface IPoint extends Cloneable +public interface IPoint extends XY, Cloneable { - /** Returns this point's x-coordinate. */ - double x (); - - /** Returns this point's y-coordinate. */ - double y (); - /** Returns the squared Euclidian distance between this point and the specified point. */ double distanceSq (double px, double py); @@ -48,16 +42,16 @@ public interface IPoint extends Cloneable Point add (double x, double y, Point result); /** Subtracts the supplied point from {@code this}. - * @return a new vector containing the result. */ - Vector subtract (double x, double y); + * @return a new point containing the result. */ + Point subtract (double x, double y); /** Subtracts the supplied point from {@code this} and stores the result in {@code result}. * @return a reference to the result, for chaining. */ - Vector subtract (double x, double y, Vector result); + Point subtract (double x, double y, Point result); /** Subtracts the supplied point from {@code this} and stores the result in {@code result}. * @return a reference to the result, for chaining. */ - Vector subtract (IPoint other, Vector result); + Point subtract (IPoint other, Point result); /** Rotates this point around the origin by the specified angle. * @return a new point containing the result. */ diff --git a/src/main/java/pythagoras/d/IVector.java b/src/main/java/pythagoras/d/IVector.java index 79ae9b3..804b3a4 100644 --- a/src/main/java/pythagoras/d/IVector.java +++ b/src/main/java/pythagoras/d/IVector.java @@ -7,14 +7,8 @@ package pythagoras.d; /** * Provides read-only access to a {@link Vector}. */ -public interface IVector +public interface IVector extends XY { - /** Returns the x-component of this vector. */ - double x (); - - /** Returns the y-component of this vector. */ - double y (); - /** Computes and returns the dot product of this and the specified other vector. */ double dot (IVector other); diff --git a/src/main/java/pythagoras/d/Point.java b/src/main/java/pythagoras/d/Point.java index 040fb78..4963981 100644 --- a/src/main/java/pythagoras/d/Point.java +++ b/src/main/java/pythagoras/d/Point.java @@ -69,12 +69,18 @@ public class Point extends AbstractPoint implements Serializable return rotate(angle, this); } - @Override // from interface IPoint + /** Subtracts the supplied x/y from this point. + * @return a reference to this point, for chaining. */ + public Point subtractLocal (double x, double y) { + return subtract(x, y, this); + } + + @Override // from XY public double x () { return x; } - @Override // from interface IPoint + @Override // from XY public double y () { return y; } diff --git a/src/main/java/pythagoras/d/Vector.java b/src/main/java/pythagoras/d/Vector.java index 60b918f..1dad4e7 100644 --- a/src/main/java/pythagoras/d/Vector.java +++ b/src/main/java/pythagoras/d/Vector.java @@ -139,12 +139,12 @@ public class Vector extends AbstractVector return normalizeLocal().scaleLocal(length); } - @Override // from AbstractVector + @Override // from XY public double x () { return x; } - @Override // from AbstractVector + @Override // from XY public double y () { return y; } diff --git a/src/main/java/pythagoras/d/XY.java b/src/main/java/pythagoras/d/XY.java new file mode 100644 index 0000000..9f71daa --- /dev/null +++ b/src/main/java/pythagoras/d/XY.java @@ -0,0 +1,19 @@ +// +// Pythagoras - a collection of geometry classes +// http://github.com/samskivert/pythagoras + +package pythagoras.d; + +/** + * Defines an x/y coordinate. This is implemented by both {@code Point} and {@code Vector} so that + * APIs which require an x/y coordinate, but don't really want to mak the distinction between a + * translation vector versus a point in 2D space, can simply accept both. + */ +public interface XY +{ + /** The x coordinate. */ + double x (); + + /** The y coordinate. */ + double y (); +} diff --git a/src/main/java/pythagoras/f/AbstractPoint.java b/src/main/java/pythagoras/f/AbstractPoint.java index 3bd9b1a..02d5b8a 100644 --- a/src/main/java/pythagoras/f/AbstractPoint.java +++ b/src/main/java/pythagoras/f/AbstractPoint.java @@ -58,17 +58,17 @@ public abstract class AbstractPoint implements IPoint } @Override // from IPoint - public Vector subtract (float x, float y) { - return subtract(x, y, new Vector()); + public Point subtract (float x, float y) { + return subtract(x, y, new Point()); } @Override - public Vector subtract (float x, float y, Vector result) { + public Point subtract (float x, float y, Point result) { return result.set(x() - x, y() - y); } @Override - public Vector subtract (IPoint other, Vector result) { + public Point subtract (IPoint other, Point result) { return subtract(other.x(), other.y(), result); } diff --git a/src/main/java/pythagoras/f/IPoint.java b/src/main/java/pythagoras/f/IPoint.java index a42342c..d60aca8 100644 --- a/src/main/java/pythagoras/f/IPoint.java +++ b/src/main/java/pythagoras/f/IPoint.java @@ -7,14 +7,8 @@ package pythagoras.f; /** * Provides read-only access to a {@link Point}. */ -public interface IPoint extends Cloneable +public interface IPoint extends XY, Cloneable { - /** Returns this point's x-coordinate. */ - float x (); - - /** Returns this point's y-coordinate. */ - float y (); - /** Returns the squared Euclidian distance between this point and the specified point. */ float distanceSq (float px, float py); @@ -48,16 +42,16 @@ public interface IPoint extends Cloneable Point add (float x, float y, Point result); /** Subtracts the supplied point from {@code this}. - * @return a new vector containing the result. */ - Vector subtract (float x, float y); + * @return a new point containing the result. */ + Point subtract (float x, float y); /** Subtracts the supplied point from {@code this} and stores the result in {@code result}. * @return a reference to the result, for chaining. */ - Vector subtract (float x, float y, Vector result); + Point subtract (float x, float y, Point result); /** Subtracts the supplied point from {@code this} and stores the result in {@code result}. * @return a reference to the result, for chaining. */ - Vector subtract (IPoint other, Vector result); + Point subtract (IPoint other, Point result); /** Rotates this point around the origin by the specified angle. * @return a new point containing the result. */ diff --git a/src/main/java/pythagoras/f/IVector.java b/src/main/java/pythagoras/f/IVector.java index 5f36dc9..be76622 100644 --- a/src/main/java/pythagoras/f/IVector.java +++ b/src/main/java/pythagoras/f/IVector.java @@ -7,14 +7,8 @@ package pythagoras.f; /** * Provides read-only access to a {@link Vector}. */ -public interface IVector +public interface IVector extends XY { - /** Returns the x-component of this vector. */ - float x (); - - /** Returns the y-component of this vector. */ - float y (); - /** Computes and returns the dot product of this and the specified other vector. */ float dot (IVector other); diff --git a/src/main/java/pythagoras/f/Point.java b/src/main/java/pythagoras/f/Point.java index 6ec5a9f..0bf5d2d 100644 --- a/src/main/java/pythagoras/f/Point.java +++ b/src/main/java/pythagoras/f/Point.java @@ -69,12 +69,18 @@ public class Point extends AbstractPoint implements Serializable return rotate(angle, this); } - @Override // from interface IPoint + /** Subtracts the supplied x/y from this point. + * @return a reference to this point, for chaining. */ + public Point subtractLocal (float x, float y) { + return subtract(x, y, this); + } + + @Override // from XY public float x () { return x; } - @Override // from interface IPoint + @Override // from XY public float y () { return y; } diff --git a/src/main/java/pythagoras/f/Vector.java b/src/main/java/pythagoras/f/Vector.java index 8b58008..62faecd 100644 --- a/src/main/java/pythagoras/f/Vector.java +++ b/src/main/java/pythagoras/f/Vector.java @@ -139,12 +139,12 @@ public class Vector extends AbstractVector return normalizeLocal().scaleLocal(length); } - @Override // from AbstractVector + @Override // from XY public float x () { return x; } - @Override // from AbstractVector + @Override // from XY public float y () { return y; } diff --git a/src/main/java/pythagoras/f/XY.java b/src/main/java/pythagoras/f/XY.java new file mode 100644 index 0000000..bd58415 --- /dev/null +++ b/src/main/java/pythagoras/f/XY.java @@ -0,0 +1,19 @@ +// +// Pythagoras - a collection of geometry classes +// http://github.com/samskivert/pythagoras + +package pythagoras.f; + +/** + * Defines an x/y coordinate. This is implemented by both {@code Point} and {@code Vector} so that + * APIs which require an x/y coordinate, but don't really want to mak the distinction between a + * translation vector versus a point in 2D space, can simply accept both. + */ +public interface XY +{ + /** The x coordinate. */ + float x (); + + /** The y coordinate. */ + float y (); +}