-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (81 loc) · 2.1 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"image"
"image/color"
"image/png"
"math"
"math/rand"
"os"
"time"
)
type Point struct {
x int
y int
surroundingColour color.RGBA
}
var (
pointsAmount = 10
height = 1920
width = 1080
minOffset = 10
RED = color.RGBA{255, 0, 0, 255}
MAGENTA = color.RGBA{255, 0, 255, 255}
GREEN = color.RGBA{0, 255, 0, 255}
CYAN = color.RGBA{0, 255, 255, 255}
BLUE = color.RGBA{0, 0, 255, 255}
YELLOW = color.RGBA{255, 255, 0, 255}
WHITE = color.RGBA{255, 255, 255, 255}
VIOLET = color.RGBA{51, 0, 154, 255}
ORANGE = color.RGBA{255, 175, 0, 255}
BROWN = color.RGBA{70, 63, 23, 255}
EMPTY_PIXEL = color.RGBA{}
COLOURS []color.RGBA = []color.RGBA{RED, MAGENTA, GREEN, CYAN, BLUE, YELLOW, WHITE, VIOLET, ORANGE, BROWN}
)
func init() {
rand.Seed(time.Now().Unix())
}
func getShortestDistanceColour(vectors []Point, point Point) color.RGBA {
distances := make([]float64, pointsAmount)
for i := 0; i < len(vectors); i++ {
distances[i] = math.Sqrt(
math.Pow(float64(vectors[i].x) - float64(point.x), 2) +
math.Pow(float64(vectors[i].y) - float64(point.y), 2),
)
}
min := distances[0]
minIndex := 0
for x := 1; x < len(distances); x++ {
if distances[x] < min {
min = distances[x]
minIndex = x
}
}
return vectors[minIndex].surroundingColour
}
func paintblueprintImage(img *image.RGBA, vectors []Point) {
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
if img.At(x, y) == EMPTY_PIXEL {
img.Set(x, y, getShortestDistanceColour(vectors, Point{y: x, x: y}))
}
}
}
}
func generateRandomPoints(points *image.RGBA) []Point {
var vectors []Point
for x := 0; x < pointsAmount; x++ {
h := rand.Intn(height - 2 * minOffset) + minOffset
w := rand.Intn(width - 2 * minOffset) + minOffset
points.Set(h, w, color.Black)
vectors = append(vectors, Point{surroundingColour: COLOURS[x], x: w, y: h})
}
return vectors
}
func main() {
img := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{height, width}})
vectors := generateRandomPoints(img)
paintblueprintImage(img, vectors)
f, _ := os.Create("voronoi.png")
defer f.Close()
png.Encode(f, img)
}