Skip to content

Instantly share code, notes, and snippets.

@dnmfarrell
Last active January 20, 2022 02:53
Show Gist options
  • Save dnmfarrell/5c96327ccae733f7052570040f14a6e0 to your computer and use it in GitHub Desktop.
Save dnmfarrell/5c96327ccae733f7052570040f14a6e0 to your computer and use it in GitHub Desktop.
Generate Sierpiński's triangle in golang
// Generate Sierpiński's triangle
// 0********************************************************************2
// ****** ****** ****** ****** ****** ******* ****** ******
// ************ ************ ************ ************
// ********* ********* ******** *********
// ***** ****** ****** ******
// ******************** *******************
// **************** ******** ********
// ***** **** ***** ****
// ********** ***********
// ****** *******
// **** ****
// ***********************************
// ******* ****** ******* ******
// *********** ************
// ********* ********
// ***** ******
// ********************
// ******** *******
// ***** ****
// **********
// *******
// ****
// 1
//
// https://www.linuxjournal.com/content/getting-started-ncurses
package main
import (
c "github.com/rthornton128/goncurses"
"math/rand"
"time"
)
func main() {
defer c.End()
w, err := c.Init()
if err != nil {
println(err)
return
}
c.CBreak(true)
w.Clear()
maxY, maxX := w.MaxYX()
maxY--
maxX--
points := [3]struct{ y, x int }{{0, 0}, {maxY, maxX / 2}, {0, maxX}}
w.MoveAddChar(points[0].y, points[0].x, '0')
w.MoveAddChar(points[1].y, points[1].x, '1')
w.MoveAddChar(points[2].y, points[2].x, '2')
rand.Seed(time.Now().Unix())
yi := rand.Int() % maxY
xi := rand.Int() % maxX
for i := 0; i < 10_000; i++ {
ri := rand.Int() % 3
yi = (yi + points[ri].y) / 2
xi = (xi + points[ri].x) / 2
if xi == 0 { // don't clobber our starting 0
continue
}
w.MoveAddChar(yi, xi, '*')
w.Refresh()
time.Sleep(5 * time.Microsecond)
}
w.MovePrint(maxY, 0, "Press any key to quit")
w.Refresh()
w.GetChar()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment