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:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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. */
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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. */
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 ();
|
||||
}
|
||||
Reference in New Issue
Block a user