diff --git a/geometry.go b/geometry.go index 621a0b4..e847b52 100644 --- a/geometry.go +++ b/geometry.go @@ -405,7 +405,7 @@ func (m Matrix) Project(u Vec) Vec { func (m Matrix) Unproject(u Vec) Vec { det := m[0]*m[3] - m[2]*m[1] return Vec{ - m[3]/det*u.X - m[2]/det*u.Y + m[2]*m[5] - m[3]*m[4], - -m[1]/det*u.X + m[0]/det*u.Y + m[1]*m[4] - m[0]*m[5], + m[3]/det*u.X - m[2]/det*u.Y + (m[2]*m[5]-m[3]*m[4])/det, + -m[1]/det*u.X + m[0]/det*u.Y + (m[1]*m[4]-m[0]*m[5])/det, } } diff --git a/geometry_test.go b/geometry_test.go index b1ce00f..17b184f 100644 --- a/geometry_test.go +++ b/geometry_test.go @@ -100,6 +100,12 @@ func TestMatrix_Unproject(t *testing.T) { assert.InDelta(t, unprojected.X, 2, delta) assert.InDelta(t, unprojected.Y, 2, delta) }) + t.Run("for scaled, rotated and moved matrix", func(t *testing.T) { + matrix := pixel.IM.Scaled(pixel.ZV, 2).Rotated(pixel.ZV, math.Pi/2).Moved(pixel.V(2, 2)) + unprojected := matrix.Unproject(pixel.V(-2, 6)) + assert.InDelta(t, unprojected.X, 2, delta) + assert.InDelta(t, unprojected.Y, 2, delta) + }) t.Run("for rotated and moved matrix", func(t *testing.T) { matrix := pixel.IM.Rotated(pixel.ZV, math.Pi/2).Moved(pixel.V(1, 1)) unprojected := matrix.Unproject(pixel.V(1, 2))