Push / Pop

This commit is contained in:
Michael Fogleman 2016-02-19 13:34:04 -05:00
parent d4efe60192
commit e554ef45e8
3 changed files with 39 additions and 5 deletions

View File

@ -47,6 +47,7 @@ type Context struct {
fillRule FillRule fillRule FillRule
fontFace font.Face fontFace font.Face
matrix Matrix matrix Matrix
stack []*Context
} }
func NewContext(width, height int) *Context { func NewContext(width, height int) *Context {
@ -362,3 +363,35 @@ func (dc *Context) Shear(x, y float64) {
func (dc *Context) TransformPoint(x, y float64) (tx, ty float64) { func (dc *Context) TransformPoint(x, y float64) (tx, ty float64) {
return dc.matrix.TransformPoint(x, y) return dc.matrix.TransformPoint(x, y)
} }
// Stack
func (dc *Context) Push() {
path := make(raster.Path, len(dc.path))
copy(path, dc.path)
dc.stack = append(dc.stack, &Context{
color: dc.color,
path: path,
start: dc.start,
lineWidth: dc.lineWidth,
lineCap: dc.lineCap,
lineJoin: dc.lineJoin,
fillRule: dc.fillRule,
fontFace: dc.fontFace,
matrix: dc.matrix,
})
}
func (dc *Context) Pop() {
s := dc.stack
x, s := s[len(s)-1], s[:len(s)-1]
dc.color = x.color
dc.path = x.path
dc.start = x.start
dc.lineWidth = x.lineWidth
dc.lineCap = x.lineCap
dc.lineJoin = x.lineJoin
dc.fillRule = x.fillRule
dc.fontFace = x.fontFace
dc.matrix = x.matrix
}

View File

@ -7,12 +7,13 @@ func main() {
dc := gg.NewContext(S, S) dc := gg.NewContext(S, S)
dc.SetRGB(1, 1, 1) dc.SetRGB(1, 1, 1)
dc.Clear() dc.Clear()
dc.SetRGBA(0, 0, 0, 0.1)
for i := 0; i < 360; i += 15 { for i := 0; i < 360; i += 15 {
dc.Identity() dc.Push()
dc.RotateAbout(gg.Radians(float64(i)), S/2, S/2) dc.RotateAbout(gg.Radians(float64(i)), S/2, S/2)
dc.DrawEllipse(S/2, S/2, S*7/16, S/8) dc.DrawEllipse(S/2, S/2, S*7/16, S/8)
dc.SetRGBA(0, 0, 0, 0.1)
dc.Fill() dc.Fill()
dc.Pop()
} }
dc.WritePNG("out.png") dc.WritePNG("out.png")
} }

View File

@ -41,10 +41,10 @@ func Rotate(angle float64) Matrix {
} }
func RotateAbout(angle, x, y float64) Matrix { func RotateAbout(angle, x, y float64) Matrix {
a := Translate(x, y) a := Translate(-x, -y)
b := Rotate(angle) b := Rotate(angle)
c := Translate(-x, -y) c := Translate(x, y)
return c.Multiply(b).Multiply(a) return a.Multiply(b).Multiply(c)
} }
func Shear(x, y float64) Matrix { func Shear(x, y float64) Matrix {