#π Simulating collision w/ python
6 messages Β· Page 1 of 1 (latest)
@vital zodiac
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
import math
class Vector:
def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z
def __repr__(self):
return f"Vector({self.x}, {self.y}, {self.z})"
def __str__(self):
return f"{self.x}i + {self.y}j + {self.z}k"
def __getitem__(self, i):
if i == 0:
return self.x
elif i == 1:
return self.y
elif i == 2:
return self.z
def __add__(self, v):
return Vector(self.x+v.x, self.y+v.y, self.z+v.z)
def __sub__(self, v):
return Vector(self.x-v.x, self.y-v.y, self.z-v.z)
def __mul__(self, v):
if isinstance(v, Vector):
return self.x*v.x+self.y*v.y+self.z*v.z
elif isinstance(v, (int, float)):
return Vector(self.x*v, self.y*v, self.z*v)
def __truediv__(self, v):
if isinstance(v, (int, float)):
return Vector(self.x/v, self.y/v, self.z/v)
def get_norm(self):
return math.sqrt(self.x**2 + self.y**2 + self.z**2)
def normalize(self):
return Vector(self.x/self.get_norm(), self.y/self.get_norm(), self.z/self.get_norm())
this one is the main file:
https://paste.pythondiscord.com/TDNQ
@vital zodiac
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.