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.
This commit is contained in:
Michael Bayne
2012-05-11 10:52:35 -07:00
parent d132e917ed
commit f2e7e7da88
12 changed files with 78 additions and 52 deletions
@@ -58,17 +58,17 @@ public abstract class AbstractPoint implements IPoint
} }
@Override // from IPoint @Override // from IPoint
public Vector subtract (double x, double y) { public Point subtract (double x, double y) {
return subtract(x, y, new Vector()); return subtract(x, y, new Point());
} }
@Override @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); return result.set(x() - x, y() - y);
} }
@Override @Override
public Vector subtract (IPoint other, Vector result) { public Point subtract (IPoint other, Point result) {
return subtract(other.x(), other.y(), result); return subtract(other.x(), other.y(), result);
} }
+5 -11
View File
@@ -7,14 +7,8 @@ package pythagoras.d;
/** /**
* Provides read-only access to a {@link Point}. * 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. */ /** Returns the squared Euclidian distance between this point and the specified point. */
double distanceSq (double px, double py); double distanceSq (double px, double py);
@@ -48,16 +42,16 @@ public interface IPoint extends Cloneable
Point add (double x, double y, Point result); Point add (double x, double y, Point result);
/** Subtracts the supplied point from {@code this}. /** Subtracts the supplied point from {@code this}.
* @return a new vector containing the result. */ * @return a new point containing the result. */
Vector subtract (double x, double y); Point subtract (double x, double y);
/** Subtracts the supplied point from {@code this} and stores the result in {@code result}. /** Subtracts the supplied point from {@code this} and stores the result in {@code result}.
* @return a reference to the result, for chaining. */ * @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}. /** Subtracts the supplied point from {@code this} and stores the result in {@code result}.
* @return a reference to the result, for chaining. */ * @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. /** Rotates this point around the origin by the specified angle.
* @return a new point containing the result. */ * @return a new point containing the result. */
+1 -7
View File
@@ -7,14 +7,8 @@ package pythagoras.d;
/** /**
* Provides read-only access to a {@link Vector}. * 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. */ /** Computes and returns the dot product of this and the specified other vector. */
double dot (IVector other); double dot (IVector other);
+8 -2
View File
@@ -69,12 +69,18 @@ public class Point extends AbstractPoint implements Serializable
return rotate(angle, this); 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 () { public double x () {
return x; return x;
} }
@Override // from interface IPoint @Override // from XY
public double y () { public double y () {
return y; return y;
} }
+2 -2
View File
@@ -139,12 +139,12 @@ public class Vector extends AbstractVector
return normalizeLocal().scaleLocal(length); return normalizeLocal().scaleLocal(length);
} }
@Override // from AbstractVector @Override // from XY
public double x () { public double x () {
return x; return x;
} }
@Override // from AbstractVector @Override // from XY
public double y () { public double y () {
return y; return y;
} }
+19
View File
@@ -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 ();
}
@@ -58,17 +58,17 @@ public abstract class AbstractPoint implements IPoint
} }
@Override // from IPoint @Override // from IPoint
public Vector subtract (float x, float y) { public Point subtract (float x, float y) {
return subtract(x, y, new Vector()); return subtract(x, y, new Point());
} }
@Override @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); return result.set(x() - x, y() - y);
} }
@Override @Override
public Vector subtract (IPoint other, Vector result) { public Point subtract (IPoint other, Point result) {
return subtract(other.x(), other.y(), result); return subtract(other.x(), other.y(), result);
} }
+5 -11
View File
@@ -7,14 +7,8 @@ package pythagoras.f;
/** /**
* Provides read-only access to a {@link Point}. * 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. */ /** Returns the squared Euclidian distance between this point and the specified point. */
float distanceSq (float px, float py); float distanceSq (float px, float py);
@@ -48,16 +42,16 @@ public interface IPoint extends Cloneable
Point add (float x, float y, Point result); Point add (float x, float y, Point result);
/** Subtracts the supplied point from {@code this}. /** Subtracts the supplied point from {@code this}.
* @return a new vector containing the result. */ * @return a new point containing the result. */
Vector subtract (float x, float y); Point subtract (float x, float y);
/** Subtracts the supplied point from {@code this} and stores the result in {@code result}. /** Subtracts the supplied point from {@code this} and stores the result in {@code result}.
* @return a reference to the result, for chaining. */ * @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}. /** Subtracts the supplied point from {@code this} and stores the result in {@code result}.
* @return a reference to the result, for chaining. */ * @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. /** Rotates this point around the origin by the specified angle.
* @return a new point containing the result. */ * @return a new point containing the result. */
+1 -7
View File
@@ -7,14 +7,8 @@ package pythagoras.f;
/** /**
* Provides read-only access to a {@link Vector}. * 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. */ /** Computes and returns the dot product of this and the specified other vector. */
float dot (IVector other); float dot (IVector other);
+8 -2
View File
@@ -69,12 +69,18 @@ public class Point extends AbstractPoint implements Serializable
return rotate(angle, this); 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 () { public float x () {
return x; return x;
} }
@Override // from interface IPoint @Override // from XY
public float y () { public float y () {
return y; return y;
} }
+2 -2
View File
@@ -139,12 +139,12 @@ public class Vector extends AbstractVector
return normalizeLocal().scaleLocal(length); return normalizeLocal().scaleLocal(length);
} }
@Override // from AbstractVector @Override // from XY
public float x () { public float x () {
return x; return x;
} }
@Override // from AbstractVector @Override // from XY
public float y () { public float y () {
return y; return y;
} }
+19
View File
@@ -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 ();
}