-
Notifications
You must be signed in to change notification settings - Fork 3
/
linear.py
37 lines (24 loc) · 872 Bytes
/
linear.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
from math import *
def d(a, b):
return sqrt(sum(map(lambda i,j: (i-j)*(i-j), a, b)))
def magnitude( (x, y, z) ):
return sqrt( x*x + y*y + z*z )
def scalar_product( (x1,y1,z1), (x2,y2,z2) ):
return x1*x2+y1*y2+z1*z2
def cross_product( (x1,y1,z1), (x2,y2,z2) ):
return ( y1*z2-y2*z1, z1*x2-z2*x1, x1*y2-x2*y1 )
def vector_add( (x1,y1,z1), (x2,y2,z2) ):
return (x1+x2, y1+y2, z1+z2)
def vector_sub( (x1,y1,z1), (x2,y2,z2) ):
return (x1-x2, y1-y2, z1-z2)
def scalar_mul( s, (x,y,z) ):
return (s*x, s*y, s*z)
def normalize( (x, y, z) ):
d = sqrt(x*x+y*y+z*z)
return (x/d, y/d, z/d)
def interpolate( (x1,y1,z1), (x2,y2,z2), frac ):
return ( x2*frac+x1*(1-frac), y2*frac+y1*(1-frac), z2*frac+z1*(1-frac) )
def vector_sum( *vecs):
return reduce( vector_add, vecs )
def norm_add( *vecs ):
return normalize( vector_sum( *vecs ) )