func NewScaleMatrix(sx, sy float64) Matrix {
	return Matrix{sx, 0, 0, sy, 0, 0}
}
NewScaleMatrix creates a transformation matrix with a sx, sy scale factor
package draw2d
import (
	"math"
)
type Matrix [6]float64
const (
	epsilon = 1e-6
)
func (tr Matrix) Determinant() float64 {
	return tr[0]*tr[3] - tr[1]*tr[2]
}
func (tr Matrix) Transform(points []float64) {
	for i, j := 0, 1; j < len(points); i, j = i+2, j+2 {
		x := points[i]
		y := points[j]
		points[i] = x*tr[0] + y*tr[2] + tr[4]
		points[j] = x*tr[1] + y*tr[3] + tr[5]
	}
}
func (tr Matrix) TransformPoint(x, y float64) (xres, yres float64) {
	xres = x*tr[0] + y*tr[2] + tr[4]
	yres = x*tr[1] + y*tr[3] + tr[5]
	return xres, yres
}
func minMax(x, y float64) (min, max float64) {
	if x > y {
		return y, x
	}
	return x, y
}
func (tr Matrix) TransformRectangle(x0, y0, x2, y2 float64) (nx0, ny0, nx2, ny2 float64) {
	points := []float64{x0, y0, x2, y0, x2, y2, x0, y2}
	tr.Transform(points)
	points[0], points[2] = minMax(points[0], points[2])
	points[4], points[6] = minMax(points[4], points[6])
	points[1], points[3] = minMax(points[1], points[3])
	points[5], points[7] = minMax(points[5], points[7])
	nx0 = math.Min(points[0], points[4])
	ny0 = math.Min(points[1], points[5])
	nx2 = math.Max(points[2], points[6])
	ny2 = math.Max(points[3], points[7])
	return nx0, ny0, nx2, ny2
}
func (tr Matrix) InverseTransform(points []float64) {
	d := tr.Determinant()  
	for i, j := 0, 1; j < len(points); i, j = i+2, j+2 {
		x := points[i]
		y := points[j]
		points[i] = ((x-tr[4])*tr[3] - (y-tr[5])*tr[2]) / d
		points[j] = ((y-tr[5])*tr[0] - (x-tr[4])*tr[1]) / d
	}
}
func (tr Matrix) InverseTransformPoint(x, y float64) (xres, yres float64) {
	d := tr.Determinant()  
	xres = ((x-tr[4])*tr[3] - (y-tr[5])*tr[2]) / d
	yres = ((y-tr[5])*tr[0] - (x-tr[4])*tr[1]) / d
	return xres, yres
}
func (tr Matrix) VectorTransform(points []float64) {
	for i, j := 0, 1; j < len(points); i, j = i+2, j+2 {
		x := points[i]
		y := points[j]
		points[i] = x*tr[0] + y*tr[2]
		points[j] = x*tr[1] + y*tr[3]
	}
}
func NewIdentityMatrix() Matrix {
	return Matrix{1, 0, 0, 1, 0, 0}
}
func NewTranslationMatrix(tx, ty float64) Matrix {
	return Matrix{1, 0, 0, 1, tx, ty}
}