-
Notifications
You must be signed in to change notification settings - Fork 8
/
10_wektor.py
49 lines (32 loc) · 1.16 KB
/
10_wektor.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
import math
class Wektor2D:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def modul(self):
return math.sqrt(self.x ** 2 + self.y ** 2)
def __add__(self, other):
return Wektor2D(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Wektor2D(self.x - other.x, self.y - other.y)
def __str__(self):
return f"Wektor2D({self.x}, {self.y})"
def __repr__(self):
return self.__str__()
def iloczyn_skalarny(wektor1, wektor2):
return wektor1.x * wektor2.x + wektor1.y * wektor2.y
def kat(wektor1, wektor2):
return math.acos(
iloczyn_skalarny(wektor1, wektor2) / (wektor1.modul() * wektor2.modul())
)
if __name__ == "__main__":
w1 = Wektor2D(1, 2)
w2 = Wektor2D(3, 4)
print(w1)
print(w2)
print(f"Modul wektora1: {w1.modul()}")
print(f"Modul wektora2: {w2.modul()}")
print(f"Wektor1 + Wektor2: {w1 + w2}")
print(f"Wektor1 - Wektor2: {w1 - w2}")
print(f"Iloczyn skalarny wektor1 i wektor2: {iloczyn_skalarny(w1, w2)}")
print(f"Kat pomiedzy wektor1 i wektor2: {kat(w1, w2)}")