-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVetor.py
More file actions
70 lines (50 loc) · 1.55 KB
/
Vetor.py
File metadata and controls
70 lines (50 loc) · 1.55 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
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
#!/usr/bin/env python3
# coding: "UTF-8"
# Autor: Renan Torres <pesterenan@gmail.com>
# Data: 07/02/2019
# Módulo Vetor
import math
class Vetor:
x = 0
y = 0
direcao = 0
def __init__(self, arg0=None, arg1=None):
if type(arg0) == tuple:
self.x = round(arg0[0], 3)
self.y = round(arg0[1], 3)
else:
self.x = round(arg0, 3)
self.y = round(arg1, 3)
def __str__(self):
return "(" + str(round(self.x, 3)) + "," + str(round(self.y, 3)) + ")"
def inverte(self):
return Vetor(self.y, self.x)
def magnitude(self):
return math.sqrt(self.x ** 2 + self.y ** 2)
def normalizar(self):
m = self.magnitude()
if m != 0:
return Vetor(self.x / m, self.y / m)
return Vetor(self.x, self.y)
def limitar(self, maxi):
if self.magnitude() > maxi:
self.normalizar()
self.multiplica(maxi)
def soma(self, outro):
return Vetor(self.x + outro.x, self.y + outro.y)
def subtrai(self, outro):
return Vetor(self.x - outro.x, self.y - outro.y)
def multiplica(self, escalar):
return Vetor(self.x * escalar, self.y * escalar)
def divide(self, escalar):
if escalar != 0:
return Vetor(self.x / escalar, self.y / escalar)
return Vetor(0, 0)
def angulo_direcao(self):
self.direcao = (math.atan2(self.y, self.x) / math.pi) * 180
return self.direcao
def vetor_distancia(vetor_a, vetor_b):
return Vetor(-(vetor_a[2]) + (vetor_b[2]), -(vetor_a[1]) + (vetor_b[1]))
def angulo_direcao(vetor):
direcao = (math.atan2(vetor.y, vetor.x) / math.pi) * 180
return round(direcao, 2)