How to resolve the algorithm Voronoi diagram step by step in the OCaml programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Voronoi diagram step by step in the OCaml programming language
Table of Contents
Problem Statement
A Voronoi diagram is a diagram consisting of a number of sites. Each Voronoi site s also has a Voronoi cell consisting of all points closest to s.
Demonstrate how to generate and display a Voroni diagram.
See algo K-means++ clustering.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Voronoi diagram step by step in the OCaml programming language
Source code in the ocaml programming language
let n_sites = 220
let size_x = 640
let size_y = 480
let sq2 ~x ~y =
(x * x + y * y)
let rand_int_range a b =
a + Random.int (b - a + 1)
let nearest_site ~site ~x ~y =
let ret = ref 0 in
let dist = ref 0 in
Array.iteri (fun k (sx, sy) ->
let d = sq2 (x - sx) (y - sy) in
if k = 0 || d < !dist then begin
dist := d;
ret := k;
end
) site;
!ret
let gen_map ~site ~rgb =
let nearest = Array.make (size_x * size_y) 0 in
let buf = Bytes.create (3 * size_x * size_y) in
for y = 0 to pred size_y do
for x = 0 to pred size_x do
nearest.(y * size_x + x) <-
nearest_site ~site ~x ~y;
done;
done;
for i = 0 to pred (size_y * size_x) do
let j = i * 3 in
let r, g, b = rgb.(nearest.(i)) in
Bytes.set buf (j+0) (char_of_int r);
Bytes.set buf (j+1) (char_of_int g);
Bytes.set buf (j+2) (char_of_int b);
done;
Printf.printf "P6\n%d %d\n255\n" size_x size_y;
print_bytes buf;
;;
let () =
Random.self_init ();
let site =
Array.init n_sites (fun i ->
(Random.int size_x,
Random.int size_y))
in
let rgb =
Array.init n_sites (fun i ->
(rand_int_range 160 255,
rand_int_range 40 160,
rand_int_range 20 140))
in
gen_map ~site ~rgb
You may also check:How to resolve the algorithm Hello world/Text step by step in the KQL programming language
You may also check:How to resolve the algorithm Magic squares of odd order step by step in the VBScript programming language
You may also check:How to resolve the algorithm Word frequency step by step in the AWK programming language
You may also check:How to resolve the algorithm Variadic function step by step in the Raku programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the MiniScript programming language