From 1003203bb13f2c70c5c22b58b833c777b6b731d6 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 1 Jul 2023 04:01:03 -0400 Subject: [PATCH] Glaggle example draws glaggle as expected --- examples/glaggle/main.go | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/examples/glaggle/main.go b/examples/glaggle/main.go index fcd187a..b77f8dc 100644 --- a/examples/glaggle/main.go +++ b/examples/glaggle/main.go @@ -1,6 +1,7 @@ package main import "os" +import "math" import "image" import "image/png" import "git.tebibyte.media/tomo/ggfx" @@ -13,18 +14,21 @@ func main () { Bounds: img.Rect, Width: 4, } - black := []uint8 { 0, 0, 0, 255 } - red := []uint8 { 255, 0, 0, 255 } - canvas.FillPolygon ( - black, - image.Pt(10, 10), - image.Pt(70, 50), - image.Pt(20, 70)) - canvas.StrokePolygon ( - red, 1, - image.Pt(10, 10), - image.Pt(70, 50), - image.Pt(20, 70)) + black := []uint8 { 0, 0, 0, 255 } + yellow := []uint8 { 255, 255, 0, 255 } + midpoint := image.Pt(canvas.Bounds.Dx() / 2, canvas.Bounds.Dy() / 2) + + face := circle(32, 100, midpoint) + canvas.FillPolygon(yellow, face...) + canvas.StrokePolygon(black, 2, face...) + + eye := circle(12, 16, midpoint.Add(image.Pt(-40, -30))) + canvas.FillPolygon(black, eye...) + eye = circle(12, 16, midpoint.Add(image.Pt(40, -30))) + canvas.FillPolygon(black, eye...) + + mouth := circle(16, 70, midpoint) + canvas.StrokePolygon(black, 2, mouth[1:8]...) file, err := os.Create("TEST.png") if err != nil { panic(err.Error()) } @@ -35,3 +39,14 @@ func main () { println("Open TEST.png to see the result.") } +func circle (resolution int, radius float64, center image.Point) []image.Point { + points := make([]image.Point, resolution) + for index := range points { + angle := float64(index) * ((2 * math.Pi) / float64(len(points))) + point := image.Pt ( + int(math.Cos(angle) * radius), + int(math.Sin(angle) * radius)) + points[index] = point.Add(center) + } + return points +}