Skip to content

Commit

Permalink
[bugfix] removed direction comparison in getSpeedInDirection in Motio…
Browse files Browse the repository at this point in the history
…nApplier. (#266)
  • Loading branch information
Liam-Rougoor authored Jun 21, 2024
1 parent b725318 commit e4b4817
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ public double getSpeedInDirection(final Direction direction) {
@Override
public double getSpeedInDirection(final double direction) {
var calculatedSpeed = 0D;
if (Double.compare(getDirection(), direction) != 0) {
final var normalizedVector = createVector(1, direction);
final var dotProduct = normalizedVector.dotProduct(motion);

if (dotProduct > 0) {
calculatedSpeed = calculateDenormalizedVector(normalizedVector, motion).magnitude();
}
final var normalizedVector = createVector(1, direction);
final var dotProduct = normalizedVector.dotProduct(motion);

if (dotProduct > 0) {
calculatedSpeed = calculateDenormalizedVector(normalizedVector, motion).magnitude();
}

return calculatedSpeed;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -743,6 +745,20 @@ void nullifySpeedInZeroDirectionDoesNotResultInNaNTest() {

@Nested
class GetSpeedInDirectionTests {
@ParameterizedTest
@EnumSource(Direction.class)
void getSpeedInExactDirectionGivesSpeedTest(Direction direction){
// Arrange
var speed = 3.7;

// Act
sut.setMotion(speed, direction);
var speedInDirection = sut.getSpeedInDirection(direction);

// Assert
assertEquals(3.7, speedInDirection, DELTA);
}

@Test
void getSpeedInOppositeDirectionGives0Test() {
// Arrange
Expand Down

0 comments on commit e4b4817

Please sign in to comment.