57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
from math import cos, sin, pi
|
|
|
|
class Vector:
|
|
def __init__ (self, x, y):
|
|
self.x = x
|
|
self.y = y
|
|
|
|
def translate (self, other):
|
|
if isinstance (other, list):
|
|
return list(map(lambda v: self.translate(v), other))
|
|
if isinstance (other, Vector):
|
|
return Vector(self.x + other.x, self.y + other.y)
|
|
|
|
def __str__ (self):
|
|
return "[ {}; {} ]".format(self.x, self.y)
|
|
|
|
def __repr__ (self):
|
|
return self.__str__()
|
|
|
|
class Matrix:
|
|
|
|
@classmethod
|
|
def rotation (cls, theta):
|
|
return cls(cos(theta), -sin(theta), sin(theta), cos(theta))
|
|
|
|
@classmethod
|
|
def scale (cls, s):
|
|
return cls(s, 0, 0, s)
|
|
|
|
def __init__ (self, a, b, c, d):
|
|
self.a = a
|
|
self.b = b
|
|
self.c = c
|
|
self.d = d
|
|
|
|
def __str__ (self):
|
|
return "[ {} {}; {} {} ]".format(self.a, self.b, self.c, self.d)
|
|
|
|
def __repr__ (self):
|
|
return self.__str__()
|
|
|
|
def multiply (self, other):
|
|
if isinstance(other, Matrix):
|
|
return Matrix(self.a * other.a + self.b * other.c,
|
|
self.a * other.b + self.b * other.d,
|
|
self.c * other.a + self.d * other.c,
|
|
self.c * other.b + self.d * other.d)
|
|
if isinstance (other, float):
|
|
return Matrix(other * self.a, other * self.b, other * self.c, other * self.d)
|
|
if isinstance (other, Vector):
|
|
return Vector(self.a * other.x + self.b * other.y, self.c * other.x + self.d * other.y)
|
|
if isinstance (other, list):
|
|
return list(map(lambda v: self.multiply(v), other))
|
|
|
|
|
|
|