From 0ff90d641e1783b4b37a33d667e65d504320704b Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 24 May 2013 06:10:03 -0700 Subject: [PATCH] A bunch of add/substract to syncmake i.Point with f/d.Point. Closes #24. --- src/main/java/pythagoras/i/AbstractPoint.java | 33 ++++++++++++++++--- src/main/java/pythagoras/i/IPoint.java | 20 +++++++++++ src/main/java/pythagoras/i/Point.java | 26 +++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/src/main/java/pythagoras/i/AbstractPoint.java b/src/main/java/pythagoras/i/AbstractPoint.java index 9f653f2..0c24909 100644 --- a/src/main/java/pythagoras/i/AbstractPoint.java +++ b/src/main/java/pythagoras/i/AbstractPoint.java @@ -10,26 +10,51 @@ package pythagoras.i; */ public abstract class AbstractPoint implements IPoint { - @Override // from interface IPoint + @Override // from IPoint public int distanceSq (int px, int py) { return Points.distanceSq(x(), y(), px, py); } - @Override // from interface IPoint + @Override // from IPoint public int distanceSq (IPoint p) { return Points.distanceSq(x(), y(), p.x(), p.y()); } - @Override // from interface IPoint + @Override // from IPoint public int distance (int px, int py) { return Points.distance(x(), y(), px, py); } - @Override // from interface IPoint + @Override // from IPoint public int distance (IPoint p) { return Points.distance(x(), y(), p.x(), p.y()); } + @Override // from IPoint + public Point add (int x, int y) { + return new Point(x() + x, y() + y); + } + + @Override // from IPoint + public Point add (int x, int y, Point result) { + return result.set(x() + x, y() + y); + } + + @Override // from IPoint + public Point subtract (int x, int y) { + return subtract(x, y, new Point()); + } + + @Override + public Point subtract (int x, int y, Point result) { + return result.set(x() - x, y() - y); + } + + @Override + public Point subtract (IPoint other, Point result) { + return subtract(other.x(), other.y(), result); + } + @Override // from interface IPoint public Point clone () { return new Point(this); diff --git a/src/main/java/pythagoras/i/IPoint.java b/src/main/java/pythagoras/i/IPoint.java index a1fe9fb..b8dc24e 100644 --- a/src/main/java/pythagoras/i/IPoint.java +++ b/src/main/java/pythagoras/i/IPoint.java @@ -27,6 +27,26 @@ public interface IPoint extends Cloneable /** Returns the Euclidian distance between this point and the supplied point. */ int distance (IPoint p); + /** Translates this point by the specified offset. + * @return a new point containing the result. */ + Point add (int x, int y); + + /** Translates this point by the specified offset and stores the result in the object provided. + * @return a reference to the result, for chaining. */ + Point add (int x, int y, Point result); + + /** Subtracts the supplied point from {@code this}. + * @return a new point containing the result. */ + Point subtract (int x, int y); + + /** Subtracts the supplied point from {@code this} and stores the result in {@code result}. + * @return a reference to the result, for chaining. */ + Point subtract (int x, int 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. */ + Point subtract (IPoint other, Point result); + /** Returns a mutable copy of this point. */ Point clone (); } diff --git a/src/main/java/pythagoras/i/Point.java b/src/main/java/pythagoras/i/Point.java index b79f893..d816045 100644 --- a/src/main/java/pythagoras/i/Point.java +++ b/src/main/java/pythagoras/i/Point.java @@ -67,6 +67,32 @@ public class Point extends AbstractPoint implements Serializable y += dy; } + /** Sets the coordinates of this point to be equal to those of the supplied point. + * @return a reference to this this, for chaining. */ + public Point set (IPoint p) { + return set(p.x(), p.y()); + } + + /** Sets the coordinates of this point to the supplied values. + * @return a reference to this this, for chaining. */ + public Point set (int x, int y) { + this.x = x; + this.y = y; + return this; + } + + /** Translates this point by the specified offset. + * @return a reference to this point, for chaining. */ + public Point addLocal (int dx, int dy) { + return add(dx, dy, this); + } + + /** Subtracts the supplied x/y from this point. + * @return a reference to this point, for chaining. */ + public Point subtractLocal (int x, int y) { + return subtract(x, y, this); + } + @Override // from interface IPoint public int x () { return x;