-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtility.py
118 lines (87 loc) · 3.71 KB
/
Utility.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
import math
class Color:
def __init__(self, red, green, blue):
self.r, self.g, self.b = red, green, blue
def __str__(self):
return "(%s,%s,%s)" % (self.r, self.g, self.b)
def __repr__(self):
return "Color" + str(self)
def __add__(self, other):
return Color(self.r + other.r, self.g + other.g, self.b + other.b)
def __sub__(self, other):
return Color(self.r - other.r, self.g - other.g, self.b - other.b)
def __mul__(self, other):
if isinstance(other, Color):
# if type(other) == Color:
return Color(self.r * other.r, self.g * other.g, self.b * other.b)
else:
return Color(self.r * other, self.g * other, self.b * other)
def __rmul__(self, other):
if isinstance(other, Color):
# if type(other) == Color:
return Color(self.r * other.r, self.g * other.g, self.b * other.b)
else:
return Color(self.r * other, self.g * other, self.b * other)
def __div__(self, other):
return Color(self.r / other, self.g / other, self.b / other)
def powc(self, power):
return Color(self.r ** power, self.g ** power, self.b ** power)
class Point:
def __init__(self, x, y, z):
self.x, self.y, self.z = float(x), float(y), float(z)
def __str__(self):
return "(%s,%s,%s)" % (self.x, self.y, self.z)
def __repr__(self):
return "Point" + str(self)
def __add__(self, vector):
# move the point in the vector direction
return Point(self.x + vector.x, self.y + vector.y, self.z + vector.z)
def __sub__(self, other):
return Vector(self.x - other.x, self.y - other.y, self.z - other.z)
def distance(self, other):
dx = self.x - other.x
dy = self.y - other.y
dz = self.z - other.z
return math.sqrt(dx * dx + dy * dy + dz * dz)
# def scalar(self, scalar):
# return Point(self.x * scalar, self.y * scalar, self.z * scalar)
class Vector(object):
def __init__(self, x, y, z):
self.x, self.y, self.z = float(x), float(y), float(z)
def __str__(self):
return "(%s,%s,%s)" % (self.x, self.y, self.z)
def __repr__(self):
return "Vector" + str(self)
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y, self.z + other.z)
def __sub__(self, other):
return Vector(self.x - other.x, self.y - other.y, self.z - other.z)
def __mul__(self, other):
# if isinstance(other, Vector):
if type(other) == Vector:
# dot multiple
return self.x * other.x + self.y * other.y + self.z * other.z
else:
# scale
return Vector(self.x * other, self.y * other, self.z * other)
def __rmul__(self, other):
# if isinstance(other, Vector):
if type(other) == Vector:
# dot multiple
return self.x * other.x + self.y * other.y + self.z * other.z
else:
# scale
return Vector(self.x * other, self.y * other, self.z * other)
def __div__(self, other):
return Vector(self.x / other, self.y / other, self.z / other)
def cross(self, other):
return Vector(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 length(self):
return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
def normalize(self):
# return self / self.length()
return self / math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
class Ray:
def __init__(self, origin, direction):
self.origin = origin
self.direction = direction