random circles example
This commit is contained in:
56
cmd/playground/circles/circles.go
Normal file
56
cmd/playground/circles/circles.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"image/png"
|
||||
"os"
|
||||
|
||||
iu "git.nkpl.cc/twocookedfaggots/imageutils"
|
||||
)
|
||||
|
||||
func square(side int) image.Rectangle { return image.Rect(0, 0, side, side) }
|
||||
|
||||
func Circles(b image.Rectangle, radius int) chan iu.Circle {
|
||||
ch := make(chan iu.Circle)
|
||||
go func() {
|
||||
defer close(ch)
|
||||
for {
|
||||
x, y := rand.Int()%b.Dx(), rand.Int()%b.Dy()
|
||||
dxdy := b.Dx()*b.Dy()
|
||||
r := rand.Int() % (dxdy/(dxdy/radius))
|
||||
ch <- iu.Circle{image.Point{x, y}, r}
|
||||
}
|
||||
}()
|
||||
return ch
|
||||
}
|
||||
|
||||
// Converts color.Opaque and color.Transparent to color.Black and color.White respectively.
|
||||
var BlackAndWhite = color.ModelFunc(
|
||||
func(c color.Color) color.Color {
|
||||
if color.AlphaModel.Convert(c) == color.Opaque {
|
||||
return color.Black
|
||||
}
|
||||
return color.White
|
||||
},
|
||||
)
|
||||
|
||||
func main() {
|
||||
canvas := image.NewRGBA(square(1024))
|
||||
circles := Circles(canvas.Bounds(), 1024/16)
|
||||
|
||||
// draw n circles
|
||||
for i := 0; i < 100; i++ {
|
||||
c := <-circles
|
||||
draw.DrawMask(
|
||||
canvas, canvas.Bounds(), &image.Uniform{color.White},
|
||||
image.Point{}, c, image.Point{}, draw.Over,
|
||||
)
|
||||
}
|
||||
err := png.Encode(os.Stdout, canvas)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user