Skip to content

Instantly share code, notes, and snippets.

@pcardune
Created July 1, 2018 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcardune/e945ad8411fa86781d35bcf2d6e533fe to your computer and use it in GitHub Desktop.
Save pcardune/e945ad8411fa86781d35bcf2d6e533fe to your computer and use it in GitHub Desktop.
Clifford Attractor Animation
// after running this program you can run
// ffmpeg -framerate 30 -i out/%06d.jpg -c:v libx264 -r 30 -pix_fmt yuv420p out2.mp4
package main
import (
"fmt"
"image"
"image/color"
"image/jpeg"
"math"
"os"
)
func makeFrame(a float64, b float64, c float64, d float64, i int) {
width := 2000.0
height := 2000.0
img := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
x := 0.0
y := 0.0
for i := 0; i < 1000000; i++ {
x2 := math.Sin(a*y) + c*math.Cos(a*x)
y2 := math.Sin(b*x) + d*math.Cos(b*y)
x = x2
y = y2
img.Set(int(x*width/4+width/2), int(y*width/4+width/2), color.RGBA{0, 255, 0, 255})
}
f, _ := os.OpenFile(fmt.Sprintf("out/%06d.jpg", i), os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
options := jpeg.Options{
Quality: 95,
}
jpeg.Encode(f, img, &options)
}
func interpolate(n1 float64, n2 float64, i int, n int) float64 {
return n1 + (n2-n1)/float64(n)*float64(i)
}
func main() {
a1 := -1.4
a2 := -1.2
b1 := 1.6
b2 := 1.9
c1 := 1.0
c2 := 1.4
d1 := 0.7
d2 := 0.9
maxGoroutines := 8
guard := make(chan struct{}, maxGoroutines)
numFrames := 1000
for i := 0; i < numFrames; i++ {
guard <- struct{}{}
go func(i int) {
fmt.Println("Frame", i, "start")
makeFrame(
interpolate(a1, a2, i, numFrames),
interpolate(b1, b2, i, numFrames),
interpolate(c1, c2, i, numFrames),
interpolate(d1, d2, i, numFrames),
i,
)
fmt.Println("Frame", i, "done")
<-guard
}(i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment