# $Id$ # simple matrix class for affine transforms import math ## # Simple 3x3 matrix class. This class is useful for calculating affine # transforms for PIL and aggdraw, and not much else. class Matrix: def __init__(self, matrix=None): if matrix is None: self.setidentity() else: self.setmatrix(matrix) def setidentity(self): self.a = 1; self.b = 0; self.c = 0 self.d = 0; self.e = 1; self.f = 0 def setmatrix(self, m): if isinstance(m, Matrix): self.a = m.a; self.b = m.b; self.c = m.c self.d = m.d; self.e = m.e; self.f = m.f else: # assume it's a matrix self.a = m[0]; self.b = m[1]; self.c = m[2] self.d = m[3]; self.e = m[4]; self.f = m[5] def getmatrix(self): return self.a, self.b, self.c, self.d, self.e, self.f def transform(self, x, y): X = self.a*x + self.b*y + self.c Y = self.d*x + self.e*y + self.f return X, Y def multiply(self, a, b, c, d, e, f): _a=self.a; _b=self.b; _c=self.c _d=self.d; _e=self.e; _f=self.f self.a = _a*a + _d*b; self.b = _b*a + _e*b; self.c = _c*a + _f*b + c; self.d = _a*d + _d*e; self.e = _b*d + _e*e; self.f = _c*d + _f*e + f; def rotate(self, a): a = a * math.pi / 180.0 c = math.cos(a) s = math.sin(a) self.multiply(c, -s, 0, s, c, 0) def scale(self, x, y): self.multiply(x, 0, 0, 0, y, 0) def translate(self, x, y): self.multiply(1, 0, x, 0, 1, y)