Skip to content

Instantly share code, notes, and snippets.

@pseudoClone
Last active October 2, 2023 14:46
Show Gist options
  • Save pseudoClone/b14c8ee06cac9c2d17ff02aaeb9434bc to your computer and use it in GitHub Desktop.
Save pseudoClone/b14c8ee06cac9c2d17ff02aaeb9434bc to your computer and use it in GitHub Desktop.
Lissajous figures: 2nd Order from gopl
package main
import (
"os"
"image"
"image/color"
"image/gif"
"io"
"math"
"math/rand" //Todo: PRNG
)
//Global variables donot use :=
var colPalette = []color.Color{color.White, color.Black}
const(
whiteVal = 0 // = because constants cannot use :=
blackVal = 1
)
func main() {
lissajousWaveGen(os.Stdout)
}
func lissajousWaveGen(out io.Writer) {
const(
TF_Order = 2
angularRes = 0.001
frameSize = 100 // +-100 overall
animFrames = 64
delay = 8 // 80ms
)
frequency := rand.Float64() * 3.0 //Go's PRNG
animation := gif.GIF{LoopCount:animFrames} //LoopCount is inside GIF type struct
// LoopCount 0 = Looping unconditional
// Loop count -1 = Show frame once, then done!
phaseDiff := 0.00
for i := 0; i < animFrames; i++ {
rectangleFrame := image.Rect(0,0, 2*frameSize + 1, 2*frameSize + 1)
img := image.NewPaletted(rectangleFrame,colPalette)
for phi := 0.0; phi < (TF_Order * 2 * math.Pi); phi += angularRes {
x := math.Sin(phi)
y := math.Sin(phi * frequency + phaseDiff)
img.SetColorIndex(frameSize + int(x * frameSize + 0.5), frameSize + int(y * frameSize + 0.5), blackVal)
// Dont use float for mapping, easier computation but at a cost of accuracy. Hence the conversion!!
// Last parameter = baseIndex
}
phaseDiff += 0.01
animation.Delay = append(animation.Delay, delay)
animation.Image = append(animation.Image, img)
}
gif.EncodeAll(out, &animation) //Not acquanited with references yet
}
@pseudoClone
Copy link
Author

There appears a small constriction in one of the constructive points between two waves. Maybe due to use of integer math.
Also, not sure if 2nd order wave is to start from the top of the bottom. I don't AKS's notes right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment