random circles example

This commit is contained in:
2026-04-04 16:57:10 +03:00
parent 3880b353c1
commit 824a4ede47
13 changed files with 308 additions and 1 deletions

View 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)
}
}