-
Notifications
You must be signed in to change notification settings - Fork 0
/
vetor.py
73 lines (57 loc) · 1.88 KB
/
vetor.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
# -*- coding: utf-8 -*-
from __future__ import division
from numpy import sqrt
class Vetor:
"""Classe Vetor, representa um vetor do R^3"""
def __init__(self, u=(0, 0, 0)):
self.x = u[0]
self.y = u[1]
self.z = u[2]
def __str__(self):
txt = "%.14f, %.14f, %.14f"%(self.x, self.y, self.z)
return txt
def __eq__(self, other):
return (self.x == other.x and
self.y == other.y and
self.z == other.z)
def __ne__(self, other):
return not self == other
def __add__(self, other):
soma = Vetor()
soma.x = self.x + other.x
soma.y = self.y + other.y
soma.z = self.z + other.z
return soma
def to_list(self):
return [self.x, self.y, self.z]
def multiply(self, real):
novo = Vetor()
novo.x = self.x * real
novo.y = self.y * real
novo.z = self.z * real
return novo
def __sub__(self, other):
return self + other.multiply(-1)
def modulo2(self):
return self.x*self.x + self.y*self.y + self.z*self.z
def modulo3(self):
mod3 = self.modulo2()
return sqrt(mod3*mod3*mod3)
def modulo(self):
return sqrt(self.modulo2())
def unit(self):
return self.multiply(1/self.modulo())
def distancia(self, other):
'''(Vetor, Vetor) -> float
Recebe referências `self` e `other` a objetos vetor e
retorna a distância euclidiana entre os pontos representados
por `self` e `other`.'''
d = self - other
return d.modulo()
def distancia2(self, other):
'''(Vetor, Vetor) -> float
Recebe referências `self` e `other` a objetos vetor e
retorna a distância quadrática euclidiana entre os pontos representados
por `self` e `other`.'''
d = self - other
return d.modulo2()