-
Notifications
You must be signed in to change notification settings - Fork 2
/
gravitropism.py
50 lines (36 loc) · 1.53 KB
/
gravitropism.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
""" Implement the gravitropism algorithm """
import math
from constants import E
def calculate_results_gravitropism(x1, y1, x2, y2, time_elapsed):
"""
Calculate distance, rate, and angle of movement.
:param x1: initial x coordinate
:type x1: float
:param x2: final x coordinate
:type x2: float
:param y1: initial y coordinate
:type y1: float
:param y2: final y coordinate
:type y2: float
:param time_elapsed: time elapsed between the two points
:type time_elapsed: float
:raises TypeError: All coordinates must be provided and time elapsed must be greater than zero.
:return:A dictionary of
distance - distance moved (mm)
rate - rate of movement (mm/min)
angle - angle of movement relative to positive x-axis (degrees)
:rtype: dict
"""
# check to see if any of the parameters are not valid (null or 0)
if any(parameter is None for parameter in [x1, y1, x2, y2]) or time_elapsed == 0:
raise TypeError(E.CALC_RESULTS_PARAM_INVALID['message'])
# Calculate the distance using the Euclidean distance formula
distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
# Calculate the rate of movement
rate = distance / time_elapsed
# Calculate the angle using the arctan2 (returns arctan in radians of y/x) function and convert to degrees
angle = math.degrees(math.atan2(y2 - y1, x2 - x1))
distance = round(distance, 2)
rate = round(rate, 2)
angle = round(angle, 2)
return {'distance': distance, 'rate': rate, 'angle': angle}