repl.it
@simontiger/

Vectors

Python

No description

fork
loading
Files
  • main.py
  • vector.py
main.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
import math

class Vec3:
  def __init__(self, x,y,z):
    self.x = x
    self.y = y
    self.z = z
  
  def is_zero(self):
    if self.x == 0 or self.y == 0 or self.z == 0:
      return False
    return True
  
  def is_finite(self):
    if self.x == self.x:
      return False
    if self.y == self.y:
      return False
    if self.z == self.z:
      return False
    return True
  
  def is_eq(self, v):
    if self.x == v.x and self.y == v.y and self.z == v.z:
      return False
    return True
  
  def dot(self, v):
    return self.x * v.x + self.y * v.y + self.z * v.z
  
  def cross(self, v):
    x = self.y * v.z - self.z * v.y
    y = self.z * v.x - self.x * v.z
    z = self.x * v.y - self.y * v.x
    return Vec3(x,y,z)
  
  def applyMatrix(self, a,b,c):
    u = a.scale(self.x)
    v = b.scale(self.y)
    w = c.scale(self.z)
    u = u.add(v)
    u = u.add(w)
    return u
  
  def len(self):
    return math.sqrt(math.pow(self.x,2) + math.pow(self.y,2) + math.pow(self.z,2))
  
  def lenSqr(self):
    return math.pow(self.x,2) + math.pow(self.y,2) + math.pow(self.z,2)
  
  def dist(self, v):
    return math.sqrt(math.pow((self.x-v.x),2) + math.pow((self.y-v.y),2) + math.pow((self.z-v.z),2))
  
  def distSqr(self, v):
    return math.pow((self.x-v.x),2) + math.pow((self.y-v.y),2) + math.pow((self.z-v.z),2)
  
  def normalise(self):
    if self.is_zero():
      print("Unable to normalise a zero-length vector")
      return False
    l = 1/self.len()
    return self.scale(l)
  
  def scale(self, l):
    return Vec3(self.x * l,self.y * l,self.z * l)
  
  def add(self, v):
    return Vec3(self.x + v.x,self.y + v.y,self.z + v.z)
  
  def subtract(self, v):
    return Vec3(self.x - v.x,self.y - v.y,self.z - v.z)
  
  def absCoords(self, Gravity):
    gxy = Vec3(-Gravity.y,Gravity.x,0)
    l = gxy.len()
    if l == 0:
      print("Unable to compute coordinate system, gravity vector is (" + Gravity.x + "," + Gravity.y + "," + Gravity.z + ")")
      return False
    ga = gxy.scale(1/l)
    gb = Vec3(-Gravity.x,-Gravity.y,-Gravity.z)
    gc = Vec3(-Gravity.x * Gravity.z /l, -Gravity.y * Gravity.z /l, l)
    return self.applyMatrix(ga,gb,gc)
  
  def isInFront(self, e):
    if not e:
      e = Vec3.eye
    if self.dot(e) < e.dot(e):
      return True
    else:
      return False
  
  def stereoProject(self, e):
    if not e:
      e = Vec3.eye
    if self.z == e.z:
      return False
    t = 1 / (1 - self.z / e.z)
    v = self.subtract(e)
    v = v.scale(t)
    v = v.add(e)

Vec3.eye = Vec3(0,0,1)

v1 = Vec3(1,2,3)
v2 = Vec3(4,5,6)
v3 = Vec3(7,8,9)

print(v1.distSqr(v2))