Skip to content

Commit

Permalink
Merge branch 'master' into vector-like
Browse files Browse the repository at this point in the history
  • Loading branch information
AstraLuma authored Oct 13, 2018
2 parents a1289d3 + 883005c commit b21a98e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.cache/
/*.egg-info/
__pycache__/
.hypothesis/
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ matrix:
allow_failures:
- python: "nightly"

cache:
directories:
- $HOME/.cache/pip
- .hypothesis

before_install:
- if [[ $MINICONDA_OS ]]; then
URL="https://repo.continuum.io/miniconda/Miniconda3-latest-${MINICONDA_OS}-x86_64.sh";
Expand Down
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest~=3.8
hypothesis
22 changes: 22 additions & 0 deletions ppb_vector/vector2.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ def angle(self, other: VectorLike) -> Real:
other = _mkvector(other, castto=Vector2)
return degrees(acos(self.normalize() * other.normalize()))

def isclose(self, other: 'Vector2', *, rel_tol: float=1e-06, abs_tol: float=1e-3):
"""
Determine whether two vectors are close in value.
rel_tol
maximum difference for being considered "close", relative to the
magnitude of the input values
abs_tol
maximum difference for being considered "close", regardless of the
magnitude of the input values
Return True if self is close in value to other, and False otherwise.
For the values to be considered close, the difference between them
must be smaller than at least one of the tolerances.
"""
diff = (self - other).length
return (
diff < rel_tol * max(self.length, other.length) or
diff < abs_tol
)

def rotate(self, degrees: Real) -> 'Vector2':
r = radians(degrees)
r_cos = cos(r)
Expand Down
18 changes: 18 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from ppb_vector import Vector2
import hypothesis.strategies as st

vectors = lambda: st.builds(
Vector2,
st.floats(min_value=-1e300, max_value=1e300),
st.floats(min_value=-1e300, max_value=1e300)
)

@st.composite
def units(draw, elements=st.floats(min_value=0, max_value=360)):
angle = draw(elements)
return Vector2(1, 0).rotate(angle)


def angle_isclose(x, y, epsilon = 6.5e-5):
d = (x - y) % 360
return (d < epsilon) or (d > 360 - epsilon)

0 comments on commit b21a98e

Please sign in to comment.