File: bubtrail.go

package info (click to toggle)
golang-github-ajstarks-svgo 2012-01-27-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,720 kB
  • sloc: xml: 80; makefile: 31; sh: 29
file content (64 lines) | stat: -rw-r--r-- 1,287 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main

import (
	"flag"
	"fmt"
	"math/rand"
	"os"
	"time"
    
	"github.com/ajstarks/svgo"
)

var (
	width   = 1200
	height  = 600
	opacity = 0.5
	size    = 40
	niter   = 200
	canvas  = svg.New(os.Stdout)
)

func init() {
	flag.IntVar(&size, "s", 40, "bubble size")
	flag.IntVar(&niter, "n", 200, "number of iterations")
	flag.Float64Var(&opacity, "o", 0.5, "opacity")
	flag.Parse()
	rand.Seed(int64(time.Now().Nanosecond()) % 1e9)
}

func background(v int) { canvas.Rect(0, 0, width, height, canvas.RGB(v, v, v)) }

func random(howsmall, howbig int) int {
	if howsmall >= howbig {
		return howsmall
	}
	return rand.Intn(howbig-howsmall) + howsmall
}

func main() {
	var style string

	canvas.Start(width, height)
	canvas.Title("Bubble Trail")
	background(200)
	canvas.Gstyle(fmt.Sprintf("fill-opacity:%.2f;stroke:none", opacity))
	for i := 0; i < niter; i++ {
		x := random(0, width)
		y := random(height/3, (height*2)/3)
		r := random(0, 10000)
		switch {
		case r >= 0 && r <= 2500:
			style = "fill:rgb(255,255,255)"
		case r > 2500 && r <= 5000:
			style = "fill:rgb(127,0,0)"
		case r > 5000 && r <= 7500:
			style = "fill:rgb(127,127,127)"
		case r > 7500 && r <= 10000:
			style = "fill:rgb(0,0,0)"
		}
		canvas.Circle(x, y, size, style)
	}
	canvas.Gend()
	canvas.End()
}