-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.py
More file actions
41 lines (38 loc) · 1.47 KB
/
Copy pathTriangle.py
File metadata and controls
41 lines (38 loc) · 1.47 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
from Vector3D import *
import math
class Triangle:
def __init__(self,v1,v2,v3,color=Vector(255,0,0)): #3d Vectors represent colors!
self.v1=v1
self.v2=v2
self.v3=v3
self._normal=None #STARTING AT LEFT
self._rnormal=None #normal from right
self._centre=(v1+v2+v3)/3
self.color=color
@property
def normal(self):
if (self._normal is None):
self._normal = (self.v3-self.v1)*(self.v2-self.v1)
return self._normal
@property
def rnormal(self):
if (self._rnormal is None):
if (self._normal is None):
self._rnormal = (self.v2-self.v1)*(self.v3-self.v1)
else:
self._rnormal = -self.normal
return self._rnormal
@property
def centre(self):
if (self._centre is None):
self._centre = (self.v1+self.v2+self.v3)/3
return self._centre
def __repr__(self):
return "Triangle({},{},{},{})".format(self.v1,self.v2,self.v3,self.color)
def normal_angle(self,point): #ONLY FOR RENDERER! MAY BE DEPRECATED
"""the angle of the normal with another vector"""
return self.normal.angle(point-self.centre)
def shade(self,light):
"""shading method"""
self.shadeVal=(self.normal.angle(light-self.centre)/math.pi)*255
self.color=Vector(self.color.x - self.shadeVal,self.color.y - self.shadeVal,self.color.z - self.shadeVal)#