forked from fogleman/gg
/
beziers.go
84 lines (75 loc) · 1.3 KB
/
beziers.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"math/rand"
"github.com/fogleman/gg"
)
func random() float64 {
return rand.Float64()*2 - 1
}
func point() (x, y float64) {
return random(), random()
}
func drawCurve(dc *gg.Context) {
dc.SetRGBA(0, 0, 0, 0.1)
dc.FillPreserve()
dc.SetRGB(0, 0, 0)
dc.SetLineWidth(12)
dc.Stroke()
}
func drawPoints(dc *gg.Context) {
dc.SetRGBA(1, 0, 0, 0.5)
dc.SetLineWidth(2)
dc.Stroke()
}
func randomQuadratic(dc *gg.Context) {
x0, y0 := point()
x1, y1 := point()
x2, y2 := point()
dc.MoveTo(x0, y0)
dc.QuadraticTo(x1, y1, x2, y2)
drawCurve(dc)
dc.MoveTo(x0, y0)
dc.LineTo(x1, y1)
dc.LineTo(x2, y2)
drawPoints(dc)
}
func randomCubic(dc *gg.Context) {
x0, y0 := point()
x1, y1 := point()
x2, y2 := point()
x3, y3 := point()
dc.MoveTo(x0, y0)
dc.CubicTo(x1, y1, x2, y2, x3, y3)
drawCurve(dc)
dc.MoveTo(x0, y0)
dc.LineTo(x1, y1)
dc.LineTo(x2, y2)
dc.LineTo(x3, y3)
drawPoints(dc)
}
func main() {
const (
S = 256
W = 8
H = 8
)
dc := gg.NewContext(S*W, S*H)
dc.SetRGB(1, 1, 1)
dc.Clear()
for j := 0; j < H; j++ {
for i := 0; i < W; i++ {
x := float64(i)*S + S/2
y := float64(j)*S + S/2
dc.Push()
dc.Translate(x, y)
dc.Scale(S/2, S/2)
if j%2 == 0 {
randomCubic(dc)
} else {
randomQuadratic(dc)
}
dc.Pop()
}
}
dc.SavePNG("out.png")
}