Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions src/pycreative/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ def dot(a, b):
bx, by = (b.x, b.y) if isinstance(b, PVector) else (float(b[0]), float(b[1]))
return ax * bx + ay * by

@staticmethod
def dist(a, b):
"""Euclidean distance between two PVectors or 2-length iterables."""
ax, ay = (a.x, a.y) if isinstance(a, PVector) else (float(a[0]), float(a[1]))
bx, by = (b.x, b.y) if isinstance(b, PVector) else (float(b[0]), float(b[1]))
return math.hypot(ax - bx, ay - by)

@staticmethod
def angle_between(a, b):
ax, ay = (a.x, a.y) if isinstance(a, PVector) else (float(a[0]), float(a[1]))
Expand Down
21 changes: 21 additions & 0 deletions tests/test_pvector_dist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def test_pvector_dist_instance_and_classstyle():
from pycreative.vector import PVector
import math

v1 = PVector(10, 20)
v2 = PVector(60, 80)

# expected distance between (10,20) and (60,80)
expected = math.hypot(50, 60)

# instance method
d1 = v1.dist(v2)
assert abs(d1 - expected) < 1e-9

# class-style/unbound call (PVector.dist(v1, v2)) should work too
d2 = PVector.dist(v1, v2)
assert abs(d2 - expected) < 1e-9

# iterable input
d3 = v1.dist((60, 80))
assert abs(d3 - expected) < 1e-9