add reflect method for matrix

This commit is contained in:
Scott Winkler 2018-04-04 23:30:38 -07:00
parent bd5138f4c1
commit c986401ee8
1 changed files with 11 additions and 0 deletions

View File

@ -3,6 +3,8 @@ package pixel
import (
"fmt"
"math"
"github.com/scottwinkler/pixel"
)
// Clamp returns x clamped to the interval [min, max].
@ -371,6 +373,15 @@ func (m Matrix) Rotated(around Vec, angle float64) Matrix {
return m
}
//Reflect reflects everything around a given point by the given angle in radians
func (m Matrix) Reflect(around pixel.Vec, angle float64) pixel.Matrix {
sin2t, cos2t := math.Sincos(2 * angle)
m[4], m[5] = m[4]-around.X, m[5]-around.Y
m = m.Chained(pixel.Matrix{cos2t, sin2t, sin2t, -cos2t, 0, 0})
m[4], m[5] = m[4]+around.X, m[5]+around.Y
return m
}
// Chained adds another Matrix to this one. All tranformations by the next Matrix will be applied
// after the transformations of this Matrix.
func (m Matrix) Chained(next Matrix) Matrix {