-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataclasses.py
67 lines (48 loc) · 1.84 KB
/
dataclasses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Dataclasses for stratigraphy module."""
from __future__ import annotations
import logging
from dataclasses import dataclass
import numpy as np
logger = logging.getLogger(__name__)
@dataclass
class Point:
"""Class to represent a point in 2D space."""
x: float
y: float
@property
def tuple(self) -> (float, float):
return self.x, self.y
def distance_to(self, point: Point) -> float:
return np.sqrt((self.x - point.x) ** 2 + (self.y - point.y) ** 2)
@dataclass
class Line:
"""Class to represent a line in 2D space."""
start: Point
end: Point
def __post_init__(self):
if self.start.x > self.end.x:
end = self.start
self.start = self.end
self.end = end
self.length = self.start.distance_to(self.end)
def distance_to(self, point: Point) -> float:
"""Calculate the distance of a point to the line.
Args:
point (Point): The point to calculate the distance to.
Returns:
float: The distance of the point to the line.
"""
# Calculate the distance of the point to the line:
# Taken from https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Line_defined_by_two_points
return np.abs(
(self.end.x - self.start.x) * (self.start.y - point.y)
- (self.start.x - point.x) * (self.end.y - self.start.y)
) / np.sqrt((self.end.x - self.start.x) ** 2 + (self.end.y - self.start.y) ** 2)
@property
def slope(self) -> float:
"""Calculate the slope of the line."""
return (self.end.y - self.start.y) / (self.end.x - self.start.x) if self.end.x - self.start.x != 0 else np.inf
@property
def intercept(self) -> float:
"""Calculate the y-intercept of the line."""
return self.start.y - self.slope * self.start.x