A simple and efficient 2D matrix transformation library for Go.
Mtransform provides a clean API for performing 2D transformations using 3x3 transformation matrices. It supports common operations like translation, scaling, rotation, and skewing, making it ideal for graphics programming, game development, and geometric calculations.
go get github.com/rustyoz/Mtransformpackage main
import (
"fmt"
"github.com/rustyoz/Mtransform"
)
func main() {
// Create a new transformation matrix
t := mtransform.NewTransform()
// Apply transformations
t.Translate(10, 20) // Move by (10, 20)
t.Scale(2, 2) // Scale by 2x
t.RotateOrigin(math.Pi/4) // Rotate 45 degrees around origin
// Apply transformation to a point
x, y := t.Apply(5, 5)
fmt.Printf("Transformed point: (%.2f, %.2f)\n", x, y)
}type Transform [3][3]float64A 3x3 transformation matrix for 2D transformations.
Creates and returns an identity transformation matrix.
Creates a new transformation matrix initialized to the identity matrix.
Multiplies two transformation matrices and returns the result.
Applies the transformation to the given coordinates and returns the transformed point.
Multiplies the current transformation matrix with another transformation matrix in-place.
Applies scaling transformation to the matrix.
Applies translation transformation to the matrix.
Applies rotation around the origin. Angle is in radians.
Applies rotation around a specific point. Angle is in radians.
Applies skew transformation along the X-axis. Angle is in radians.
Applies skew transformation along the Y-axis. Angle is in radians.
Compares two transformation matrices for equality.
t := mtransform.NewTransform()
// Chain multiple transformations
t.Translate(100, 50) // Move to (100, 50)
t.Scale(1.5, 1.5) // Scale up by 1.5x
t.RotateOrigin(math.Pi/6) // Rotate 30 degrees
// Apply to multiple points
points := [][]float64{{0, 0}, {10, 10}, {20, 0}}
for _, point := range points {
x, y := t.Apply(point[0], point[1])
fmt.Printf("(%.1f, %.1f) -> (%.1f, %.1f)\n", point[0], point[1], x, y)
}// Create two separate transformations
t1 := mtransform.NewTransform()
t1.Scale(2, 2)
t2 := mtransform.NewTransform()
t2.Translate(10, 10)
// Combine them
combined := mtransform.MultiplyTransforms(*t1, *t2)
// Or multiply in-place
t1.MultiplyWith(*t2)t := mtransform.NewTransform()
// Rotate 90 degrees around point (50, 50)
t.RotatePoint(math.Pi/2, 50, 50)
// Apply to a point
x, y := t.Apply(60, 50) // Point 10 units to the right of rotation center
fmt.Printf("Rotated point: (%.1f, %.1f)\n", x, y) // Should be (50, 60)This project is licensed under the terms specified in the LICENSE file.
Contributions are welcome! Please feel free to submit issues and pull requests.