-4

Then with this velocity and acceleration and initial position find the next position(2D). The only tricky part is the creation of the vector!

GigI
  • 51
  • 2
  • 9

3 Answers3

1

Just use standard vector math. Distance is the Pythagorean theorem and magnitude is trigonometry:

from math import *

class Vector2D:
    def __init__(self, x, y):
        self.x = float(x)
        self.y = float(y)

    def direction(self):
        return degrees(atan(self.y / self.x))

    def magnitude(self):
        return sqrt(self.x ** 2 + self.y ** 2)
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
0

You can create a class , then each instance (object) of that class would be a velocity object (vector) . A very simple example is -

class Velocity:
    def __init__(self, mag, direction):
        self.mag = mag
        self.direction = direction

Then you can create velocity objects like -

v1 = Velocity(5,5)
v2 = Velocity(10,15)

You can access the magnitude and direction of each velocity as -

print(v1.mag)
>> 5
print(v1.direction)
>> 5

The above is a minimalistic example , you will need to add whatever operations (read functions) you want our velocity object to support.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • Why would direction be a `float`/`double`? it should have at least two dimensions `(i,j)` – Cory Kramer Jun 16 '15 at 18:32
  • @CoryKramer why not? In 2D `(magnitude, direction)` are perfectly fine to define a vector. They contain exactly the same information as an `(x, y)` vector would. – sebastian Jun 16 '15 at 19:14
0

You could create a velocity tuple, where magnitude is at index 0 and direction is at index 1. Then define acceleration as a float and a startingPos where x is at index 0 and y is at index 1.

#0: magnitude 1: direction (degrees above x axis)
velocity = (2.3, 55)
acceleration = 3.2
#0: x 1: y
startingPos = (-10, 0)
heinst
  • 8,520
  • 7
  • 41
  • 77