Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rounding bug in Vector2.rotation #67

Merged
merged 5 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions ppb_vector/vector2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import typing
import collections
from math import acos, cos, degrees, hypot, radians, sin
from math import acos, cos, degrees, hypot, isclose, radians, sin
from numbers import Real
from collections.abc import Sequence

Expand Down Expand Up @@ -163,8 +163,10 @@ def rotate(self, degrees: Real) -> 'Vector2':
r = radians(degrees)
r_cos = cos(r)
r_sin = sin(r)
x = round(self.x * r_cos - self.y * r_sin, 5)
y = round(self.x * r_sin + self.y * r_cos, 5)
assert isclose(r_cos * r_cos + r_sin * r_sin, 1)
nbraud marked this conversation as resolved.
Show resolved Hide resolved

x = self.x * r_cos - self.y * r_sin
y = self.x * r_sin + self.y * r_cos
return Vector2(x, y)

def normalize(self) -> 'Vector2':
Expand Down
2 changes: 1 addition & 1 deletion tests/test_vector2_reflect.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

@pytest.mark.parametrize("initial_vector, surface_normal, expected_vector", reflect_data)
def test_reflect(initial_vector, surface_normal, expected_vector):
assert initial_vector.reflect(surface_normal) == expected_vector
assert initial_vector.reflect(surface_normal).isclose(expected_vector)
6 changes: 4 additions & 2 deletions tests/test_vector2_rotate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ppb_vector import Vector2
from utils import angle_isclose
import pytest
import math

Expand All @@ -17,8 +18,9 @@ def isclose(x, y, epsilon = 6.5e-5):

@pytest.mark.parametrize('input, degrees, expected', data)
def test_multiple_rotations(input, degrees, expected):
assert input.rotate(degrees) == expected
assert isclose(input.angle(expected), degrees)
assert input.rotate(degrees).isclose(expected)
assert angle_isclose(input.angle(expected), degrees)


def test_for_exception():
with pytest.raises(TypeError):
Expand Down