Skip to content

Commit 0ffdb35

Browse files
authored
Merge pull request #126 from Flashky/feature/57-add-multiply-by-scalar-to-vector2
feat: add Vector2 multiply by scalar method #57
2 parents 9742ff5 + 7fcf2be commit 0ffdb35

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/main/java/com/adventofcode/flashk/common/Vector2.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ public static Vector2 transform(Vector2 start, Vector2 end) {
6464

6565
return new Vector2(x,y);
6666
}
67-
67+
68+
/// Multiplies the given vector by a scalar, returning a new Vector2.
69+
/// @param vector the vector to multiply
70+
/// @param scalar the scalar value to multiply the vector by
71+
/// @return a new Vector2 with the result of the scalar product.
72+
public static Vector2 multiply(Vector2 vector, int scalar) {
73+
return new Vector2(vector.x * scalar, vector.y * scalar);
74+
}
75+
6876
/**
6977
* Substracts the right operand vector to the left operand vector, applying absolute value to the result.
7078
*

src/test/java/com/adventofcode/flashk/common/Vector2Test.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,4 +753,20 @@ void testNormalizedOrigin() {
753753
assertThrows(IllegalStateException.class, vector::normalized);
754754

755755
}
756+
757+
@Test
758+
void multiply() {
759+
760+
Vector2 vector = new Vector2(2, 3);
761+
Vector2 result = Vector2.multiply(vector, 5);
762+
763+
// The result is a new vector with the product of the vector by the scalar
764+
assertNotSame(vector, result);
765+
assertEquals(10, result.getX());
766+
assertEquals(15, result.getY());
767+
768+
// Original vector is not modified
769+
assertEquals(2, vector.getX());
770+
assertEquals(3, vector.getY());
771+
}
756772
}

0 commit comments

Comments
 (0)