How to resolve the algorithm Voronoi diagram step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Voronoi diagram step by step in the Wren 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 Wren programming language

Source code in the wren programming language

import "graphics" for Canvas, Color
import "dome" for Window
import "random" for Random

class Game {
    static init() {
        Window.title = "Voronoi diagram"
        var cells = 70
        var size = 700
        Window.resize(size, size)
        Canvas.resize(size, size)
        voronoi(cells, size)
    }

    static update() {}

    static draw(alpha) {}

    static distSq(x1, x2, y1, y2) { (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) }

    static voronoi(cells, size) {
        var r = Random.new()
        var px = List.filled(cells, 0)
        var py = List.filled(cells, 0)
        var cl = List.filled(cells, 0)
        for (i in 0...cells) {
            px[i] = r.int(size)
            py[i] = r.int(size)
            cl[i] = Color.rgb(r.int(256), r.int(256), r.int(256))
        }
        for (x in 0...size) {
            for (y in 0...size) {
                var n = 0
                for (i in 0...cells) {
                     if (distSq(px[i], x, py[i], y) < distSq(px[n], x, py[n], y)) n = i
                }
                Canvas.pset(x, y, cl[n])
            }
        }
        for (i in 0...cells) {
            Canvas.circlefill(px[i], py[i], 2, Color.black)
        }
    }
}

  

You may also check:How to resolve the algorithm Array concatenation step by step in the Maxima programming language
You may also check:How to resolve the algorithm Naming conventions step by step in the Go programming language
You may also check:How to resolve the algorithm Floyd-Warshall algorithm step by step in the Standard ML programming language
You may also check:How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the PL/SQL programming language
You may also check:How to resolve the algorithm Stack step by step in the 6502 Assembly programming language