-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformations.py
More file actions
131 lines (103 loc) · 4.2 KB
/
transformations.py
File metadata and controls
131 lines (103 loc) · 4.2 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from math import sin, cos
class Transformations:
def __init__(self):
"""Inititalizer for the class
! Note: Under no circumstance should the origin and origin rotation be accessed or modified directly,
use the property getters and setters for that purpose
"""
self.origin_ = [0, 0]
self.origin_rot = 0
@property
def originx(self):
""" The getter for originx property of the object
:return: The x coordinate of the new origin
"""
return self.origin_[0]
@originx.setter
def originx(self, x):
""" The setter for the orginx property of the object
:param x: The new x coordinate for the origin
:return: None
"""
if type(x) in (int, float):
self.origin_[0] = x
else:
raise TypeError("Expected an int or float but received {} instead".format(type(x)))
@property
def originy(self):
"""The getter for originy property of the object
:return: The y coordinate of the origin
"""
return self.origin_[1]
@originy.setter
def originy(self, y):
"""The setter for the orginy property of the object
:param y: The new y coordinate for the origin
:return: None
"""
if type(y) in (int, float):
self.origin_[1] = y
else:
raise TypeError("Expected an int or float but received {} instead".format(type(y)))
@property
def origin(self):
"""The getter for the origin of the object
:return: The origin of the shifted coordinate system with respect to the old system
"""
return [self.originx, self.originy]
@origin.setter
def origin(self, origin):
"""The setter for the origin of the object
:param origin: The new origin for the shifted coordinate system with respect to the original system
:return: None
"""
try:
val1, val2 = origin
except ValueError:
raise ValueError("Expected an iterable with two items")
else:
self.originx, self.originy = origin
@property
def axes_rotation(self):
"""The getter for the rotation of the axes of new coordinate system
:return: The angle by which the axes have been shifted anticlockwise(in radian)
"""
return self.origin_rot
@axes_rotation.setter
def axes_rotation(self, angle):
"""The setter for the rotation of the axes of the new coordinate system
:param angle: The angle(radian) by which the axes have to be rotated anticlockwise
:return: None
"""
if type(angle) in (int, float):
self.origin_rot = angle
else:
raise TypeError("Expected int or float but received {} instead".format(type(angle)))
def transform(self, pos):
"""Transforms the given coordinate to the true origin of the system
! Note: Should be used only after the origin and the axes rotation of the object has been set, otherwise
it will have no effect on the coordinates
:param pos: The coordinates of the the point in the new coordinate system which have to be shifted to the true
origin
:return: The coordinate in a tuple shifted to the true origin
"""
try:
val1, val2 = pos
except ValueError:
raise ValueError("Expected an iterable with two items")
else:
x = pos[0] * cos(self.axes_rotation) - pos[1] * sin(self.axes_rotation)
y = pos[0] * sin(self.axes_rotation) + pos[1] * cos(self.axes_rotation)
return x + self.originx, y + self.originy
def set_origin(self, pos):
"""Function to set the origin of the new coordinate system relative to the true origin of the system
:param pos: The new origin of the system relative to the true origin
:return: None
"""
self.origin = pos
def set_rotation(self, angle):
"""Function to set the axes rotation(radian) in the anticlockwise direction
:param angle: The angle by which the axes are rotated(radian) in anticlockwise direction
:return: None
"""
self.axes_rotation = angle