add ocean fish

This commit is contained in:
Yongwei Xing 2021-03-10 11:58:56 +08:00
parent 35cb465736
commit a1f1c00a3a
5 changed files with 162 additions and 18 deletions

View file

@ -34,6 +34,7 @@ This package is still working in progress. More types would be added. Welcome an
- Circle Grid
- Contour Line
- Noise Line
- Ocean Fish
For these kinds of art, the package provides as many parameters to control the appearance.
@ -245,6 +246,46 @@ func main() {
```
![](images/noiseline.png)
### Dot Line
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(2080, 2080)
c.SetBackground(color.RGBA{230, 230, 230, 255})
c.SetLineWidth(10)
c.SetIterations(4000)
c.SetColorSchema(generativeart.Plasma)
c.FillBackground()
c.Draw(generativeart.NewDotLine(100, 20, 50, false))
c.ToPNG("dotline.png")
}
```
![](images/dotline.png)
### Ocean Fish
```go
func main() {
rand.Seed(time.Now().Unix())
colors := []color.RGBA{
{0xCF, 0x2B, 0x34, 0xFF},
{0xF0, 0x8F, 0x46, 0xFF},
{0xF0, 0xC1, 0x29, 0xFF},
{0x19, 0x6E, 0x94, 0xFF},
{0x35, 0x3A, 0x57, 0xFF},
}
c := generativeart.NewCanva(500, 500)
c.SetColorSchema(colors)
c.Draw(generativeart.NewOceanFish(100, 8))
c.ToPNG("oceanfish.png")
}
```
![](images/oceanfish.png)
### Circle Loop
```go
@ -264,23 +305,6 @@ func main() {
![](images/circleloop.png)
### Dot Line
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(2080, 2080)
c.SetBackground(color.RGBA{230, 230, 230, 255})
c.SetLineWidth(10)
c.SetIterations(4000)
c.SetColorSchema(generativeart.Plasma)
c.FillBackground()
c.Draw(generativeart.NewDotLine(100, 20, 50, false))
c.ToPNG("dotline.png")
}
```
![](images/dotline.png)
### Julia Set
@ -382,3 +406,4 @@ Thanks for the following sites and repos, I got lots of ideas, inspiration, code
- https://openprocessing.org/sketch/1071233
- https://twitter.com/okazz_
- https://openprocessing.org/sketch/738638
- https://openprocessing.org/sketch/1102157

View file

@ -108,3 +108,18 @@ nl := generativeart.NewNoiseLine(1000)
```
![](../images/noiseline.png)
## Ocean Fish
`Ocean Fish` draws a ocean and some fishes in the center.
### parameters
- lineNum: The number of the line used to simulate the ocean wave.
- fishNum: The number of fish.
```go
o := generativeart.NewOceanFish(100, 8)
```
![](../images/oceanfish.png)

View file

@ -0,0 +1,30 @@
package main
import (
"github.com/jdxyw/generativeart"
"image/color"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().Unix())
//colors := []color.RGBA{
// {0x05, 0x1F, 0x34, 0xFF},
// {0x02, 0x74, 0x95, 0xFF},
// {0x01, 0xA9, 0xC1, 0xFF},
// {0xBA, 0xD6, 0xDB, 0xFF},
// {0xF4, 0xF5, 0xF5, 0xFF},
//}
colors := []color.RGBA{
{0xCF, 0x2B, 0x34, 0xFF},
{0xF0, 0x8F, 0x46, 0xFF},
{0xF0, 0xC1, 0x29, 0xFF},
{0x19, 0x6E, 0x94, 0xFF},
{0x35, 0x3A, 0x57, 0xFF},
}
c := generativeart.NewCanva(500, 500)
c.SetColorSchema(colors)
c.Draw(generativeart.NewOceanFish(100, 8))
c.ToPNG("oceanfish.png")
}

BIN
images/oceanfish.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

74
oceanfish.go Normal file
View file

@ -0,0 +1,74 @@
package generativeart
import (
"github.com/fogleman/gg"
"github.com/jdxyw/generativeart/common"
"math"
"math/rand"
)
type oceanFish struct {
lineNum int
fishNum int
}
// NewOceanFish returns a oceanFish object.
func NewOceanFish(lineNum, fishNum int) *oceanFish {
return &oceanFish{
lineNum: lineNum,
fishNum: fishNum,
}
}
// Generative draws a ocean and fish images.
func (o *oceanFish) Generative(c *canva) {
ctex := gg.NewContextForRGBA(c.img)
o.drawlines(ctex, c)
for i := 0; i < o.fishNum; i++ {
ctex.Push()
theta := float64(360*i) / float64(o.fishNum)
r := float64(c.width) / 4.0
ctex.Push()
ctex.Translate(float64(c.width/2)+r*math.Cos(gg.Radians(theta)), float64(c.height/2)+r*math.Sin(gg.Radians(theta)))
ctex.Rotate(gg.Radians(theta + 90))
o.drawfish(ctex, c, 0, 0, float64(c.width)/10)
ctex.Pop()
ctex.Clip()
o.drawlines(ctex, c)
ctex.Pop()
ctex.ClearPath()
ctex.ResetClip()
}
}
func (o *oceanFish) drawlines(ctx *gg.Context, c *canva) {
for i := 0; i < o.lineNum; i++ {
cl := c.opts.colorSchema[rand.Intn(len(c.opts.colorSchema))]
ctx.SetColor(cl)
ctx.SetLineWidth(common.RandomRangeFloat64(3, 20))
y := rand.Float64() * float64(c.height)
ctx.DrawLine(0, y+common.RandomRangeFloat64(-50, 50), float64(c.width), y+common.RandomRangeFloat64(-50, 50))
ctx.Stroke()
}
}
func (o *oceanFish) drawfish(ctex *gg.Context, c *canva, ox, oy, r float64) {
ctex.Push()
ctex.Translate(ox, oy)
ctex.Rotate(gg.Radians(180))
ctex.MoveTo(r*math.Cos(gg.Radians(0))-r*math.Pow(math.Sin(gg.Radians(0)), 2)/math.Sqrt(2),
r*math.Cos(gg.Radians(0))*math.Sin(gg.Radians(0)))
for theta := 1.0; theta < 361.0; theta += 1.0 {
x := r*math.Cos(gg.Radians(theta)) - r*math.Pow(math.Sin(gg.Radians(theta)), 2)/math.Sqrt(2)
y := r * math.Cos(gg.Radians(theta)) * math.Sin(gg.Radians(theta))
ctex.LineTo(x, y)
}
ctex.ClosePath()
ctex.Pop()
}