diff --git a/README.md b/README.md index 705b30b..0913cee 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ This package is still working in progress. More types would be added. Welcome an - Color Circle2 - Circle Grid - Contour Line +- Noise Line For these kinds of art, the package provides as many parameters to control the appearance. @@ -57,6 +58,7 @@ NewColorCircle(circleNum int) NewColorCircle2(circleNum int) NewCircleGrid(circleNumMin, circleNumMax int) NewContourLine(lineNum int) +NewNoiseLine(n int) ``` ## Docs @@ -215,6 +217,28 @@ func main() { ![](images/contourline.png) +### Noise Line + +```go +func main() { + rand.Seed(time.Now().Unix()) + colors := []color.RGBA{ + {0x06, 0x7B, 0xC2, 0xFF}, + {0x84, 0xBC, 0xDA, 0xFF}, + {0xEC, 0xC3, 0x0B, 0xFF}, + {0xF3, 0x77, 0x48, 0xFF}, + {0xD5, 0x60, 0x62, 0xFF}, + } + c := generativeart.NewCanva(1000, 1000) + c.SetBackground(color.RGBA{0xF0, 0xFE, 0xFF, 0xFF}) + c.FillBackground() + c.SetColorSchema(colors) + c.Draw(generativeart.NewNoiseLine(1000)) + c.ToPNG("noiseline.png") +} +``` + +![](images/noiseline.png) ### Circle Loop ```go diff --git a/contourline.go b/contourline.go index 5ef5f85..7b5359c 100644 --- a/contourline.go +++ b/contourline.go @@ -21,7 +21,6 @@ func NewContourLine(lineNum int) *contourLine { // Generative draws a contour line image. func (cl *contourLine) Generative(c *canva) { ctex := gg.NewContextForRGBA(c.img) - //noise := perlin.NewPerlin(2, 2, 3, rand.Int63()) noise := common.NewPerlinNoise() for i := 0; i < cl.lineNum; i++ { cl := c.opts.colorSchema[rand.Intn(len(c.opts.colorSchema))] diff --git a/docs/doc.md b/docs/doc.md index 851a8dd..0c4e103 100644 --- a/docs/doc.md +++ b/docs/doc.md @@ -93,4 +93,18 @@ func julia1(z complex128) complex128 { julia := generativeart.NewJulia(julia1, 40, 1.5, 1.5) ``` -![](../images/julia.png) \ No newline at end of file +![](../images/julia.png) + +## Noise Line + +`Noise Line` draws some random line and circles based on `perlin noise`. + +### parameters + +- n: The number of random line. + +```go +nl := generativeart.NewNoiseLine(1000) +``` + +![](../images/noiseline.png) \ No newline at end of file diff --git a/example/example_noiseline.go b/example/example_noiseline.go new file mode 100644 index 0000000..a74b1e1 --- /dev/null +++ b/example/example_noiseline.go @@ -0,0 +1,25 @@ +package main + +import ( + "github.com/jdxyw/generativeart" + "image/color" + "math/rand" + "time" +) + +func main() { + rand.Seed(time.Now().Unix()) + colors := []color.RGBA{ + {0x06, 0x7B, 0xC2, 0xFF}, + {0x84, 0xBC, 0xDA, 0xFF}, + {0xEC, 0xC3, 0x0B, 0xFF}, + {0xF3, 0x77, 0x48, 0xFF}, + {0xD5, 0x60, 0x62, 0xFF}, + } + c := generativeart.NewCanva(1000, 1000) + c.SetBackground(color.RGBA{0xF0, 0xFE, 0xFF, 0xFF}) + c.FillBackground() + c.SetColorSchema(colors) + c.Draw(generativeart.NewNoiseLine(1000)) + c.ToPNG("noiseline.png") +} diff --git a/example/test.go b/example/test.go index ae425c7..6fd698d 100644 --- a/example/test.go +++ b/example/test.go @@ -1,41 +1,43 @@ package main -import ( - "github.com/fogleman/gg" - "github.com/jdxyw/generativeart" - "github.com/llgcode/draw2d/draw2dimg" - "image" - "math/rand" - "time" -) +import "github.com/fogleman/gg" func main() { - const S = 400 - rand.Seed(time.Now().Unix()) - dest := image.NewRGBA(image.Rect(0, 0, 500, 500)) + //const S = 400 + //rand.Seed(time.Now().Unix()) + //dest := image.NewRGBA(image.Rect(0, 0, 500, 500)) + // + //ctex := gg.NewContextForRGBA(dest) + //ctex.Push() + //ctex.Translate(500/2, 500/2) + //ctex.Rotate(40) + //ctex.SetColor(color.RGBA{0xFF, 0x00, 0x00, 255}) + //for theta :=0.0; theta<361.0; theta+=1.0 { + // x := 100*math.Cos(gg.Radians(theta)) - 100*math.Pow(math.Sin(gg.Radians(theta)), 2) / math.Sqrt(2) + // y := 100*math.Cos(gg.Radians(theta))*math.Sin(gg.Radians(theta)) + // + // x1 := 100*math.Cos(gg.Radians(theta+1)) - 100*math.Pow(math.Sin(gg.Radians(theta+1)), 2) / math.Sqrt(2) + // y1 := 100*math.Cos(gg.Radians(theta+1))*math.Sin(gg.Radians(theta+1)) + // + // ctex.DrawLine(x, y, x1, y1) + // ctex.Stroke() + //} + //ctex.Pop() + // + // + //f, _ := os.Create("test.png") + // + //if err := png.Encode(f, dest); err != nil { + // f.Close() + //} - gc := draw2dimg.NewGraphicContext(dest) - gc.SetStrokeColor(generativeart.Orange) - gc.BeginPath() - gc.MoveTo(100, 100) - gc.QuadCurveTo(150, 50, 200, 100) - //fmt.Println(gc.LastPoint()) - //gc.QuadCurveTo(200, 100, 300, 200) - gc.QuadCurveTo(250, 250, 200, 400) - gc.QuadCurveTo(100, 250, 100, 100) - //gc.QuadCurveTo(200, 400, 100, 10) - //gc.Close() - gc.Stroke() - - ctex := gg.NewContextForRGBA(dest) - ctex.SetColor(generativeart.White) - //ctex.DrawCircle(100, 100, 5) - //ctex.DrawCircle(200, 400, 5) - //ctex.DrawCircle(200, 100, 5) - //ctex.DrawCircle(300, 200, 5) - //ctex.DrawCircle(200, 400, 5) - ctex.Fill() - //gc.FillStroke() - - draw2dimg.SaveToPngFile("test.png", dest) + dc := gg.NewContext(1000, 1000) + dc.DrawCircle(350, 500, 300) + dc.Clip() + dc.DrawCircle(650, 500, 300) + dc.Clip() + dc.DrawRectangle(0, 0, 1000, 1000) + dc.SetRGB(0, 0, 0) + dc.Fill() + dc.SavePNG("out.png") } diff --git a/images/noiseline.png b/images/noiseline.png new file mode 100644 index 0000000..06330f6 Binary files /dev/null and b/images/noiseline.png differ diff --git a/noiseline.go b/noiseline.go new file mode 100644 index 0000000..bdb9e04 --- /dev/null +++ b/noiseline.go @@ -0,0 +1,57 @@ +package generativeart + +import ( + "github.com/fogleman/gg" + "github.com/jdxyw/generativeart/common" + "math" + "math/rand" +) + +type noiseLine struct { + n int +} + +// NewNoiseLine returns a noiseLine object. +func NewNoiseLine(n int) *noiseLine { + return &noiseLine{ + n: n, + } +} + +// Generative draws a noise line image. +func (nl *noiseLine) Generative(c *canva) { + ctex := gg.NewContextForRGBA(c.img) + noise := common.NewPerlinNoise() + + ctex.SetColor(Black) + for i := 0; i < 80; i++ { + x := rand.Float64() * float64(c.width) + y := rand.Float64() * float64(c.height) + + s := rand.Float64() * float64(c.width) / 8 + ctex.SetLineWidth(0.5) + ctex.DrawEllipse(x, y, s, s) + ctex.Stroke() + } + + t := rand.Float64() * 10 + for i := 0; i < nl.n; i++ { + x := common.RandomRangeFloat64(-0.5, 1.5) * float64(c.width) + y := common.RandomRangeFloat64(-0.5, 1.5) * float64(c.height) + cl := c.opts.colorSchema[rand.Intn(len(c.opts.colorSchema))] + cl.A = uint8(c.opts.alpha) + + l := 400 + for j := 0; j < l; j++ { + var ns = 0.0005 + w := math.Sin(math.Pi*float64(j)/float64(l-1)) * 5 + theta := noise.Noise(x*ns, y*ns, t) * 100 + ctex.SetColor(cl) + ctex.DrawCircle(x, y, w) + ctex.Fill() + x += math.Cos(theta) + y += math.Sin(theta) + t += 0.0000003 + } + } +}