add silk sky

This commit is contained in:
Yongwei Xing 2021-03-03 17:14:03 +08:00
parent 966671b688
commit eb5b0e05bd
4 changed files with 144 additions and 6 deletions

View file

@ -21,11 +21,11 @@ func (cl *circleLoop) Generative(c *canva) {
r := cl.radius
var theta float64 = 0
for i:=0; i<c.opts.nIters; i++ {
for i := 0; i < c.opts.nIters; i++ {
ctex.Push()
ctex.Translate(float64(c.width/2), float64(c.height/2))
x := cl.radius*math.Cos(gg.Radians(theta))
y := cl.radius*math.Sin(gg.Radians(theta*2))
x := cl.radius * math.Cos(gg.Radians(theta))
y := cl.radius * math.Sin(gg.Radians(theta*2))
ctex.SetLineWidth(c.opts.lineWidth)
ctex.SetColor(c.opts.lineColor)
@ -33,7 +33,7 @@ func (cl *circleLoop) Generative(c *canva) {
ctex.DrawEllipse(x, y, r/2, r/2)
ctex.Stroke()
ctex.Pop()
r+=math.Cos((theta))*math.Sin((theta/2))+math.Sin((theta))*math.Cos((theta/2))
theta += math.Pi/2
r += math.Cos((theta))*math.Sin((theta/2)) + math.Sin((theta))*math.Cos((theta/2))
theta += math.Pi / 2
}
}

View file

@ -0,0 +1,15 @@
package main
import (
"generativeart"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(600, 600, 1, 1)
c.SetAlpha(10)
c.Draw(generativeart.NewSilkSky(15, 5))
c.ToPNG("silksky.png")
}

47
silksky.go Normal file
View file

@ -0,0 +1,47 @@
package generativeart
import (
"github.com/fogleman/gg"
"math/rand"
)
type silkSky struct {
circleNum int
sunRadius float64
}
// NewSilkSky returns a silkSky object.
func NewSilkSky(circleNum int, sunRadius float64) *silkSky {
return &silkSky{
circleNum: circleNum,
sunRadius: sunRadius,
}
}
// Generative draws a silk sky image.
func (s *silkSky) Generative(c *canva) {
ctex := gg.NewContextForRGBA(c.img)
xm := float64(rand.Intn(c.width/5)) + float64(c.width*4/5-c.width/5)
ym := float64(rand.Intn(c.height/5))+ float64(c.height*4/5-c.height/5)
mh := s.circleNum*2 + 2
ms := s.circleNum*2 + 50
mv := 100
for i := 0; i < s.circleNum; i++ {
for j := 0; j < s.circleNum; j++ {
hsv := HSV{
H: s.circleNum + j,
S: i + 50,
V: 70,
}
rgba := hsv.ToRGB(mh, ms, mv)
xn := (float64(i) + 0.5) * float64(c.width) / float64(s.circleNum)
yn := (float64(j) + 0.5) * float64(c.height) / float64(s.circleNum)
ctex.SetRGBA255(int(rgba.R), int(rgba.G), int(rgba.B), c.opts.alpha)
r := Distance(xn, yn, xm, ym)
ctex.DrawEllipse(xn, yn, r-s.sunRadius/2, r-s.sunRadius/2)
ctex.Fill()
}
}
}

View file

@ -1,6 +1,82 @@
package generativeart
import "math"
import (
"image/color"
"math"
)
type HSV struct {
H, S, V int
}
func (hs HSV) ToRGB(mh, ms, mv int) color.RGBA {
if hs.H > mh {
hs.H = mh
}
if hs.S > ms {
hs.S = ms
}
if hs.V > mv {
hs.V = mv
}
h, s, v := float64(hs.H)/float64(mh), float64(hs.S)/float64(ms), float64(hs.V)/float64(mv)
var r, g, b float64
if s == 0 { //HSV from 0 to 1
r = v * 255
g = v * 255
b = v * 255
} else {
h = h * 6
if h == 6 {
h = 0
} //H must be < 1
i := math.Floor(h) //Or ... var_i = floor( var_h )
v1 := v * (1 - s)
v2 := v * (1 - s*(h-i))
v3 := v * (1 - s*(1-(h-i)))
if i == 0 {
r = v
g = v3
b = v1
} else if i == 1 {
r = v2
g = v
b = v1
} else if i == 2 {
r = v1
g = v
b = v3
} else if i == 3 {
r = v1
g = v2
b = v
} else if i == 4 {
r = v3
g = v1
b = v
} else {
r = v
g = v1
b = v2
}
r = r * 255 //RGB results from 0 to 255
g = g * 255
b = b * 255
}
rgb := color.RGBA{
R: uint8(r),
G: uint8(g),
B: uint8(b),
A: 0,
}
return rgb
}
func ConvertCartesianToPixel(x, y, xaixs, yaixs float64, h, w int) (int, int) {
xr, yr := x/xaixs, y/yaixs