Jump to content

Voronoi diagram: Difference between revisions

→‎{{header|Proecessing}}: adding Java and Python modes
(Added Wren)
(→‎{{header|Proecessing}}: adding Java and Python modes)
Line 1,710:
end procedure
main()</lang>
 
=={{header|Processing}}==
{{trans|Python}}
<lang Java>void setup() {
size(500, 500);
generateVoronoiDiagram(width, height, 25);
saveFrame("VoronoiDiagram.png");
}
 
void generateVoronoiDiagram(int w, int h, int num_cells) {
int nx[] = new int[num_cells];
int ny[] = new int[num_cells];
int nr[] = new int[num_cells];
int ng[] = new int[num_cells];
int nb[] = new int[num_cells];
for (int n=0; n < num_cells; n++) {
nx[n]=int(random(w));
ny[n]=int(random(h));
nr[n]=int(random(256));
ng[n]=int(random(256));
nb[n]=int(random(256));
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
float dmin = dist(0, 0, w - 1, h - 1);
int j = -1;
for (int i=0; i < num_cells; i++) {
float d = dist(0, 0, nx[i] - x, ny[i] - y);
if (d < dmin) {
dmin = d;
j = i;
}
}
set(x, y, color(nr[j], ng[j], nb[j]));
}
}
}
}
</lang>
 
==={{header|Processing}}===
{{trans|Python}}
<lang Python>def setup():
size(500, 500)
generate_voronoi_diagram(width, height, 25)
saveFrame("VoronoiDiagram.png")
 
def generate_voronoi_diagram(w, h, num_cells):
nx, ny, nr, ng, nb = [], [], [], [], []
for i in range(num_cells):
nx.append(int(random(w)))
ny.append(int(random(h)))
nr.append(int(random(256)))
ng.append(int(random(256)))
nb.append(int(random(256)))
for y in range(h):
for x in range(w):
dmin = dist(0, 0, w - 1, h - 1)
j = -1
for i in range(num_cells):
d = dist(0, 0, nx[i] - x, ny[i] - y)
if d < dmin:
dmin = d
j = i
set(x, y, color(nr[j], ng[j], nb[j]))
</lang>
 
=={{header|Prolog}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.