class Vehicle(object): def __init__(self, location, velocity, target): self.location = location self.velocity = velocity self.acceleration = Vector(0.1, 0.1) self.max_speed = 1 self.max_force = 1 self.target = target def seek(self, target): # find vector to target desired = Vector.static_sub(target, self.location) desired.normalize() desired.mult(self.max_speed) # Reynold's formula for steering force steering_force = Vector.static_sub(desired, self.velocity) # apply force self.acceleration.add(steering_force) # recalc position self.velocity.add(self.acceleration) self.location.add(self.velocity) print(self.location.x, self.location.y) def update(self, canvas): self.seek(self.target) def render(self, canvas): # seeker canvas.create_circle(self.location.x, self.location.y, 7, fill='black') # target canvas.create_circle(self.target.x, self.target.y, 7, fill='red')
def __init__(self, position=None, velocity=None): self.img = Image.open(os.path.join('..', 'assets', 'black_square.png')) self.tk_img = ImageTk.PhotoImage(self.img.rotate(0)) self.position = Vector(60, 60) if position is None else position self.velocity = Vector(0, 0) if velocity is None else position self.acceleration = Vector(0.01, 0.01) self.side = 17 self.angle = 0 self.angular_velocity = 0 self.angular_acceleration = 0
def seek(self, target): # find vector to target desired = Vector.static_sub(target, self.location) desired.normalize() desired.mult(self.max_speed) # Reynold's formula for steering force steering_force = Vector.static_sub(desired, self.velocity) # apply force self.acceleration.add(steering_force) # recalc position self.velocity.add(self.acceleration) self.location.add(self.velocity) print(self.location.x, self.location.y)
def __init__(self, location, velocity, target): self.location = location self.velocity = velocity self.acceleration = Vector(0.1, 0.1) self.max_speed = 1 self.max_force = 1 self.target = target
def __init__(self, normal_vector=None, constant_term=None): self.dimension = 2 if not normal_vector: all_zeros = ['0'] * self.dimension normal_vector = Vector(all_zeros) self.normal_vector = normal_vector if not constant_term: constant_term = Decimal('0') self.constant_term = Decimal(constant_term) self.set_basepoint()
class Square(object): def __init__(self, position=None, velocity=None): self.img = Image.open(os.path.join('..', 'assets', 'black_square.png')) self.tk_img = ImageTk.PhotoImage(self.img.rotate(0)) self.position = Vector(60, 60) if position is None else position self.velocity = Vector(0, 0) if velocity is None else position self.acceleration = Vector(0.01, 0.01) self.side = 17 self.angle = 0 self.angular_velocity = 0 self.angular_acceleration = 0 def update(self, canvas): width = int(canvas['width']) height = int(canvas['height']) if self.position.x > width: self.position.x = 0 if self.position.y > height: self.position.y = 0 self.velocity.add(self.acceleration) self.position.add(self.velocity) self.angle = (math.degrees(math.atan2(self.velocity.y, self.velocity.x))) print(self.angle) def render(self, canvas): self.tk_img = ImageTk.PhotoImage(self.img.rotate(self.angle)) canvas.create_image(50, 50, image=self.tk_img) def event_left_pressed(self, event): self.velocity.y += 1 def event_right_pressed(self, event): self.velocity.y -= 1
def set_basepoint(self): try: n = self.normal_vector c = self.constant_term basepoint_coords = ['0'] * self.dimension initial_index = Line.first_nonzero_index(n) initial_coefficient = n[initial_index] basepoint_coords[initial_index] = c / initial_coefficient self.basepoint = Vector(basepoint_coords) except Exception as e: if str(e) == Line.NO_NONZERO_ELTS_FOUND_MSG: self.basepoint = None else: raise e
from vectors.vector import Vector if __name__ == "__main__": # Addition v1 = Vector([8.218, -9.341]) v2 = Vector([-1.129, 2.111]) print('{} + {} = {}'.format(v1, v2, v1 + v2)) print() # Subtraction v3 = Vector([7.119, 8.215]) v4 = Vector([-8.223, 0.878]) print('{} - {} = {}'.format(v3, v4, v3 - v4)) print() # Multiplication v5 = 7.41 v6 = Vector([1.671, -1.012, -0.318]) print('{} * {} = {}'.format(v5, v6, v5 * v6)) print() # Magnitude v7 = Vector([-0.221, 7.437]) v8 = Vector([8.813, -1.331, -6.247]) print('||{}|| = {}'.format(v7, v7.magnitude())) print('||{}|| = {}'.format(v8, v8.magnitude())) print() # Normalisation v9 = Vector([5.581, -2.136]) v10 = Vector([1.996, 3.108, -4.554])
from matplotlib import pyplot as plt from vectors.vector import Vector axes = plt.axes() v1 = Vector(dx=1, dy=0.5) v2 = Vector(dx=1.5, dy=3) v3 = Vector(dx=1, dy=1) v4 = Vector.sum([v1, v2, v3]) axes.arrow(v1.x, v1.y, v1.dx, v1.dy, ec='r', linestyle='dashdot') axes.arrow(v2.x, v2.y, v2.dx, v2.dy, ec='g', linestyle='dashdot') axes.arrow(v3.x, v3.y, v3.dx, v3.dy, ec='b', linestyle='dashdot') axes.arrow(v4.x, v4.y, v4.dx, v4.dy, ec='k') plt.axis([0, 6, 0, 6]) plt.title('Sum of Multiple Vectors') plt.show()
from matplotlib import pyplot as plt from vectors.vector import Vector axes = plt.axes() v1 = Vector(0, 0, 3, 0) v2 = Vector(3, 0, 0, 4) v3 = Vector.add(v1, v2) axes.arrow(v1.x, v1.y, v1.dx, v1.dy, ec='r') axes.arrow(v2.x, v2.y, v2.dx, v2.dy, ec='g') axes.arrow(v3.x, v3.y, v3.dx, v3.dy, ec='b') plt.axis([0, 5, 0, 5]) plt.title('Adding Vectors') plt.show()