Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created March 1, 2020 21:03
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 peterhellberg/76dfb78bb393a8e5661df08f9464ecc9 to your computer and use it in GitHub Desktop.
Save peterhellberg/76dfb78bb393a8e5661df08f9464ecc9 to your computer and use it in GitHub Desktop.
Twisting column using gui (and gfx) inspired by https://trasevol.dog/2018/01/24/di20/
package main
import (
"image"
"image/draw"
"time"
"github.com/peterhellberg/gfx"
"github.com/peterhellberg/gui"
)
var start = time.Now()
func main() {
gui.Run(loop)
}
func loop() {
win, err := gui.Open(
gui.Title("gui-twisting-column"),
gui.Size(512, 512),
gui.Decorated(false),
gui.Resizable(false),
)
if err != nil {
panic(err)
}
ticker := time.NewTicker(32 * time.Millisecond)
go func() {
for t := range ticker.C {
win.Send(gui.EventUpdate{t})
}
}()
for event := range win.Events() {
switch event := event.(type) {
case gui.EventClose:
ticker.Stop()
win.Close()
case gui.EventKeyboardDown:
if event.Key == "escape" {
ticker.Stop()
win.Close()
}
case gui.EventKeyboardChar:
if event.Char == 'q' {
ticker.Stop()
win.Close()
}
case gui.EventUpdate:
win.Draw(update)
}
}
}
func update(dst draw.Image) image.Rectangle {
m := gfx.NewImage(128, 128)
t := time.Since(start).Seconds() / 3
for y := 0.0; y < 127; y++ {
yy := y / 1024
a := cos(0.2*sin(t*0.1+yy*2)) + 0.5*cos(-0.2*t+yy/2)
for i := 0.0; i <= 0.75; i += 0.25 {
x1 := 64 + 32*cos(a+i+0.25)
x2 := 64 + 32*cos(a+i)
if x1 < x2 {
gfx.DrawLineBresenham(m, gfx.V(x1, y), gfx.V(x2, y), gfx.ColorWhite)
gfx.SetVec(m, gfx.V(x1, y), gfx.ColorBlack)
gfx.SetVec(m, gfx.V(x2, y), gfx.ColorBlack)
}
}
}
s := gfx.NewScaledImage(m, 4)
gfx.DrawSrc(dst, s.Bounds(), s, gfx.ZP)
return dst.Bounds()
}
const tau = gfx.Pi * 2
func cos(x float64) float64 {
return gfx.MathCos(x * tau)
}
func sin(x float64) float64 {
return gfx.MathSin(x * tau)
}
@peterhellberg
Copy link
Author

ezgif-5-f1c9417f7b9e

@peterhellberg
Copy link
Author

Screenshot 2020-03-01 at 22 41 50

Screenshot 2020-03-01 at 22 42 03

Screenshot 2020-03-01 at 22 42 19

Screenshot 2020-03-01 at 22 42 49

Screenshot 2020-03-01 at 22 43 35

@peterhellberg
Copy link
Author

Not drawing the black outline

Screenshot 2020-03-01 at 22 49 21

Screenshot 2020-03-01 at 22 50 16

Screenshot 2020-03-01 at 22 51 31

@peterhellberg
Copy link
Author

package main

import (
	"image"
	"image/draw"
	"time"

	"github.com/peterhellberg/gfx"
	"github.com/peterhellberg/gui"
)

var start = time.Now()

func main() {
	gui.Run(loop)
}

func loop() {
	win, err := gui.Open(
		gui.Title("gui-twisting-column"),
		gui.Size(1024, 1024),
		gui.Decorated(false),
		gui.Resizable(false),
	)
	if err != nil {
		panic(err)
	}

	ticker := time.NewTicker(48 * time.Millisecond)

	go func() {
		for t := range ticker.C {
			win.Send(gui.EventUpdate{t})
		}
	}()

	for event := range win.Events() {
		switch event := event.(type) {
		case gui.EventClose:
			ticker.Stop()
			win.Close()
		case gui.EventKeyboardDown:
			if event.Key == "escape" {
				win.Send(gui.EventClose{})
			}
		case gui.EventKeyboardChar:
			if event.Char == 'q' {
				win.Send(gui.EventClose{})
			}
		case gui.EventUpdate:
			win.Draw(update)
		}
	}
}

func update(dst draw.Image) image.Rectangle {
	m := gfx.NewImage(512, 512)

	t := time.Since(start).Seconds() / 3

	for y := 0.0; y < 512; y++ {
		yy := y / 4096

		a := cos(0.2*sin(t*0.1+yy*2)) + 0.5*cos(-0.2*t+yy/2)

		for i := 0.0; i <= 0.75; i += 0.25 {
			x1 := 256 + 128*cos(a+i+0.25)
			x2 := 256 + 128*cos(a+i)

			if x1 < x2 {
				for x := x1; x < x2; x++ {
					v := gfx.V(x, y)
					n := v.To(gfx.V(x1, y)).Len() / 256
					c := gfx.PaletteEDG8.At(n)

					gfx.SetVec(m, v, c)
				}

				gfx.SetVec(m, gfx.V(x1, y), gfx.ColorBlack)
				gfx.SetVec(m, gfx.V(x2, y), gfx.ColorBlack)
			}
		}
	}

	gfx.DrawSrc(dst, m.Bounds().Add(gfx.Pt(256, 256)), m, gfx.ZP)

	return dst.Bounds()
}

const tau = gfx.Pi * 2

func cos(x float64) float64 {
	return gfx.MathCos(x * tau)
}

func sin(x float64) float64 {
	return gfx.MathSin(x * tau)
}

@peterhellberg
Copy link
Author

ezgif-5-b743586a3371

@peterhellberg
Copy link
Author

ezgif-5-8ccf2db33c2a

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