package uk.co.flyingsquirrels.utils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static uk.co.flyingsquirrels.test.Matchers.closeTo;
import org.jbox2d.common.Vec2;
import org.junit.Test;
public class MathUtilsTest {
private static final Vec2 UNIT_Y = new Vec2(0, 1);
@Test
public void clampInRange() {
assertThat(MathUtils.clamp(50., 40., 60.), equalTo(50.));
}
@Test
public void clampBelow() {
assertThat(MathUtils.clamp(30., 40., 60.), equalTo(40.));
}
@Test
public void clampAbove() {
assertThat(MathUtils.clamp(70., 40., 60.), equalTo(60.));
}
@Test
public void barrelClampInRange() {
assertThat(MathUtils.barrelClamp(50, 180), equalTo(50.));
}
@Test
public void barrelClampBelow() {
assertThat(MathUtils.barrelClamp(-200,180), equalTo(160.));
}
@Test
public void barrelClampWellBelow() {
assertThat(MathUtils.barrelClamp(-350, 180), equalTo(10.));
}
@Test
public void barrelClampAbove() {
assertThat(MathUtils.barrelClamp(210, 180), equalTo(-150.));
}
@Test
public void barrelClampWellAbove() {
assertThat(MathUtils.barrelClamp(740, 180), equalTo(20.));
}
@Test
public void clampAngleInRange() {
assertThat(MathUtils.clampAngle(2), closeTo(2f));
}
@Test
public void clampAngleBelow() {
assertThat(MathUtils.clampAngle(-4), closeTo(2.283f));
}
@Test
public void clampAngleWellBelow() {
assertThat(MathUtils.clampAngle(-8), closeTo(-1.717f));
}
@Test
public void clampAngleAbove() {
assertThat(MathUtils.clampAngle(5), closeTo(-1.283f));
}
@Test
public void clampAngleWellAbove() {
assertThat(MathUtils.clampAngle(10), closeTo(-2.566f));
}
@Test
public void toRadians() {
assertThat(MathUtils.toRadians(180.), equalTo(Math.PI));
}
@Test
public void toDegrees() {
assertThat(MathUtils.toDegrees(Math.PI), equalTo(180.));
}
@Test
public void rotateBy0() {
checkRotation(UNIT_Y, 0, new Vec2(0, 1));
}
@Test
public void rotateBy30() {
checkRotation(UNIT_Y, 30, new Vec2(-0.5f, 0.866f));
}
@Test
public void rotateBy45() {
checkRotation(UNIT_Y, 45, new Vec2(-0.707f, 0.707f));
}
@Test
public void rotateBy90() {
checkRotation(UNIT_Y, 90, new Vec2(-1f, 0f));
}
@Test
public void rotateBy135() {
checkRotation(UNIT_Y, 135, new Vec2(-0.707f, -0.707f));
}
@Test
public void rotateBy180() {
checkRotation(UNIT_Y, 180, new Vec2(0, -1f));
}
@Test
public void rotateByMinus30() {
checkRotation(UNIT_Y, -30, new Vec2(0.5f, 0.866f));
}
@Test
public void rotateByMinus45() {
checkRotation(UNIT_Y, -45, new Vec2(0.707f, 0.707f));
}
@Test
public void rotateByMinus90() {
checkRotation(UNIT_Y, -90, new Vec2(1f, 0f));
}
@Test
public void rotateByMinus135() {
checkRotation(UNIT_Y, -135, new Vec2(0.707f, -0.707f));
}
@Test
public void rotateByMinus180() {
checkRotation(UNIT_Y, -180, new Vec2(0f, -1f));
}
private void checkRotation(Vec2 input, double angleInDegrees, Vec2 expected) {
Vec2 rotated = MathUtils.rotate(input, MathUtils
.toRadians(angleInDegrees));
assertThat(rotated.x, closeTo(expected.x));
assertThat(rotated.y, closeTo(expected.y));
}
}