-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeometry.py
More file actions
107 lines (79 loc) · 2.27 KB
/
geometry.py
File metadata and controls
107 lines (79 loc) · 2.27 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from math import sqrt
from math import pi
from math import tan
class vec4(object):
__slots__ = ('data', )
def __init__(self, x, y, z, w):
self.data = (x, y, z, w)
def __getitem__(self, index):
return self.data[index]
class vec3(object):
__slots__ = ('data', )
def __init__(self, x, y, z):
self.data = (x, y, z)
def __getitem__(self, index):
return self.data[index]
def get_x(self):
return self.data[0]
def get_y(self):
return self.data[1]
def get_z(self):
return self.data[2]
def set_x(self, val):
self.data = (val, self.data[1], self.data[2])
def set_y(self, val):
self.data = (self.data[0], val, self.data[2])
def set_z(self, val):
self.data = (self.data[0], self.data[1], val)
x = property(get_x, set_x)
y = property(get_y, set_y)
z = property(get_z, set_z)
def norm(self):
return sqrt(self.data[0]*self.data[0]
+ self.data[1]*self.data[1]
+ self.data[2]*self.data[2]
)
def normalize(self):
len = self.norm()
self.data = (
self.data[0] / len,
self.data[1] / len,
self.data[2] / len
)
return self
def __mul__(self, other):
if type(other) is float:
return vec3(
self[0]*other,
self[1]*other,
self[2]*other
)
return self[0]*other[0] + self[1]*other[1] + self[2]*other[2]
def __add__(self, other):
return vec3(
self[0] + other[0],
self[1] + other[1],
self[2] + other[2]
)
def __sub__(self, other):
return vec3(
self[0] - other[0],
self[1] - other[1],
self[2] - other[2]
)
def __neg__(self):
return vec3(
-self[0],
-self[1],
-self[2]
)
def cross(self, other):
return vec3(
self[1]*other[2] - self[2]*other[1],
self[2]*other[0] - self[0]*other[2],
self[0]*other[1] - self[1]*other[0]
)
def __str__(self):
return '{} {} {}'.format(*self.data)