From 8524f5c9117174a558d66f7d24e180d344706077 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 30 Apr 2012 15:14:35 -0700 Subject: [PATCH] Updated double versions. --- .../java/pythagoras/d/AbstractVector.java | 21 +++++++++++++++++++ src/main/java/pythagoras/d/IVector.java | 17 +++++++++++++++ src/main/java/pythagoras/d/Vector.java | 7 +++++++ 3 files changed, 45 insertions(+) diff --git a/src/main/java/pythagoras/d/AbstractVector.java b/src/main/java/pythagoras/d/AbstractVector.java index 9712cc2..f870d91 100644 --- a/src/main/java/pythagoras/d/AbstractVector.java +++ b/src/main/java/pythagoras/d/AbstractVector.java @@ -17,6 +17,17 @@ public abstract class AbstractVector implements IVector return x()*other.x() + y()*other.y(); } + @Override // from interface IVector + public Vector cross (IVector other) { + return cross(other, new Vector()); + } + + @Override // from interface IVector + public Vector cross (IVector other, Vector result) { + double x = x(), y = y(), ox = other.x(), oy = other.y(); + return result.set(y*ox - x*oy, x*oy - y*ox); + } + @Override // from interface IVector public Vector negate () { return negate(new Vector()); @@ -125,6 +136,16 @@ public abstract class AbstractVector implements IVector return result.set(x() + x, y() + y); } + @Override // from interface IVector + public Vector subtract (double x, double y) { + return subtract(x, y, new Vector()); + } + + @Override // from interface IVector + public Vector subtract (double x, double y, Vector result) { + return result.set(x() - x, y() - y); + } + @Override // from interface IVector public Vector addScaled (IVector other, double v) { return addScaled(other, v, new Vector()); diff --git a/src/main/java/pythagoras/d/IVector.java b/src/main/java/pythagoras/d/IVector.java index afecbd7..79ae9b3 100644 --- a/src/main/java/pythagoras/d/IVector.java +++ b/src/main/java/pythagoras/d/IVector.java @@ -18,6 +18,15 @@ public interface IVector /** Computes and returns the dot product of this and the specified other vector. */ double dot (IVector other); + /** Computes the cross product of this and the specified other vector. + * @return a new vector containing the result. */ + Vector cross (IVector other); + + /** Computes the cross product of this and the specified other vector, placing the result in + * the object supplied. + * @return a reference to the result, for chaining. */ + Vector cross (IVector other, Vector result); + /** Negates this vector. * @return a new vector containing the result. */ Vector negate (); @@ -106,6 +115,14 @@ public interface IVector * @return a reference to the result, for chaining. */ Vector subtract (IVector other, Vector result); + /** Subtracts a vector from this one. + * @return a new vector containing the result. */ + Vector subtract (double x, double y); + + /** Subtracts a vector from this one and places the result in the supplied object. + * @return a reference to the result, for chaining. */ + Vector subtract (double x, double y, Vector result); + /** Rotates this vector by the specified angle. * @return a new vector containing the result. */ Vector rotate (double angle); diff --git a/src/main/java/pythagoras/d/Vector.java b/src/main/java/pythagoras/d/Vector.java index b7b3e05..faf38a2 100644 --- a/src/main/java/pythagoras/d/Vector.java +++ b/src/main/java/pythagoras/d/Vector.java @@ -29,6 +29,13 @@ public class Vector extends AbstractVector public Vector () { } + /** Computes the cross product of this and the specified other vector, storing the result in + * this vector. + * @return a reference to this vector, for chaining. */ + public Vector crossLocal (IVector other) { + return cross(other, this); + } + /** Negates this vector in-place. * @return a reference to this vector, for chaining. */ public Vector negateLocal () {