From 7048d8231be79cdefe39585aaaa43cd02c9f396f Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 30 Apr 2012 15:07:44 -0700 Subject: [PATCH] Added Vector.cross() & co. Closes #10. --- src/main/java/pythagoras/f/AbstractVector.java | 11 +++++++++++ src/main/java/pythagoras/f/IVector.java | 9 +++++++++ src/main/java/pythagoras/f/Vector.java | 7 +++++++ 3 files changed, 27 insertions(+) diff --git a/src/main/java/pythagoras/f/AbstractVector.java b/src/main/java/pythagoras/f/AbstractVector.java index 0d048bb..7b7b76e 100644 --- a/src/main/java/pythagoras/f/AbstractVector.java +++ b/src/main/java/pythagoras/f/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) { + float 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()); diff --git a/src/main/java/pythagoras/f/IVector.java b/src/main/java/pythagoras/f/IVector.java index 0dd4fac..a7147b8 100644 --- a/src/main/java/pythagoras/f/IVector.java +++ b/src/main/java/pythagoras/f/IVector.java @@ -18,6 +18,15 @@ public interface IVector /** Computes and returns the dot product of this and the specified other vector. */ float 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 (); diff --git a/src/main/java/pythagoras/f/Vector.java b/src/main/java/pythagoras/f/Vector.java index 09eec17..6b6d98d 100644 --- a/src/main/java/pythagoras/f/Vector.java +++ b/src/main/java/pythagoras/f/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 () {