-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.py
137 lines (108 loc) · 3.64 KB
/
vector.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
import math
from matrix import Matrix
class Vec2:
def __init__(self, x, y=None):
if isinstance(x, (list, tuple)) and len(x) == 2 and y is None:
self.x, self.y = x[0], x[1]
else:
self.x, self.y = x, y
def to_matrix(self):
m = Matrix(2, 1).m
m[0, 0] = self.x
m[1, 0] = self.y
return m
@staticmethod
def zero():
return Vec3(0, 0)
def __add__(self, other):
return Vec2(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vec2(self.x - other.x, self.y - other.y)
def __mul__(self, other):
if isinstance(other, (int, float)): # 向量和标量的乘法
return Vec2(self.x * other, self.y * other)
elif isinstance(other, Vec2): # 两个向量的点积
return self.x * other.x + self.y * other.y
def __rmul__(self, scalar):
return self.__mul__(scalar)
def norm(self):
return math.sqrt(self.x * self.x + self.y * self.y)
def normalize(self, l=1):
norm = self.norm()
self.x *= l / norm
self.y *= l / norm
return self
def __getitem__(self, index):
if index == 0:
return self.x
elif index == 1:
return self.y
else:
raise IndexError("Index out of range")
def __setitem__(self, index, value):
if index == 0:
self.x = value
elif index == 1:
self.y = value
else:
raise IndexError("Index out of range")
def __str__(self):
return f'({self.x}, {self.y})'
class Vec3:
def __init__(self, x, y=None, z=None):
if isinstance(x, (list, tuple)) and len(x) == 3 and y is None and z is None:
self.x, self.y, self.z = x[0], x[1], x[2]
else:
self.x, self.y, self.z = x, y, z
def to_matrix(self):
m = Matrix(3, 1).m
m[0, 0] = self.x
m[1, 0] = self.y
m[2, 0] = self.z
return m
@staticmethod
def zero():
return Vec3(0, 0, 0)
def __add__(self, other):
return Vec3(self.x + other.x, self.y + other.y, self.z + other.z)
def __sub__(self, other):
return Vec3(self.x - other.x, self.y - other.y, self.z - other.z)
def __mul__(self, other):
if isinstance(other, (int, float)): # 向量和标量的乘法
return Vec3(self.x * other, self.y * other, self.z * other)
elif isinstance(other, Vec3): # 两个向量的点积
return self.x * other.x + self.y * other.y + self.z * other.z
def __rmul__(self, scalar):
return self.__mul__(scalar)
def cross(self, other):
return Vec3(self.y * other.z - self.z * other.y,
self.z * other.x - self.x * other.z,
self.x * other.y - self.y * other.x)
def norm(self):
return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
def normalize(self, l=1):
norm = self.norm()
self.x *= l / norm
self.y *= l / norm
self.z *= l / norm
return self
def __getitem__(self, index):
if index == 0:
return self.x
elif index == 1:
return self.y
elif index == 2:
return self.z
else:
raise IndexError("Index out of range")
def __setitem__(self, index, value):
if index == 0:
self.x = value
elif index == 1:
self.y = value
elif index == 2:
self.z = value
else:
raise IndexError("Index out of range")
def __str__(self):
return f'({self.x}, {self.y}, {self.z})'