From ffd2f10b24eed2f2e6aba9d09bdd57d6570b02f8 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 2 Sep 2011 15:15:06 -0700 Subject: [PATCH] Changed Vector.mult to Vector.scale and clarified the javadocs. As Tim points out, vector multiplication implies dot or cross products. These methods are scaling, not multiplying. Cross product coming in a future commit. --- .../java/pythagoras/d/AbstractVector.java | 14 +++++++------- src/main/java/pythagoras/d/IVector.java | 19 +++++++++++-------- .../pythagoras/d/NonUniformTransform.java | 2 +- .../java/pythagoras/d/UniformTransform.java | 2 +- src/main/java/pythagoras/d/Vector.java | 13 +++++++------ .../java/pythagoras/f/AbstractVector.java | 14 +++++++------- src/main/java/pythagoras/f/IVector.java | 19 +++++++++++-------- .../pythagoras/f/NonUniformTransform.java | 2 +- .../java/pythagoras/f/UniformTransform.java | 2 +- src/main/java/pythagoras/f/Vector.java | 13 +++++++------ src/test/java/pythagoras/f/TransformTest.java | 14 +++++++------- 11 files changed, 61 insertions(+), 53 deletions(-) diff --git a/src/main/java/pythagoras/d/AbstractVector.java b/src/main/java/pythagoras/d/AbstractVector.java index 2671081..ef61373 100644 --- a/src/main/java/pythagoras/d/AbstractVector.java +++ b/src/main/java/pythagoras/d/AbstractVector.java @@ -34,7 +34,7 @@ public abstract class AbstractVector implements IVector @Override // from interface IVector public Vector normalize (Vector result) { - return mult(1f / length(), result); + return scale(1f / length(), result); } @Override // from interface IVector @@ -71,22 +71,22 @@ public abstract class AbstractVector implements IVector } @Override // from interface IVector - public Vector mult (double v) { - return mult(v, new Vector()); + public Vector scale (double v) { + return scale(v, new Vector()); } @Override // from interface IVector - public Vector mult (double v, Vector result) { + public Vector scale (double v, Vector result) { return result.set(x()*v, y()*v); } @Override // from interface IVector - public Vector mult (IVector other) { - return mult(other, new Vector()); + public Vector scale (IVector other) { + return scale(other, new Vector()); } @Override // from interface IVector - public Vector mult (IVector other, Vector result) { + public Vector scale (IVector other, Vector result) { return result.set(x()*other.x(), y()*other.y()); } diff --git a/src/main/java/pythagoras/d/IVector.java b/src/main/java/pythagoras/d/IVector.java index 41d05fe..2c8656b 100644 --- a/src/main/java/pythagoras/d/IVector.java +++ b/src/main/java/pythagoras/d/IVector.java @@ -52,21 +52,24 @@ public interface IVector /** Returns the squared distance from this vector to the specified other. */ double distanceSq (IVector other); - /** Multiplies this vector by a scalar. + /** Scales this vector uniformly by the specified magnitude. * @return a new vector containing the result. */ - Vector mult (double v); + Vector scale (double v); - /** Multiplies this vector by a scalar and places the result in the supplied object. + /** Scales this vector uniformly by the specified magnitude, and places the result in the + * supplied object. * @return a reference to the result, for chaining. */ - Vector mult (double v, Vector result); + Vector scale (double v, Vector result); - /** Multiplies this vector by another. + /** Scales this vector's x and y components independently by the x and y components of the + * supplied vector. * @return a new vector containing the result. */ - Vector mult (IVector other); + Vector scale (IVector other); - /** Multiplies this vector by another, storing the result in the object provided. + /** Scales this vector's x and y components independently by the x and y components of the + * supplied vector, and stores the result in the object provided. * @return a reference to the result vector, for chaining. */ - Vector mult (IVector other, Vector result); + Vector scale (IVector other, Vector result); /** Adds a vector to this one. * @return a new vector containing the result. */ diff --git a/src/main/java/pythagoras/d/NonUniformTransform.java b/src/main/java/pythagoras/d/NonUniformTransform.java index ad4c1e8..8777c39 100644 --- a/src/main/java/pythagoras/d/NonUniformTransform.java +++ b/src/main/java/pythagoras/d/NonUniformTransform.java @@ -150,7 +150,7 @@ public class NonUniformTransform extends AbstractTransform @Override // from Transform public Transform invert () { Vector iscale = new Vector(1f / scaleX, 1f / scaleY); - Vector t = new Vector(tx, ty).negateLocal().rotateLocal(-rotation).multLocal(iscale); + Vector t = new Vector(tx, ty).negateLocal().rotateLocal(-rotation).scaleLocal(iscale); return new NonUniformTransform(iscale.x, iscale.y, -rotation, t.x, t.y); } diff --git a/src/main/java/pythagoras/d/UniformTransform.java b/src/main/java/pythagoras/d/UniformTransform.java index e6e5326..193e691 100644 --- a/src/main/java/pythagoras/d/UniformTransform.java +++ b/src/main/java/pythagoras/d/UniformTransform.java @@ -124,7 +124,7 @@ public class UniformTransform extends AbstractTransform @Override // from Transform public Transform invert () { double nscale = 1f / scale, nrotation = -rotation; - Vector t = translation().negateLocal().rotateLocal(nrotation).multLocal(nscale); + Vector t = translation().negateLocal().rotateLocal(nrotation).scaleLocal(nscale); return new UniformTransform(nscale, nrotation, t.x, t.y); } diff --git a/src/main/java/pythagoras/d/Vector.java b/src/main/java/pythagoras/d/Vector.java index a7bafe9..5663cd7 100644 --- a/src/main/java/pythagoras/d/Vector.java +++ b/src/main/java/pythagoras/d/Vector.java @@ -41,16 +41,17 @@ public class Vector extends AbstractVector return normalize(this); } - /** Multiplies this vector in-place by a scalar. + /** Scales this vector in place, uniformly by the specified magnitude. * @return a reference to this vector, for chaining. */ - public Vector multLocal (double v) { - return mult(v, this); + public Vector scaleLocal (double v) { + return scale(v, this); } - /** Multiplies this vector in-place by another. + /** Scales this vector's x and y components, in place, independently by the x and y components + * of the supplied vector. * @return a reference to this vector, for chaining. */ - public Vector multLocal (IVector other) { - return mult(other, this); + public Vector scaleLocal (IVector other) { + return scale(other, this); } /** Adds a vector in-place to this one. diff --git a/src/main/java/pythagoras/f/AbstractVector.java b/src/main/java/pythagoras/f/AbstractVector.java index ad03d3d..cd33060 100644 --- a/src/main/java/pythagoras/f/AbstractVector.java +++ b/src/main/java/pythagoras/f/AbstractVector.java @@ -34,7 +34,7 @@ public abstract class AbstractVector implements IVector @Override // from interface IVector public Vector normalize (Vector result) { - return mult(1f / length(), result); + return scale(1f / length(), result); } @Override // from interface IVector @@ -71,22 +71,22 @@ public abstract class AbstractVector implements IVector } @Override // from interface IVector - public Vector mult (float v) { - return mult(v, new Vector()); + public Vector scale (float v) { + return scale(v, new Vector()); } @Override // from interface IVector - public Vector mult (float v, Vector result) { + public Vector scale (float v, Vector result) { return result.set(x()*v, y()*v); } @Override // from interface IVector - public Vector mult (IVector other) { - return mult(other, new Vector()); + public Vector scale (IVector other) { + return scale(other, new Vector()); } @Override // from interface IVector - public Vector mult (IVector other, Vector result) { + public Vector scale (IVector other, Vector result) { return result.set(x()*other.x(), y()*other.y()); } diff --git a/src/main/java/pythagoras/f/IVector.java b/src/main/java/pythagoras/f/IVector.java index 1959cde..d7fd66c 100644 --- a/src/main/java/pythagoras/f/IVector.java +++ b/src/main/java/pythagoras/f/IVector.java @@ -52,21 +52,24 @@ public interface IVector /** Returns the squared distance from this vector to the specified other. */ float distanceSq (IVector other); - /** Multiplies this vector by a scalar. + /** Scales this vector uniformly by the specified magnitude. * @return a new vector containing the result. */ - Vector mult (float v); + Vector scale (float v); - /** Multiplies this vector by a scalar and places the result in the supplied object. + /** Scales this vector uniformly by the specified magnitude, and places the result in the + * supplied object. * @return a reference to the result, for chaining. */ - Vector mult (float v, Vector result); + Vector scale (float v, Vector result); - /** Multiplies this vector by another. + /** Scales this vector's x and y components independently by the x and y components of the + * supplied vector. * @return a new vector containing the result. */ - Vector mult (IVector other); + Vector scale (IVector other); - /** Multiplies this vector by another, storing the result in the object provided. + /** Scales this vector's x and y components independently by the x and y components of the + * supplied vector, and stores the result in the object provided. * @return a reference to the result vector, for chaining. */ - Vector mult (IVector other, Vector result); + Vector scale (IVector other, Vector result); /** Adds a vector to this one. * @return a new vector containing the result. */ diff --git a/src/main/java/pythagoras/f/NonUniformTransform.java b/src/main/java/pythagoras/f/NonUniformTransform.java index c022507..eb6cf91 100644 --- a/src/main/java/pythagoras/f/NonUniformTransform.java +++ b/src/main/java/pythagoras/f/NonUniformTransform.java @@ -150,7 +150,7 @@ public class NonUniformTransform extends AbstractTransform @Override // from Transform public Transform invert () { Vector iscale = new Vector(1f / scaleX, 1f / scaleY); - Vector t = new Vector(tx, ty).negateLocal().rotateLocal(-rotation).multLocal(iscale); + Vector t = new Vector(tx, ty).negateLocal().rotateLocal(-rotation).scaleLocal(iscale); return new NonUniformTransform(iscale.x, iscale.y, -rotation, t.x, t.y); } diff --git a/src/main/java/pythagoras/f/UniformTransform.java b/src/main/java/pythagoras/f/UniformTransform.java index cd94244..2fec6f9 100644 --- a/src/main/java/pythagoras/f/UniformTransform.java +++ b/src/main/java/pythagoras/f/UniformTransform.java @@ -124,7 +124,7 @@ public class UniformTransform extends AbstractTransform @Override // from Transform public Transform invert () { float nscale = 1f / scale, nrotation = -rotation; - Vector t = translation().negateLocal().rotateLocal(nrotation).multLocal(nscale); + Vector t = translation().negateLocal().rotateLocal(nrotation).scaleLocal(nscale); return new UniformTransform(nscale, nrotation, t.x, t.y); } diff --git a/src/main/java/pythagoras/f/Vector.java b/src/main/java/pythagoras/f/Vector.java index bdd814d..8e07db1 100644 --- a/src/main/java/pythagoras/f/Vector.java +++ b/src/main/java/pythagoras/f/Vector.java @@ -41,16 +41,17 @@ public class Vector extends AbstractVector return normalize(this); } - /** Multiplies this vector in-place by a scalar. + /** Scales this vector in place, uniformly by the specified magnitude. * @return a reference to this vector, for chaining. */ - public Vector multLocal (float v) { - return mult(v, this); + public Vector scaleLocal (float v) { + return scale(v, this); } - /** Multiplies this vector in-place by another. + /** Scales this vector's x and y components, in place, independently by the x and y components + * of the supplied vector. * @return a reference to this vector, for chaining. */ - public Vector multLocal (IVector other) { - return mult(other, this); + public Vector scaleLocal (IVector other) { + return scale(other, this); } /** Adds a vector in-place to this one. diff --git a/src/test/java/pythagoras/f/TransformTest.java b/src/test/java/pythagoras/f/TransformTest.java index 2c49d6e..568c5fd 100644 --- a/src/test/java/pythagoras/f/TransformTest.java +++ b/src/test/java/pythagoras/f/TransformTest.java @@ -142,7 +142,7 @@ public class TransformTest test(tpre, point, expect); } for (Vector vector : VECTORS) { - Vector expect = vector.mult(scale); + Vector expect = vector.scale(scale); test(t, vector, expect); test(tpost, vector, expect); test(tpre, vector, expect); @@ -177,7 +177,7 @@ public class TransformTest test(tpre, point, expect); } for (Vector vector : VECTORS) { - Vector expect = vector.mult(scale); + Vector expect = vector.scale(scale); test(t, vector, expect); test(tpost, vector, expect); test(tpre, vector, expect); @@ -207,7 +207,7 @@ public class TransformTest test(tpre, point, expect); } for (Vector vector : VECTORS) { - Vector expect = vector.rotate(angle).multLocal(scale); + Vector expect = vector.rotate(angle).scaleLocal(scale); test(t, vector, expect); test(tpost, vector, expect); test(tpre, vector, expect); @@ -236,7 +236,7 @@ public class TransformTest test(tpre, point, expect); } for (Vector vector : VECTORS) { - Vector expect = vector.mult(scale).rotateLocal(angle); + Vector expect = vector.scale(scale).rotateLocal(angle); test(tpost, vector, expect); test(tpre, vector, expect); } @@ -255,7 +255,7 @@ public class TransformTest test(t, point, expect); } for (Vector vector : VECTORS) { - Vector expect = vector.mult(scale).rotateLocal(angle); + Vector expect = vector.scale(scale).rotateLocal(angle); test(t, vector, expect); } } @@ -286,7 +286,7 @@ public class TransformTest test(tpre, point, expect); } for (Vector vector : VECTORS) { - Vector expect = vector.mult(scale).rotateLocal(angle); + Vector expect = vector.scale(scale).rotateLocal(angle); test(tpost, vector, expect); test(tpre, vector, expect); } @@ -307,7 +307,7 @@ public class TransformTest test(t, point, expect); } for (Vector vector : VECTORS) { - Vector expect = vector.mult(scale).rotateLocal(angle); + Vector expect = vector.scale(scale).rotateLocal(angle); test(t, vector, expect); } }