Skip to content

Instantly share code, notes, and snippets.

@rjp
Created February 14, 2019 22:45
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 rjp/73cb8077e4c17a06f52f19d0ec75135d to your computer and use it in GitHub Desktop.
Save rjp/73cb8077e4c17a06f52f19d0ec75135d to your computer and use it in GitHub Desktop.
Scribble a heart
package main
import (
"fmt"
"math"
"math/rand"
"os"
"time"
"github.com/ajstarks/svgo"
)
var canvas *svg.SVG
func abs(d int) int {
if d < 0 {
return -d
}
return d
}
func jitter(d int, dir int) (float64, float64) {
d = distance(d, rand.Intn(2)+1, dir)
return heart(d)
}
func distance(d int, travel int, dir int) int {
for i := 0; i < travel; i++ {
d = d + dir
}
return d
}
func deg2rad(d int) float64 {
return (float64(d) / 360.0) * 2 * math.Pi
}
func heart(d int) (float64, float64) {
r := deg2rad(d)
x := 16 * math.Pow(math.Sin(r), 3)
y := 13*math.Cos(r) - 5*math.Cos(2*r) - 2*math.Cos(3*r) + 0.1*math.Cos(4*r)
return x, y
}
func line(d0, d1 int) {
fmt.Fprintf(os.Stderr, "Line from %d to %d\n", d0, d1)
px, py := heart(d0)
x, y := heart(d1)
sx := 200 + int(10*px)
sy := 400 - (200 + int(10*py))
ex := 200 + int(10*x)
ey := 400 - (200 + int(10*y))
if os.Getenv("FLIP") != "" {
sx = 400 - sx
ex = 400 - ex
}
canvas.Line(sx, sy, ex, ey, "stroke:green;stroke-width:1")
}
func main() {
px := 99999.0
py := 0.0
rand.Seed(time.Now().Unix())
canvas = svg.New(os.Stdout)
canvas.Start(400, 400)
ds := 10
de := 120
for {
if de-ds < 15 {
break
}
line(ds, de)
ds = distance(ds, rand.Intn(4)+rand.Intn(2)+1, +1)
line(de, ds)
de = distance(de, rand.Intn(4)+rand.Intn(2)+1, -1)
}
ds = 120
de = 350
for {
if de-ds < 15 {
break
}
line(ds, de)
ds = distance(ds, rand.Intn(4)+rand.Intn(2)+1, +1)
if abs(ds-180) < 20 {
ds = distance(ds, rand.Intn(4)+3, +1)
}
line(de, ds)
de = distance(de, rand.Intn(4)+rand.Intn(2)+1, -1)
if de < 270 {
de = distance(de, rand.Intn(4)+3, -1)
}
}
canvas.End()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment