-
Notifications
You must be signed in to change notification settings - Fork 1
/
voronoi.go
112 lines (99 loc) · 2.74 KB
/
voronoi.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// MIT License
//
// Copyright (c) 2020 Pavel Tišnovský
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"math"
"github.com/veandco/go-sdl2/sdl"
)
const (
width = 640
height = 480
)
func putpixel(surface *sdl.Surface, x int32, y int32, r byte, g byte, b byte) {
if x >= 0 && x < surface.W && y >= 0 && y < surface.H {
switch surface.Format.BitsPerPixel {
case 24:
index := x*3 + y*surface.Pitch
pixels := surface.Pixels()
pixels[index] = b
pixels[index+1] = g
pixels[index+2] = r
case 32:
index := x*4 + y*surface.Pitch
pixels := surface.Pixels()
pixels[index] = b
pixels[index+1] = g
pixels[index+2] = r
}
}
}
type Point struct {
X float64
Y float64
}
func main() {
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
panic(err)
}
defer sdl.Quit()
window, err := sdl.CreateWindow("Texture based on Voronoi diagram", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
width, height, sdl.WINDOW_SHOWN)
if err != nil {
panic(err)
}
defer window.Destroy()
primarySurface, err := window.GetSurface()
if err != nil {
panic(err)
}
points := []Point{
{100, 200},
{200, 100},
{500, 100},
{400, 420},
{150, 50},
{250, 400},
{550, 520},
{400, 100},
}
for y := int32(0); y < height; y++ {
for x := int32(0); x < width; x++ {
var color byte = 0
var minDistance float64 = math.MaxFloat64
for i := 0; i < len(points); i++ {
distance := math.Hypot(float64(x)-points[i].X, float64(y)-points[i].Y)
if distance < minDistance {
d := distance
if d > 255 {
d = 255
}
color = byte(d)
minDistance = distance
}
}
putpixel(primarySurface, x, y, color, color, color)
}
}
sdl.Delay(100)
window.UpdateSurface()
sdl.Delay(2000)
}