-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
polygon.go
58 lines (52 loc) · 1.19 KB
/
polygon.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
package triangle
import (
"image"
"math/rand"
"time"
)
// GetPoints retrieves the triangle points after the Sobel threshold has been applied.
func (p *Processor) GetPoints(img *image.NRGBA, threshold, maxPoints int) []Point {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
width, height := img.Bounds().Dx(), img.Bounds().Dy()
var (
sum, total uint8
x, y, sx, sy int
row, col, step int
points []Point
dpoints []Point
)
for y = 0; y < height; y++ {
for x = 0; x < width; x++ {
sum, total = 0, 0
for row = -1; row <= 1; row++ {
sy = y + row
step = sy * width
if sy >= 0 && sy < height {
for col = -1; col <= 1; col++ {
sx = x + col
if sx >= 0 && sx < width {
sum += img.Pix[(sx+step)<<2]
total++
}
}
}
}
if total > 0 {
sum /= total
}
if sum > uint8(threshold) {
points = append(points, Point{X: float64(x), Y: float64(y)})
}
}
}
ilen := len(points)
limit := int(float64(ilen) * p.PointRate)
if limit > maxPoints {
limit = maxPoints
}
for i := 0; i < limit && i < ilen; i++ {
j := int(float64(ilen) * r.Float64())
dpoints = append(dpoints, points[j])
}
return dpoints
}