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()
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(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()
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()