Browse Source

Add euclidean distance and tests

tb-init-ui-render
Taylor Bockman 5 years ago
parent
commit
6697efd627
  1. 23
      clusterview/math.py
  2. 19
      tests/test_math.py

23
clusterview/math.py

@ -0,0 +1,23 @@
import math
from .points import Point
class Math:
"""
Defines useful static helper methods for mathematical functions in
Computational Geometry.
"""
@classmethod
def euclidean_distance(cls, p1, p2):
"""
Given two points of type Point, returns the euclidean distance between
those two points.
"""
if not isinstance(p1, Point) or not isinstance(p2, Point):
raise ValueError("Euclidean distance can only be calculated " +
"on points")
return math.sqrt(math.pow(p2.x - p1.x, 2) + math.pow(p2.y - p1.y, 2))

19
tests/test_math.py

@ -0,0 +1,19 @@
import pytest
from clusterview.colors import Color
from clusterview.math import Math
from clusterview.points import Point
def test_euclidean_distance():
p1 = Point(150, 100, Color.BLACK, 8, 800, 600)
p2 = Point(300, 200, Color.BLACK, 8, 800, 600)
d = Math.euclidean_distance(p1, p2)
assert round(d, 2) == 180.28
def test_euclidean_distance_bad_points():
with pytest.raises(ValueError):
Math.euclidean_distance(1, 1)
Loading…
Cancel
Save