Repository navigation
Expand file tree
/
Copy pathangle.py
More file actions
333 lines (287 loc) · 11.2 KB
/
Copy pathangle.py
File metadata and controls
333 lines (287 loc) · 11.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 14 09:45:14 2021
@author: schloe
"""
import math as m
import numpy
class Angle():
T_RAD=0
T_GON=1
T_DEG=2
T_SELF_DEFINED=3
minimum = 0
maximum = m.tau
interval = m.tau ## maximum - minimum
half_interval = m.pi ## intervall / 2
__INTERNAL_MINIMUM = -m.pi
__INTERNAL_MAXIMUM = +m.pi
__INTERNAL_INTERVAL = m.tau ## __int_maximum - __int_minimum
__INTERNAL_HALF_INTERVAL = m.pi ## __int_intervall / 2
angle=0
a_type=T_RAD
a_symmetric=False
def __init__(self, angle=0, type=T_RAD, symmetric=False, minimum=0, maximum=m.tau):
if not isinstance(angle, (float,numpy.longdouble,int)):
raise TypeError("Type of angle has to be float or int")
if not isinstance(symmetric, (bool)):
raise TypeError("Type of symmetric has to be Boolean")
if not (isinstance(minimum, (float, int)) and isinstance(maximum, (float,int))):
raise TypeError("Type of minimum and maximum have to be float or int")
if not (type in [self.T_RAD, self.T_GON, self.T_DEG, self.T_SELF_DEFINED]):
raise TypeError("Type of type has to be Angle.t_rad, .t_gon, .t_deg or .t_self")
if type==self.T_SELF_DEFINED:
self.minimum = minimum
self.maximum = maximum
else:
self.minimum = 0
if type==self.T_RAD:
self.maximum = m.tau
elif type==self.T_GON:
self.maximum = 400
elif type==self.T_DEG:
self.maximum = 360
else:
raise TypeError("How did you get here? This should never happen.")
self.interval = self.maximum - self.minimum
self.half_interval = self.interval / 2
if ((symmetric==True) and (type!=self.T_SELF_DEFINED)):
self.minimum -= self.half_interval
self.maximum -= self.half_interval
self.angle = self.__ext2int(self.__normalize(angle))
self.a_type = type
self.a_symmetric = symmetric
def __str__(self):
return (format(self.__normalize(self.__int2ext(self.angle))))
def __internal_normalize(self, angle):
if (angle < self.__INTERNAL_MINIMUM) or (angle >= self.__INTERNAL_MAXIMUM):
angle = ((angle-self.__INTERNAL_MINIMUM)%(self.__INTERNAL_INTERVAL))+self.__INTERNAL_MINIMUM
return(angle)
def __normalize(self, angle):
if (angle < self.minimum) or (angle >= self.maximum):
angle = ((angle-self.minimum)%(self.interval))+self.minimum
return(angle)
def __ext2int(self,value):
return(value/self.half_interval*self.__INTERNAL_HALF_INTERVAL)
def __int2ext(self,value):
return(self.__normalize(value*self.half_interval/self.__INTERNAL_HALF_INTERVAL))
def get_intervall(self):
return(self.intervall)
def get_half_intervall(self):
return(self.half_intervall)
def __neg__(self):
return(-self.__int2ext(self.angle))
def __invert__(self):
return(self.__int2ext(self.angle))
def __add__(self, add):
if isinstance(add, Angle):
angle = self.angle + add.angle
elif isinstance(add, (float, int)):
angle = self.angle + self.__ext2int(add)
else:
return(None)
return(self.__int2ext(angle))
def __radd__(self,add):
return(self.__add__(add))
def __sub__(self, sub):
if isinstance(sub, Angle):
angle = self.angle - sub.angle
elif isinstance(sub, (float, int)):
angle = self.angle - self.__ext2int(sub)
else:
return(None)
return(self.__int2ext(angle))
def __rsub__(self, sub):
if isinstance(sub, (float, int)):
angle = self.__ext2int(sub) - self.angle
else:
return(None)
return(self.__int2ext(angle))
def __mul__(self, mul):
if isinstance(mul, (float, int)):
angle = self.angle * mul
else:
return(None)
return(self.__int2ext(angle))
def __rmul__(self, mul):
return(self.__mul__(mul))
def __truediv__(self, div):
if isinstance(div, (float, int)):
angle = self.angle / div
else:
return(None)
return(self.__int2ext(angle))
def __rtruediv__(self, div):
return(None)
def comp_error(self):
raise TypeError("angles can only be compared to angles")
return(None)
def __lt__(self, other):
if isinstance(other, Angle):
if other.angle == self.angle:
return(False)
if (self.__internal_normalize(self.angle - other.angle)) < 0:
return(True)
else:
return(False)
else:
self.comp_error()
def __le__(self, other):
if isinstance(other, Angle):
if other.angle == self.angle:
return(True)
if (self.__internal_normalize(self.angle - other.angle)) < 0:
return(True)
else:
return(False)
else:
self.comp_error()
def __gt__(self, other):
if isinstance(other, Angle):
if other.angle == self.angle:
return(False)
if (self.__internal_normalize(self.angle - other.angle)) > 0:
return(True)
else:
return(False)
else:
self.comp_error()
def __ge__(self, other):
if isinstance(other, Angle):
if other.angle == self.angle:
return(True)
if (self.__internal_normalize(self.angle - other.angle)) > 0:
return(True)
else:
return(False)
else:
self.comp_error()
def __eq__(self, other):
if isinstance(other, Angle):
if self.__internal_normalize(other.angle - self.angle) == 0:
return(True)
else:
return(False)
else:
self.comp_error()
def __ne__(self, other):
if isinstance(other, Angle):
if other.angle != self.angle:
return(True)
else:
return(False)
else:
self.comp_error()
@staticmethod
def is_similar(arg1, arg2, delta):
if abs(arg1-arg2)<abs(delta):
return(True)
else:
return(False)
# all trigonometric functions are declared as staitic methods, so they
# can be called as Angle.sin(x). x can be either of Angle-Type or any type
# the math library provides (float, int). It's not possible to call these
# methods as x.sin(), when x is of Angle type.
@staticmethod
def sin(x):
if isinstance(x, Angle):
return(m.sin(x.__ext2int(x)))
else:
return(m.sin(x))
@staticmethod
def cos(x):
if isinstance(x, Angle):
return(m.cos(x.__ext2int(x)))
else:
return(m.cos(x))
@staticmethod
def tan(x):
if isinstance(x, Angle):
return(m.tan(x.__ext2int(x)))
else:
return(m.tan(x))
def asin(self, arg, type=T_RAD, symmetric=False, minimum=0, maximum=m.tau):
if not isinstance(arg, (float, int)):
raise TypeError("Type of arg has to be float or int")
self.__init__(0,type,symmetric,minimum,maximum)
self.angle = m.asin(arg)
return(self.__int2ext(self.angle))
def acos(self, arg, type=T_RAD, symmetric=False, minimum=0, maximum=m.tau):
if not isinstance(arg, (float, int)):
raise TypeError("Type of arg has to be float or int")
self.__init__(0,type,symmetric,minimum,maximum)
self.angle = m.acos(arg)
return(self.__int2ext(self.angle))
def atan(self, arg, type=T_RAD, symmetric=False, minimum=0, maximum=m.tau):
if not isinstance(arg, (float, int)):
raise TypeError("Type of arg has to be float or int")
self.__init__(0,type,symmetric,minimum,maximum)
self.angle = m.atan(arg)
return(self.__int2ext(self.angle))
def atan2(self, arg1, arg2, type=T_RAD, symmetric=False, minimum=0, maximum=m.tau):
if not (isinstance(arg1, (float, int)) and isinstance(arg2, (float, int))):
raise TypeError("Type of arg1 and arg2 has to be float or int")
self.__init__(0,type,symmetric,minimum,maximum)
self.angle = m.atan2(arg1,arg2)
return(self.__int2ext(self.angle))
"""
w1=Angle(500,Angle.T_GON)
w2=Angle(200,Angle.T_GON)
w3=Angle(50,Angle.T_GON)
w4=Angle(0,Angle.T_DEG)
w4=w4+720
print('w4 =',w4)
print('w1 =',w1)
print('-w1 =',-w1)
print('~w1 =',~w1)
print('w2 =',w2)
print('w1+w2 =',w1+w2)
print('w1+30 =',w1+30)
print('w1+30.0 =',w1+30.0)
print('w1+\'30\' =',w1+'30')
print('30+w1 =',30+w1)
print('30.0+w1 =',30.0+w1)
print('\'30\'+w1 =','30'+w1)
print('w2-w1 =',w2-w1)
print('w1-30 =',w1-30)
print('w1-30.0 =',w1-30.0)
print('w1-\'30\' =',w1-'30')
print('30-w1 =',30-w1)
print('30.0-w1 =',30.0-w1)
print('\'30\'-w1 =','30'-w1)
print('w1*w2 =',w1*w2)
print('w1*3 =',w1*3)
print('w1*3.0 =',w1*3.0)
print('3*w1 =',3*w1)
print('-3.0*w1 =',-3.0*w1)
print('w1/3 =',w1/3)
print('3/w1 =',3/w1)
print('w1/0 = division by zero error')
print('sin(w1) =',Angle.sin(w1))
print('atan2(-3,2)=',w3.atan2(-3,2,Angle.T_GON))
print('20<19 =',Angle(20,Angle.T_GON)<Angle(19,Angle.T_GON))
print('19<20 =',Angle(19,Angle.T_GON)<Angle(20,Angle.T_GON))
print('19<19 =',Angle(19,Angle.T_GON)<Angle(19,Angle.T_GON))
print('19<=19 =',Angle(19,Angle.T_GON)<=Angle(19,Angle.T_GON))
print('50<251 =',Angle(50,Angle.T_GON)<Angle(251,Angle.T_GON))
print('50<250 =',Angle(50,Angle.T_GON)<Angle(250,Angle.T_GON),' ... undefined!')
print('20>19 =',Angle(20,Angle.T_GON)>Angle(19,Angle.T_GON))
print('19>20 =',Angle(19,Angle.T_GON)>Angle(20,Angle.T_GON))
print('19>19 =',Angle(19,Angle.T_GON)>Angle(19,Angle.T_GON))
print('19>=19 =',Angle(19,Angle.T_GON)>=Angle(19,Angle.T_GON))
print('50>251 =',Angle(50,Angle.T_GON)>Angle(251,Angle.T_GON))
print('50>250 =',Angle(50,Angle.T_GON)>Angle(250,Angle.T_GON),' ... undefined!')
print('50==450 =',Angle(50,Angle.T_GON)==Angle(450,Angle.T_GON))
print('50!=450 =',Angle(50,Angle.T_GON)!=Angle(450,Angle.T_GON))
print('50!=451 =',Angle(50,Angle.T_GON)!=Angle(451,Angle.T_GON))
print('w1!=w2 =',w1!=w2)
w3=Angle(50,Angle.T_GON)
print('w2==4*w3=',w2==Angle(w3*4,Angle.T_GON))
print('0.001 is_similar -0.001 by 0.1',Angle.is_similar(-0.001,+0.001,0.1))
print('w1 is_similar w2 by 0.1',Angle.is_similar(w1,w2,0.1))
w1=Angle(150,Angle.T_GON)
w2=Angle(135,Angle.T_DEG)
print(w1)
print(w2)
print('w1 is_similar w2 by 0.1',Angle.is_similar(w1,w2,0.1))
"""