Taylor Bockman
5 years ago
2 changed files with 42 additions and 0 deletions
@ -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)) |
@ -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…
Reference in new issue