From a92fa10eb14148a76664087838a1f8e37abd70de Mon Sep 17 00:00:00 2001 From: Yongwei Xing Date: Tue, 2 Mar 2021 18:02:47 +0800 Subject: [PATCH] add circle line --- circleline.go | 52 +++++++++++++++++++++++++++++++++++ example/example_circleline.go | 18 ++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 circleline.go create mode 100644 example/example_circleline.go diff --git a/circleline.go b/circleline.go new file mode 100644 index 0000000..5561e8d --- /dev/null +++ b/circleline.go @@ -0,0 +1,52 @@ +package generativeart + +import ( + "github.com/fogleman/gg" + "math" + "math/rand" +) + +type point struct { + x, y float64 +} + +type circleLine struct { + step float64 + lineNum int + radius float64 +} + +// NewCircleLine returns a circleLine object. +func NewCircleLine(step float64, lineNum int, radius float64) *circleLine { + return &circleLine{ + step: step, + lineNum: lineNum, + radius: radius, + } +} + +// Generative draws a cirle line image. +func (cl *circleLine) Generative(c *canva) { + ctex := gg.NewContextForRGBA(c.img) + ctex.SetLineWidth(c.opts.lineWidth) + ctex.SetColor(c.opts.lineColor) + var points []point + for theta := -math.Pi; theta <= math.Pi; theta += cl.step { + x := cl.radius * math.Cos(theta) + y := cl.radius * math.Sin(theta) + xi, yi := ConvertCartesianToPixel(x, y, c.xaixs, c.yaixs, c.width, c.height) + points = append(points, point{ + x: float64(xi), + y: float64(yi), + }) + } + + for i := 0; i < cl.lineNum; i++ { + p1 := points[rand.Intn(len(points))] + p2 := points[rand.Intn(len(points))] + + ctex.MoveTo(p1.x, p1.y) + ctex.LineTo(p2.x, p2.y) + ctex.Stroke() + } +} diff --git a/example/example_circleline.go b/example/example_circleline.go new file mode 100644 index 0000000..e6a35a5 --- /dev/null +++ b/example/example_circleline.go @@ -0,0 +1,18 @@ +package main + +import ( + "generativeart" + "math/rand" + "time" +) + +func main() { + rand.Seed(time.Now().Unix()) + c := generativeart.NewCanva(800, 800, 2, 2) + c.SetBackground(generativeart.Tan) + c.SetLineWidth(1.0) + c.SetLineColor(generativeart.Lavender) + c.FillBackground() + c.Draw(generativeart.NewCircleLine(0.02, 600, 1.5)) + c.ToPNG("circleline.png") +}