-
Notifications
You must be signed in to change notification settings - Fork 0
/
angle_class.py
157 lines (131 loc) · 4.13 KB
/
angle_class.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
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
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 14 09:45:14 2021
@author: schloe
"""
import math as m
class Angle:
# minimum = 0
# maximum = 400
minimum = 0
maximum = 2*m.pi
# minimum = -m.pi
# maximum = +m.pi
def __init__(self, angle=0):
if isinstance(angle, (float,int)):
Angle.intervall = self.maximum - self.minimum
Angle.half_intervall = self.intervall / 2
self.angle = float(self.normalize(angle))
else:
raise TypeError("Type of angle has to be either float or int")
def __str__(self):
return (format(self.angle))
def normalize(self, angle):
if (angle < self.minimum) or (angle >= self.maximum):
angle = ((angle-self.minimum)%self.intervall)+self.minimum
return(angle)
def __neg__(self):
return(-self.angle)
def __add__(self, add):
if isinstance(add, Angle):
angle = self.angle + add.angle
elif isinstance(add, (float, int)):
angle = self.angle + add
else:
return(None)
angle = self.normalize(angle)
return(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 - sub
else:
return(None)
angle = self.normalize(angle)
return(angle)
def __rsub__(self, sub):
if isinstance(sub, (float, int)):
angle = sub - self.angle
else:
return(None)
angle = self.normalize(angle)
return(angle)
def __mul__(self, mul):
if isinstance(mul, (float, int)):
angle = self.angle * mul
else:
return(None)
angle = self.normalize(angle)
return(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)
angle = self.normalize(angle)
return(angle)
def __rtruediv__(self, div):
return(None)
def sin(arg):
return(m.sin(arg.angle/Angle.half_intervall*m.pi))
def cos(arg):
return(m.cos(arg.angle/Angle.half_intervall*m.pi))
def tan(arg):
return(m.tan(arg.angle/Angle.half_intervall*m.pi))
def asin(arg):
angle = m.asin(arg)/m.pi*Angle.half_intervall
angle = Angle.normalize(Angle,angle)
return(angle)
def acos(arg):
angle = m.acos(arg)/m.pi*Angle.half_intervall
angle = Angle.normalize(Angle,angle)
return(angle)
def atan(arg):
angle = m.atan(arg)/m.pi*Angle.half_intervall
angle = Angle.normalize(Angle,angle)
return(angle)
def atan2(arg1,arg2):
angle = m.atan2(arg1,arg2)/m.pi*Angle.half_intervall
angle = Angle.normalize(Angle,angle)
return(angle)
#==============================================================================
# Demonstration data
#==============================================================================
#w1=Angle(500)
#w2=Angle(200)
#w3=Angle(50)
#w4=Angle()
#w4=w4+800
#
#print('w4 =',w4)
#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(w3))
#print('atan2(3,2)=',Angle.atan2(3,2))