-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoints.go
74 lines (64 loc) · 1.59 KB
/
points.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
package kluge
import (
"image"
"math"
"math/rand"
"github.com/fogleman/gg"
)
// Point represents a point, speciying the coordinate
// and the RGB color at the same position
type Point struct {
X int
Y int
R uint8
G uint8
B uint8
}
func newPoint(x, y int, r, g, b uint8) *Point {
return &Point{
X: x,
Y: y,
R: r,
G: g,
B: b,
}
}
func randFloats(min, max float64) float64 {
return min + rand.Float64()*(max-min)
}
// GeneratePoints run through the image creating random points
func GeneratePoints(img image.Image, threshold float64) []*Point {
points := make([]*Point, 0)
for x := 0; x < img.Bounds().Max.X; x++ {
for y := 0; y < img.Bounds().Max.Y; y++ {
r, g, b, _ := img.At(x, y).RGBA()
if uint8(r) <= 250 && uint8(g) <= 250 && uint8(b) <= 250 {
val := randFloats(0, 100)
if val < threshold {
points = append(points, newPoint(x, y, uint8(r), uint8(g), uint8(b)))
}
}
}
}
return points
}
// BuildLines create a new image and draw lines between points with
// a minimum distance
func BuildLines(w, h int, points []*Point, minDist float64) *gg.Context {
// create empty context
ctx := gg.NewContext(w, h)
// iterate over points and draw a line if they
// have a mininum distance
for i, p := range points {
for j := 0; j < i; j++ {
if math.Hypot(float64(p.X-points[j].X), float64(p.Y-points[j].Y)) < minDist {
ctx.MoveTo(float64(p.X), float64(p.Y))
ctx.LineTo(float64(points[j].X), float64(points[j].Y))
// ctx.SetLineWidth(0.65)
ctx.SetRGB255(int(points[j].R), int(points[j].G), int(points[j].B))
ctx.Stroke()
}
}
}
return ctx
}