def scalebox(p1, p2): p1 = Vector(p1) p2 = Vector(p2) x1, y1 = p1 x2, y2 = p2 return (Matrix.translate(p1) * Matrix.scaleXY(x2 - x1, y2 - y1)).inverse() #m = [[1.0/(x2-x1), 0, 0], [0, 1.0/(y2-y1), 0], [-x1, -y1, 1]] #m = [[1.0/(x2-x1), 0, -1.0/x1], [0, 1.0/(y2-y1), -1.0/y1], [0, 0, 1]] return Matrix(m)
def rotate(angle): """Return a rotate matrix, angle in radians anticlockwise""" return Matrix( numpy.array( [[math.cos(angle), -math.sin(angle), 0], [math.sin(angle), math.cos(angle), 0], [0, 0, 1]], numpy.Float))
def scaleboxInverse(p1, p2): #p1 = Vector(p1) #p2 = Vector(p2) x1, y1 = p1 x2, y2 = p2 #return (Matrix.translate(p1) * Matrix.scaleXY(x2-x1, y2-y1)).inverse() #m = [[(x2-x1), 0, 0], [0, (y2-y1), 0], [x1, y1, 1]] m = [[(x2 - x1), 0, x1], [0, (y2 - y1), y1], [0, 0, 1]] return Matrix(m)
def __mul__(self, other): """Multiplies with a Matrix or point""" #if type(other) == types.TupleType: if issequence(other) and len(other) == 2: x, y = other if True: x, y = float(x), float(y) vec = array([x, y, 1]) newvec = numpy.dot(self.matrix, vec) newx = newvec[0] / newvec[2] newy = newvec[1] / newvec[2] return Vector((newx, newy)) else: m = self.matrix nx = m[0, 0] * x + m[0, 1] * y + m[0, 2] ny = m[1, 0] * x + m[1, 1] * y + m[1, 2] nw = m[2, 0] * x + m[2, 1] * y + m[2, 2] return Vector((nx / nw, ny / nw)) elif type(other) == Matrix: newmatrix = numpy.matrixmultiply(self.matrix, other.matrix) return Matrix(newmatrix[0, 0], newmatrix[1, 1], newmatrix[0, 2], newmatrix[1, 2]) else: raise Exception, "can only multiply with 'point tuples', and Matrices"
def nolocation(self): matrix = numpy.array(self.matrix) matrix[0][2] = 0 matrix[1][2] = 0 return Matrix(matrix)
def shear(x, y): """Returns a shear matrix""" return Matrix( numpy.array([[1, x, 0], [y, 1, 0], [0, 0, 1]], numpy.Float))
def scaleXY(x, y): """Returns a non-uniform scale matrix""" return Matrix( numpy.array([[x, 0, 0], [0, y, 0], [0, 0, 1]], numpy.Float))
def scale(s): """Returns a uniform scale matrix""" return Matrix( numpy.array([[s, 0, 0], [0, s, 0], [0, 0, 1]], numpy.Float))
def translate(point): """Return a translation matrix""" x, y = point return Matrix( numpy.array([[1, 0, x], [0, 1, y], [0, 0, 1]], numpy.Float))
def inverse(self): """Return the inverse of this matrix""" return Matrix(linear_algebra.inverse(self.matrix))