From b77c5f2881a00643f491a7bad138520bb2b5ec73 Mon Sep 17 00:00:00 2001 From: Michael Fogleman Date: Sat, 20 Feb 2016 14:53:33 -0500 Subject: [PATCH] ShearAbout --- README.md | 5 +++-- context.go | 4 ++++ matrix.go | 15 +++++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1e8b921..52a935a 100644 --- a/README.md +++ b/README.md @@ -101,10 +101,11 @@ SetFillRule(fillRule FillRule) Identity() Translate(x, y float64) Scale(x, y float64) -ScaleAbout(sx, sy, x, y float64) Rotate(angle float64) -RotateAbout(angle, x, y float64) Shear(x, y float64) +ScaleAbout(sx, sy, x, y float64) +RotateAbout(angle, x, y float64) +ShearAbout(sx, sy, x, y float64) TransformPoint(x, y float64) (tx, ty float64) InvertY() Push() diff --git a/context.go b/context.go index 6abb827..3d39527 100644 --- a/context.go +++ b/context.go @@ -404,6 +404,10 @@ func (dc *Context) Shear(x, y float64) { dc.matrix = dc.matrix.Shear(x, y) } +func (dc *Context) ShearAbout(sx, sy, x, y float64) { + dc.matrix = dc.matrix.ShearAbout(sx, sy, x, y) +} + func (dc *Context) TransformPoint(x, y float64) (tx, ty float64) { return dc.matrix.TransformPoint(x, y) } diff --git a/matrix.go b/matrix.go index 601fb45..d517839 100644 --- a/matrix.go +++ b/matrix.go @@ -56,12 +56,19 @@ func RotateAbout(angle, x, y float64) Matrix { func Shear(x, y float64) Matrix { return Matrix{ - 1, x, - y, 1, + 1, y, + x, 1, 0, 0, } } +func ShearAbout(sx, sy, x, y float64) Matrix { + a := Translate(-x, -y) + b := Shear(sx, sy) + c := Translate(x, y) + return a.Multiply(b).Multiply(c) +} + func (a Matrix) Multiply(b Matrix) Matrix { return Matrix{ a.XX*b.XX + a.YX*b.XY, @@ -108,3 +115,7 @@ func (a Matrix) RotateAbout(angle, x, y float64) Matrix { func (a Matrix) Shear(x, y float64) Matrix { return Shear(x, y).Multiply(a) } + +func (a Matrix) ShearAbout(sx, sy, x, y float64) Matrix { + return ShearAbout(sx, sy, x, y).Multiply(a) +}