Skip to content

Instantly share code, notes, and snippets.

@dimkouv
Created January 5, 2020 20:58
Show Gist options
  • Save dimkouv/2547bd9d7a51130a35075dc44aac2f54 to your computer and use it in GitHub Desktop.
Save dimkouv/2547bd9d7a51130a35075dc44aac2f54 to your computer and use it in GitHub Desktop.
package main
import (
"image"
"image/color"
"image/png"
"math"
"math/rand"
"os"
"time"
)
func symmetricDraw(img *image.RGBA, offX, offY, x, y int, color color.Color) {
pixels := [4][2]int{
{x, y}, {x, -y}, {-x, y}, {-x, -y},
}
for _, p := range pixels {
img.Set(offX+p[0], offY+p[1], color)
}
}
func drawEllipse(img *image.RGBA, offX, offY, a, b int, color color.Color) {
f64a := float64(a)
f64b := float64(b)
x := 0
y := b
d1 := math.Pow(f64b, 2.0) - math.Pow(f64a, 2.0)*f64b + 0.25*math.Pow(f64a, 2.0)
symmetricDraw(img, offX, offY, x, y, color)
for math.Pow(f64a, 2)*(float64(y)-0.5) > math.Pow(f64b, 2)*float64(x+1) {
if d1 < 0 {
d1 += math.Pow(f64b, 2) * float64(2*x+3)
x++
} else {
d1 += math.Pow(f64b, 2)*float64(2*x+3) + math.Pow(f64a, 2)*float64(-2*y+2)
x++
y--
}
symmetricDraw(img, offX, offY, x, y, color)
}
d2 := math.Pow(f64b, 2.0)*math.Pow(float64(x)+0.5, 2) + math.Pow(f64a, 2)*math.Pow(float64(y-1), 2) - math.Pow(f64a, 2)*math.Pow(f64b, 2)
for y > 0 {
if d2 < 0 {
d2 += math.Pow(f64b, 2)*float64(2*x+2) + math.Pow(f64a, 2)*float64(-2*y+3)
x++
y--
} else {
d2 += math.Pow(f64a, 2) * float64(-2*y+3)
y--
}
symmetricDraw(img, offX, offY, x, y, color)
}
}
func main() {
width := 1000
height := 1000
rand.Seed(time.Now().Unix())
black := color.RGBA{0, 0, 0, 0xff}
upLeft := image.Point{0, 0}
lowRight := image.Point{width, height}
img := image.NewRGBA(image.Rectangle{upLeft, lowRight})
for i := 0; i < width; i++ {
for j := 0; j < height; j++ {
img.Set(i, j, black)
}
}
for i := 0; i < 123; i++ {
a := 10 + rand.Intn(70)
b := 10 + rand.Intn(70)
posX := rand.Intn(1000)
posY := rand.Intn(1000)
drawEllipse(img, posX, posY, a, b, color.RGBA{
uint8(rand.Intn(256)), uint8(rand.Intn(256)), uint8(rand.Intn(256)), 0xff,
})
}
f, _ := os.Create("image.png")
_ = png.Encode(f, img)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment