-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgmath.py
More file actions
39 lines (29 loc) · 1.01 KB
/
Copy pathgmath.py
File metadata and controls
39 lines (29 loc) · 1.01 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
import math
from display import *
#vector functions
#normalize vetor, should modify the parameter
def normalize(vector):
magnitude = math.sqrt( vector[0] * vector[0] +
vector[1] * vector[1] +
vector[2] * vector[2])
for i in range(3):
vector[i] = vector[i] / magnitude
#Return the dot porduct of a . b
def dot_product(a, b):
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
#Calculate the surface normal for the triangle whose first
#point is located at index i in polygons
def calculate_normal(polygons, i):
A = [0, 0, 0]
B = [0, 0, 0]
N = [0, 0, 0]
A[0] = polygons[i+1][0] - polygons[i][0]
A[1] = polygons[i+1][1] - polygons[i][1]
A[2] = polygons[i+1][2] - polygons[i][2]
B[0] = polygons[i+2][0] - polygons[i][0]
B[1] = polygons[i+2][1] - polygons[i][1]
B[2] = polygons[i+2][2] - polygons[i][2]
N[0] = A[1] * B[2] - A[2] * B[1]
N[1] = A[2] * B[0] - A[0] * B[2]
N[2] = A[0] * B[1] - A[1] * B[0]
return N