-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVornoi.html
71 lines (69 loc) · 2.29 KB
/
Vornoi.html
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
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.dom.js"></script>
<style>
*{
margin:0;
padding:0;
}
body{
display:flex;
align-items:center;
justify-content:center;
min-height:100vh;
flex-direction: column;
}
</style>
</head>
<body>
<script>
let n=50;
let rate = 100;
let centers = []
function setup(){
createCanvas(window.innerWidth, window.innerHeight);
for(let i = 0; i < n; i++){
centers.push(new Center(random() * width, random() * height));
}
background(255);
strokeWeight(5);
centers.forEach((center) => {
point(center.x, center.y);
});
}
function draw(){
for(let i = 0; i < rate; i++){
let x = random() * width;
let y = random() * height;
let min = Infinity;
let min_idx = -1;
for(let j = 0; j < centers.length; j++) {
let val = centers[j].calcDist(x, y);
if(val < min){
min = val;
min_idx = j;
}
}
centers[min_idx].show(x, y);
}
}
class Center{
constructor(x, y){
this.x = x;
this.y = y;
this.c = [int(random()*256), int(random()*256), int(random()*256)];
console.log(this.c);
}
show(x, y){
stroke(this.c[0], this.c[1], this.c[2]);
point(x, y);
}
calcDist(x, y){
return ((this.x - x) ** 2 + (this.y - y) ** 2);
}
}
</script>
</body>
</html>