Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Last active March 14, 2019 09:55
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/db70d52856ea78bb995b855c8a4e1d9b to your computer and use it in GitHub Desktop.
Save peterhellberg/db70d52856ea78bb995b855c8a4e1d9b to your computer and use it in GitHub Desktop.
(Simplex) Noise loops using gfx
package main
import "github.com/peterhellberg/gfx"
const (
seed = 123
scale = 256
radius = 420
count = 64
)
func main() {
var (
p = gfx.PaletteEN4
m = gfx.IM.Moved(gfx.V(512, 512))
sn = gfx.NewSimplexNoise(seed)
dst = gfx.NewImage(1024, 1024, p.Color(3))
)
s := newState(scale, radius, count, sn, m)
gfx.EachImageVec(dst, gfx.V(1, 1), func(u gfx.Vec) {
gfx.SetVec(dst, u, p.At(s.noise(u)))
})
for _, u := range s.points {
n := s.noise(u)
gfx.DrawCircle(dst, u, n*32, 0, p.At(n))
}
gfx.SavePNG("gfx-noise-loops.png", dst)
}
func newState(scale, radius float64, count int, sn *gfx.SimplexNoise, matrix gfx.Matrix) *state {
s := &state{
SimplexNoise: sn,
scale: scale,
matrix: matrix,
theta: (gfx.Pi * 2) / float64(count),
}
for n := 0; n < count; n++ {
angle := s.theta * float64(n)
s.points = append(s.points, matrix.Project(gfx.V(
radius*gfx.MathCos(angle),
radius*gfx.MathSin(angle),
)))
}
return s
}
type state struct {
*gfx.SimplexNoise
scale float64
theta float64
matrix gfx.Matrix
points gfx.Polygon
}
func (s *state) noise(u gfx.Vec) float64 {
return (s.Noise2D(u.X/s.scale, u.Y/s.scale) + 1) / 2
}
@peterhellberg
Copy link
Author

gfx-noise-loops

@peterhellberg
Copy link
Author

gfx-noise-loops

@peterhellberg
Copy link
Author

Using gfx.PaletteEDG16

gfx-noise-loops

@peterhellberg
Copy link
Author

peterhellberg commented Mar 14, 2019

Using gfx.PaletteSplendor128 and point count set to 512

gfx-noise-loops-Splendor128

@peterhellberg
Copy link
Author

Animation

noise loop animation

package main

import "github.com/peterhellberg/gfx"

const (
	seed   = 123
	scale  = 256
	radius = 420
	count  = 512
)

func main() {
	var (
		p  = gfx.PaletteEN4
		m  = gfx.IM.Moved(gfx.V(512, 512))
		sn = gfx.NewSimplexNoise(seed)
	)

	s := newState(scale, radius, count, sn, m)

	for i, u := range s.points {
		dst := gfx.NewImage(1024, 1024, p.Color(3))

		n := s.noise(u)

		gfx.DrawCircle(dst, u, n*64, 0, p.At(n))

		gfx.SavePNG(gfx.Sprintf("gfx-noise-loops-animation-%03d.png", i), dst)
	}
}

func newState(scale, radius float64, count int, sn *gfx.SimplexNoise, matrix gfx.Matrix) *state {
	s := &state{
		SimplexNoise: sn,
		scale:        scale,
		matrix:       matrix,
		theta:        (gfx.Pi * 2) / float64(count),
	}

	for n := 0; n < count; n++ {
		angle := s.theta * float64(n)

		s.points = append(s.points, matrix.Project(gfx.V(
			radius*gfx.MathCos(angle),
			radius*gfx.MathSin(angle),
		)))
	}

	return s
}

type state struct {
	*gfx.SimplexNoise
	scale  float64
	theta  float64
	matrix gfx.Matrix
	points gfx.Polygon
}

func (s *state) noise(u gfx.Vec) float64 {
	return (s.Noise2D(u.X/s.scale, u.Y/s.scale) + 1) / 2
}

@peterhellberg
Copy link
Author

peterhellberg commented Mar 14, 2019

Animation with noise field background

package main

import "github.com/peterhellberg/gfx"

const (
	seed   = 123
	scale  = 256
	radius = 320
	count  = 256
)

func main() {
	var (
		p  = gfx.PaletteEN4
		m  = gfx.IM.Moved(gfx.V(512, 512))
		sn = gfx.NewSimplexNoise(seed)
	)

	s := newState(scale, radius, count, sn, m)

	for i, u := range s.points {
		dst := gfx.NewImage(1024, 1024)

		gfx.EachImageVec(dst, gfx.V(1, 1), func(u gfx.Vec) {
			gfx.SetVec(dst, u, p.At(s.noise(u)))
		})

		n := s.noise(u)

		gfx.DrawCircle(dst, u, n*128, 0, p.At(n))

		gfx.SavePNG(gfx.Sprintf("gfx-noise-loops-animation-%03d.png", i), dst)
	}
}

func newState(scale, radius float64, count int, sn *gfx.SimplexNoise, matrix gfx.Matrix) *state {
	s := &state{
		SimplexNoise: sn,
		scale:        scale,
		matrix:       matrix,
		theta:        (gfx.Pi * 2) / float64(count),
	}

	for n := 0; n < count; n++ {
		angle := s.theta * float64(n)

		s.points = append(s.points, matrix.Project(gfx.V(
			radius*gfx.MathCos(angle),
			radius*gfx.MathSin(angle),
		)))
	}

	return s
}

type state struct {
	*gfx.SimplexNoise
	scale  float64
	theta  float64
	matrix gfx.Matrix
	points gfx.Polygon
}

func (s *state) noise(u gfx.Vec) float64 {
	return (s.Noise2D(u.X/s.scale, u.Y/s.scale) + 1) / 2
}

@peterhellberg
Copy link
Author

peterhellberg commented Mar 14, 2019

Without moving the position of the drawn circle

circle-animation

package main

import "github.com/peterhellberg/gfx"

const (
	size   = 1024
	seed   = 123
	scale  = 256
	radius = 256
	count  = 256
)

func main() {
	var (
		p  = gfx.PaletteEN4
		m  = gfx.IM.Moved(gfx.V(512, 512))
		sn = gfx.NewSimplexNoise(seed)
	)

	s := newState(scale, radius, count, sn, m)

	for i, u := range s.points {
		dst := gfx.NewImage(size, size, p.Color(3))

		n := s.noise(u)

		gfx.DrawCircle(dst, gfx.V(512, 512), n*256, 0, p.At(n))

		gfx.SavePNG(gfx.Sprintf("gfx-noise-loops-animation-2-%03d.png", i), dst)
	}
}

func newState(scale, radius float64, count int, sn *gfx.SimplexNoise, matrix gfx.Matrix) *state {
	s := &state{
		SimplexNoise: sn,
		scale:        scale,
		matrix:       matrix,
		theta:        (gfx.Pi * 2) / float64(count),
	}

	for n := 0; n < count; n++ {
		angle := s.theta * float64(n)

		s.points = append(s.points, matrix.Project(gfx.V(
			radius*gfx.MathCos(angle),
			radius*gfx.MathSin(angle),
		)))
	}

	return s
}

type state struct {
	*gfx.SimplexNoise
	scale  float64
	theta  float64
	matrix gfx.Matrix
	points gfx.Polygon
}

func (s *state) noise(u gfx.Vec) float64 {
	return (s.Noise2D(u.X/s.scale, u.Y/s.scale) + 1) / 2
}

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