Voronoi diagram: Difference between revisions
Content added Content deleted
(added ReScript) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 14: | Line 14: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Requires [https://www.autohotkey.com/boards/viewtopic.php?f=6&t=6517 GDIP Library] |
Requires [https://www.autohotkey.com/boards/viewtopic.php?f=6&t=6517 GDIP Library] |
||
< |
<syntaxhighlight lang="autohotkey">;------------------------------------------------------------------------ |
||
Gui, 1: +E0x20 +Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs |
Gui, 1: +E0x20 +Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs |
||
Gui, 1: Show, NA |
Gui, 1: Show, NA |
||
Line 91: | Line 91: | ||
ExitApp |
ExitApp |
||
Return |
Return |
||
;------------------------------------------------------------------------</ |
;------------------------------------------------------------------------</syntaxhighlight> |
||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="basic256">global ancho, alto |
||
ancho = 500 : alto = 500 |
ancho = 500 : alto = 500 |
||
Line 137: | Line 137: | ||
refresh |
refresh |
||
imgsave "Voronoi_diagram.jpg", "jpg" |
imgsave "Voronoi_diagram.jpg", "jpg" |
||
end</ |
end</syntaxhighlight> |
||
Line 145: | Line 145: | ||
Image is in PNM P6, written to stdout. |
Image is in PNM P6, written to stdout. |
||
Run as <code>a.out > stuff.pnm</code>. |
Run as <code>a.out > stuff.pnm</code>. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 263: | Line 263: | ||
gen_map(); |
gen_map(); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
[[File:voronoi_cpp.png|256px]] |
[[File:voronoi_cpp.png|256px]] |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <windows.h> |
#include <windows.h> |
||
#include <vector> |
#include <vector> |
||
Line 459: | Line 459: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="d">import std.random, std.algorithm, std.range, bitmap; |
||
struct Point { uint x, y; } |
struct Point { uint x, y; } |
||
Line 503: | Line 503: | ||
.generateVoronoi(imageWidth, imageHeight) |
.generateVoronoi(imageWidth, imageHeight) |
||
.savePPM6("voronoi.ppm"); |
.savePPM6("voronoi.ppm"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang="delphi"> |
||
uses System.Generics.Collections; |
uses System.Generics.Collections; |
||
Line 614: | Line 614: | ||
Canvas.Draw(0,0, img); |
Canvas.Draw(0,0, img); |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="freebasic">Dim Shared As Integer ancho = 500, alto = 500 |
||
Screenres ancho, alto, 8 |
Screenres ancho, alto, 8 |
||
Cls |
Cls |
||
Line 654: | Line 654: | ||
Generar_Diagrama_Voronoi(ancho, alto, 25) |
Generar_Diagrama_Voronoi(ancho, alto, 25) |
||
Bsave "Voronoi_diadram.bmp",0 |
Bsave "Voronoi_diadram.bmp",0 |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
[[file:GoVoronoi.png|thumb|right|Output png]] |
[[file:GoVoronoi.png|thumb|right|Output png]] |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 741: | Line 741: | ||
fmt.Println(err) |
fmt.Println(err) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Uses the repa and repa-io libraries. |
Uses the repa and repa-io libraries. |
||
< |
<syntaxhighlight lang="haskell"> |
||
-- Compile with: ghc -O2 -fllvm -fforce-recomp -threaded --make |
-- Compile with: ghc -O2 -fllvm -fforce-recomp -threaded --make |
||
{-# LANGUAGE BangPatterns #-} |
{-# LANGUAGE BangPatterns #-} |
||
Line 807: | Line 807: | ||
writeImageToBMP "out.bmp" voro |
writeImageToBMP "out.bmp" voro |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
Line 813: | Line 813: | ||
[[File:Voronoi-normal_unicon.PNG|right]] |
[[File:Voronoi-normal_unicon.PNG|right]] |
||
[[File:Voronoi-taxi_unicon.PNG|right]] |
[[File:Voronoi-taxi_unicon.PNG|right]] |
||
< |
<syntaxhighlight lang="icon">link graphics,printf,strings |
||
record site(x,y,colour) # site data position and colour |
record site(x,y,colour) # site data position and colour |
||
Line 883: | Line 883: | ||
every site := !siteL do # mark sites |
every site := !siteL do # mark sites |
||
DrawCircle(site.x,site.y,1) |
DrawCircle(site.x,site.y,1) |
||
end</ |
end</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
Line 895: | Line 895: | ||
A straightforward solution: generate random points and for each pixel find the index of the least distance. Note that the square root is avoided to improve performance. |
A straightforward solution: generate random points and for each pixel find the index of the least distance. Note that the square root is avoided to improve performance. |
||
< |
<syntaxhighlight lang="j">NB. (number of points) voronoi (shape) |
||
NB. Generates an array of indices of the nearest point |
NB. Generates an array of indices of the nearest point |
||
voronoi =: 4 :0 |
voronoi =: 4 :0 |
||
Line 903: | Line 903: | ||
load'viewmat' |
load'viewmat' |
||
viewmat 25 voronoi 500 500</ |
viewmat 25 voronoi 500 500</syntaxhighlight> |
||
Another solution generates Voronoi cells from Delaunay triangulation. The page [[Voronoi diagram/J/Delaunay triangulation]] also contains a convex hull algorithm. This is a vector based approach instead of a pixel based approach and is about twice as fast for this task's example. |
Another solution generates Voronoi cells from Delaunay triangulation. The page [[Voronoi diagram/J/Delaunay triangulation]] also contains a convex hull algorithm. This is a vector based approach instead of a pixel based approach and is about twice as fast for this task's example. |
||
Line 911: | Line 911: | ||
This a direct reformulation of the explicit version. |
This a direct reformulation of the explicit version. |
||
< |
<syntaxhighlight lang="j">Voronoi=. ,"0/&i./@:] (i. <./)@:(+/@:*:@:-"1)"1 _ ] ?@$~ 2 ,~ [ |
||
viewmat 25 Voronoi 500 500 [ load'viewmat'</ |
viewmat 25 Voronoi 500 500 [ load'viewmat'</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{libheader|Swing}} {{libheader|AWT}} |
{{libheader|Swing}} {{libheader|AWT}} |
||
< |
<syntaxhighlight lang="java">import java.awt.Color; |
||
import java.awt.Graphics; |
import java.awt.Graphics; |
||
import java.awt.Graphics2D; |
import java.awt.Graphics2D; |
||
Line 993: | Line 993: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 1,019: | Line 1,019: | ||
[[File:VDjs310.png|200px|right|thumb|Output VDjs310.png]] |
[[File:VDjs310.png|200px|right|thumb|Output VDjs310.png]] |
||
< |
<syntaxhighlight lang="javascript"><!-- VoronoiD.html --> |
||
<html> |
<html> |
||
<head><title>Voronoi diagram</title> |
<head><title>Voronoi diagram</title> |
||
Line 1,081: | Line 1,081: | ||
</body> |
</body> |
||
</html> |
</html> |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 1,090: | Line 1,090: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
First version generates an image with random colors as centroids for the voronoi tesselation: |
First version generates an image with random colors as centroids for the voronoi tesselation: |
||
< |
<syntaxhighlight lang="julia"> |
||
using Images |
using Images |
||
function voronoi(w, h, n_centroids) |
function voronoi(w, h, n_centroids) |
||
Line 1,104: | Line 1,104: | ||
end |
end |
||
img = voronoi(800, 600, 200) |
img = voronoi(800, 600, 200) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Second version takes an image as an input, samples random centroids for the voronoi cells, and asignes every pixel within that cell the color of the centroid: |
Second version takes an image as an input, samples random centroids for the voronoi cells, and asignes every pixel within that cell the color of the centroid: |
||
< |
<syntaxhighlight lang="julia"> |
||
using TestImages, Images |
using TestImages, Images |
||
function voronoi_img!(img, n_centroids) |
function voronoi_img!(img, n_centroids) |
||
Line 1,130: | Line 1,130: | ||
img = testimage("mandrill") |
img = testimage("mandrill") |
||
voronoi_img!(img, 300) |
voronoi_img!(img, 300) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.3 |
||
import java.awt.Color |
import java.awt.Color |
||
Line 1,184: | Line 1,184: | ||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
Voronoi(70, 700).isVisible = true |
Voronoi(70, 700).isVisible = true |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
Line 1,191: | Line 1,191: | ||
If no place on a vertical line is closer to the current site, then there's no point looking further left or right. |
If no place on a vertical line is closer to the current site, then there's no point looking further left or right. |
||
Don't bother square-rooting to get distances.. |
Don't bother square-rooting to get distances.. |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
WindowWidth =600 |
WindowWidth =600 |
||
WindowHeight =600 |
WindowHeight =600 |
||
Line 1,283: | Line 1,283: | ||
next y |
next y |
||
end function |
end function |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 1,289: | Line 1,289: | ||
{{works with|LÖVE|11.3}} |
{{works with|LÖVE|11.3}} |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="lua"> |
||
function love.load( ) |
function love.load( ) |
||
love.math.setRandomSeed( os.time( ) ) --set the random seed |
love.math.setRandomSeed( os.time( ) ) --set the random seed |
||
Line 1,367: | Line 1,367: | ||
end |
end |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">Needs["ComputationalGeometry`"] |
||
DiagramPlot[{{4.4, 14}, {6.7, 15.25}, {6.9, 12.8}, {2.1, 11.1}, {9.5, 14.9}, {13.2, 11.9}, {10.3, 12.3}, |
DiagramPlot[{{4.4, 14}, {6.7, 15.25}, {6.9, 12.8}, {2.1, 11.1}, {9.5, 14.9}, {13.2, 11.9}, {10.3, 12.3}, |
||
{6.8, 9.5}, {3.3, 7.7}, {0.6, 5.1}, {5.3, 2.4}, {8.45, 4.7}, {11.5, 9.6}, {13.8, 7.3}, {12.9, 3.1}, {11, 1.1}}]</ |
{6.8, 9.5}, {3.3, 7.7}, {0.6, 5.1}, {5.3, 2.4}, {8.45, 4.7}, {11.5, 9.6}, {13.8, 7.3}, {12.9, 3.1}, {11, 1.1}}]</syntaxhighlight> |
||
[[File:mma_voronoi.png|Right]] |
[[File:mma_voronoi.png|Right]] |
||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
<lang>0 П4 |
<syntaxhighlight lang="text">0 П4 |
||
0 П5 |
0 П5 |
||
ИП0 1 - x^2 ИП1 1 - x^2 + КвКор П3 |
ИП0 1 - x^2 ИП1 1 - x^2 + КвКор П3 |
||
Line 1,386: | Line 1,386: | ||
КИП7 [x] С/П |
КИП7 [x] С/П |
||
КИП5 ИП5 ИП1 - x>=0 04 |
КИП5 ИП5 ИП1 - x>=0 04 |
||
КИП4 ИП4 ИП0 - x>=0 02</ |
КИП4 ИП4 ИП0 - x>=0 02</syntaxhighlight> |
||
''Input'': Р0 - diagram width; Р1 - diagram height; Р0 - number of the points; РA - РE - coordinates and colors of the points in format ''C,XXYY'' (example: 3,0102). |
''Input'': Р0 - diagram width; Р1 - diagram height; Р0 - number of the points; РA - РE - coordinates and colors of the points in format ''C,XXYY'' (example: 3,0102). |
||
Line 1,418: | Line 1,418: | ||
{{works with|nim|0.19.4}} |
{{works with|nim|0.19.4}} |
||
{{libheader|nim-libgd}} |
{{libheader|nim-libgd}} |
||
< |
<syntaxhighlight lang="nim"> |
||
from sequtils import newSeqWith |
from sequtils import newSeqWith |
||
from random import rand, randomize |
from random import rand, randomize |
||
Line 1,472: | Line 1,472: | ||
main() |
main() |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
{{works with|ocaml|4.07.1}} |
{{works with|ocaml|4.07.1}} |
||
< |
<syntaxhighlight lang="ocaml">let n_sites = 220 |
||
let size_x = 640 |
let size_x = 640 |
||
Line 1,536: | Line 1,536: | ||
rand_int_range 20 140)) |
rand_int_range 20 140)) |
||
in |
in |
||
gen_map ~site ~rgb</ |
gen_map ~site ~rgb</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use Imager; |
use Imager; |
||
Line 1,593: | Line 1,593: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/voronoi-Euclidean.png Euclidean Voronoi diagram] (offsite image) |
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/voronoi-Euclidean.png Euclidean Voronoi diagram] (offsite image) |
||
Line 1,603: | Line 1,603: | ||
Can resize, double or halve sites (press +/-), and toggle between Euclid, Manhattan, and Minkowski (press e/m/w). |
Can resize, double or halve sites (press +/-), and toggle between Euclid, Manhattan, and Minkowski (press e/m/w). |
||
You can run this online [http://phix.x10.mx/p2js/voronoi.htm here] (it' a bit slow tho). |
You can run this online [http://phix.x10.mx/p2js/voronoi.htm here] (it' a bit slow tho). |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
<span style="color: #000080;font-style:italic;">-- |
||
-- demo\rosetta\VoronoiDiagram.exw |
-- demo\rosetta\VoronoiDiagram.exw |
||
Line 1,805: | Line 1,805: | ||
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|Processing}}== |
=={{header|Processing}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="java">void setup() { |
||
size(500, 500); |
size(500, 500); |
||
generateVoronoiDiagram(width, height, 25); |
generateVoronoiDiagram(width, height, 25); |
||
Line 1,843: | Line 1,843: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
==={{header|Processing Python mode}}=== |
==={{header|Processing Python mode}}=== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="python">def setup(): |
||
size(500, 500) |
size(500, 500) |
||
generate_voronoi_diagram(width, height, 25) |
generate_voronoi_diagram(width, height, 25) |
||
Line 1,870: | Line 1,870: | ||
j = i |
j = i |
||
set(x, y, color(nr[j], ng[j], nb[j])) |
set(x, y, color(nr[j], ng[j], nb[j])) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Line 1,877: | Line 1,877: | ||
< |
<syntaxhighlight lang="prolog">:- dynamic pt/6. |
||
voronoi :- |
voronoi :- |
||
V is random(20) + 20, |
V is random(20) + 20, |
||
Line 1,938: | Line 1,938: | ||
minkowski_3(X1, Y1, X2, Y2, D) :- |
minkowski_3(X1, Y1, X2, Y2, D) :- |
||
D is (abs(X2 - X1)**3 + abs(Y2-Y1)**3)**0.33. |
D is (abs(X2 - X1)**3 + abs(Y2-Y1)**3)**0.33. |
||
</syntaxhighlight> |
|||
</lang> |
|||
[[File:prolog_manhattan.png|320px]] |
[[File:prolog_manhattan.png|320px]] |
||
[[File:prolog_euclide.png|320px]] |
[[File:prolog_euclide.png|320px]] |
||
Line 1,947: | Line 1,947: | ||
===Euclidean=== |
===Euclidean=== |
||
[[File:Voronoi_PureBasic.png|320px|thumb|center|Voronoi Diagram in PureBasic]] |
[[File:Voronoi_PureBasic.png|320px|thumb|center|Voronoi Diagram in PureBasic]] |
||
< |
<syntaxhighlight lang="purebasic">Structure VCoo |
||
x.i: y.i |
x.i: y.i |
||
Colour.i: FillColour.i |
Colour.i: FillColour.i |
||
Line 2,016: | Line 2,016: | ||
If file$ <> "" |
If file$ <> "" |
||
SaveImage(img, file$, #PB_ImagePlugin_PNG) |
SaveImage(img, file$, #PB_ImagePlugin_PNG) |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
===Taxicab=== |
===Taxicab=== |
||
[[File:Voronoi_Diagram_in_PureBasic_(Taxicab).png|320px|thumb|center|Voronoi Diagram in PureBasic]] |
[[File:Voronoi_Diagram_in_PureBasic_(Taxicab).png|320px|thumb|center|Voronoi Diagram in PureBasic]] |
||
< |
<syntaxhighlight lang="purebasic">Structure VCoo |
||
x.i: y.i |
x.i: y.i |
||
Colour.i: FillColour.i |
Colour.i: FillColour.i |
||
Line 2,087: | Line 2,087: | ||
If file$ <> "" |
If file$ <> "" |
||
SaveImage(img, file$, #PB_ImagePlugin_PNG) |
SaveImage(img, file$, #PB_ImagePlugin_PNG) |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
This implementation takes in a list of points, each point being a tuple and returns a dictionary consisting of all the points at a given site. |
This implementation takes in a list of points, each point being a tuple and returns a dictionary consisting of all the points at a given site. |
||
< |
<syntaxhighlight lang="python">from PIL import Image |
||
import random |
import random |
||
import math |
import math |
||
Line 2,123: | Line 2,123: | ||
image.show() |
image.show() |
||
generate_voronoi_diagram(500, 500, 25)</ |
generate_voronoi_diagram(500, 500, 25)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[[File:Voronoi_python.png|500px|thumb|center|Voronoi Diagram in Python]] |
[[File:Voronoi_python.png|500px|thumb|center|Voronoi Diagram in Python]] |
||
Line 2,129: | Line 2,129: | ||
=={{header|QB64}}== |
=={{header|QB64}}== |
||
{{trans|Liberty Basic}} |
{{trans|Liberty Basic}} |
||
< |
<syntaxhighlight lang="qb64">_Title "Voronoi Diagram" |
||
Dim As Integer pnt, px, py, i, x, y, adjct, sy, ly |
Dim As Integer pnt, px, py, i, x, y, adjct, sy, ly |
||
Line 2,197: | Line 2,197: | ||
End If |
End If |
||
Next |
Next |
||
End Function</ |
End Function</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
Line 2,214: | Line 2,214: | ||
[[File:VDR310.png|200px|right|thumb|Output VDR310.png]] |
[[File:VDR310.png|200px|right|thumb|Output VDR310.png]] |
||
<syntaxhighlight lang="r"> |
|||
<lang r> |
|||
## HF#1 Random Hex color |
## HF#1 Random Hex color |
||
randHclr <- function() { |
randHclr <- function() { |
||
Line 2,266: | Line 2,266: | ||
pVoronoiD(10,"","",2) ## Manhattan metric |
pVoronoiD(10,"","",2) ## Manhattan metric |
||
pVoronoiD(10,"","",3) ## Minkovski metric |
pVoronoiD(10,"","",3) ## Minkovski metric |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 2,289: | Line 2,289: | ||
First approach |
First approach |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 2,317: | Line 2,317: | ||
(define c (argmin (curryr (metric) x) centroids)) |
(define c (argmin (curryr (metric) x) centroids)) |
||
(dict-set res c (cons x (dict-ref res c))))) |
(dict-set res c (cons x (dict-ref res c))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Different metrics |
Different metrics |
||
< |
<syntaxhighlight lang="racket"> |
||
(define (euclidean-distance a b) |
(define (euclidean-distance a b) |
||
(for/sum ([x (in-vector a)] [y (in-vector b)]) |
(for/sum ([x (in-vector a)] [y (in-vector b)]) |
||
Line 2,330: | Line 2,330: | ||
(define metric (make-parameter euclidean-distance)) |
(define metric (make-parameter euclidean-distance)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
[[File:voronoi2.png|200px|thumb|right|The contour plot of the classification function.]] |
[[File:voronoi2.png|200px|thumb|right|The contour plot of the classification function.]] |
||
Line 2,338: | Line 2,338: | ||
Alternative approach |
Alternative approach |
||
< |
<syntaxhighlight lang="racket"> |
||
;; Plots the Voronoi diagram as a contour plot of |
;; Plots the Voronoi diagram as a contour plot of |
||
;; the classification function built for a set of points |
;; the classification function built for a set of points |
||
Line 2,363: | Line 2,363: | ||
(λ (x) |
(λ (x) |
||
(hash-ref tbl (argmin (curry (metric) x) centroids)))) |
(hash-ref tbl (argmin (curry (metric) x) centroids)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="racket"> |
||
(define pts |
(define pts |
||
(for/list ([i 50]) (vector (random) (random)))) |
(for/list ([i 50]) (vector (random) (random)))) |
||
Line 2,388: | Line 2,388: | ||
#:alphas '(1)) |
#:alphas '(1)) |
||
(points3d pts3d #:sym 'fullcircle3))) |
(points3d pts3d #:sym 'fullcircle3))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 2,398: | Line 2,398: | ||
Generates a Euclidean, a Taxicab and a Minkowski Voronoi diagram using the same set of domain points and colors. |
Generates a Euclidean, a Taxicab and a Minkowski Voronoi diagram using the same set of domain points and colors. |
||
<lang |
<syntaxhighlight lang="raku" line>use Image::PNG::Portable; |
||
my @bars = '▁▂▃▅▆▇▇▆▅▃▂▁'.comb; |
my @bars = '▁▂▃▅▆▇▇▆▅▃▂▁'.comb; |
||
Line 2,442: | Line 2,442: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
See [https://github.com/thundergnat/rc/blob/master/img/Voronoi-Euclidean-perl6.png Euclidean], [https://github.com/thundergnat/rc/blob/master/img/Voronoi-Taxicab-perl6.png Taxicab] & [https://github.com/thundergnat/rc/blob/master/img/Voronoi-Minkowski-perl6.png Minkowski] Voronoi diagram example images. |
See [https://github.com/thundergnat/rc/blob/master/img/Voronoi-Euclidean-perl6.png Euclidean], [https://github.com/thundergnat/rc/blob/master/img/Voronoi-Taxicab-perl6.png Taxicab] & [https://github.com/thundergnat/rc/blob/master/img/Voronoi-Minkowski-perl6.png Minkowski] Voronoi diagram example images. |
||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="red">Red [ |
||
Source: https://github.com/vazub/rosetta-red |
Source: https://github.com/vazub/rosetta-red |
||
Tabs: 4 |
Tabs: 4 |
||
Line 2,509: | Line 2,509: | ||
image diagram-l1 image diagram-l2 |
image diagram-l1 image diagram-l2 |
||
] |
] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ReScript}}== |
=={{header|ReScript}}== |
||
< |
<syntaxhighlight lang="rescript">let n_sites = 60 |
||
let size_x = 640 |
let size_x = 640 |
||
Line 2,578: | Line 2,578: | ||
gen_map(site, rgb) |
gen_map(site, rgb) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Voronoi diagram |
# Project : Voronoi diagram |
||
Line 2,703: | Line 2,703: | ||
next |
next |
||
return number(str) |
return number(str) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output image: |
Output image: |
||
Line 2,711: | Line 2,711: | ||
Uses [[Raster graphics operations/Ruby]] |
Uses [[Raster graphics operations/Ruby]] |
||
[[File:voronoi_rb.png|thumb|right|Sample output from Ruby program]] |
[[File:voronoi_rb.png|thumb|right|Sample output from Ruby program]] |
||
< |
<syntaxhighlight lang="ruby"># frozen_string_literal: true |
||
require_relative 'raster_graphics' |
require_relative 'raster_graphics' |
||
Line 2,749: | Line 2,749: | ||
end |
end |
||
pixmap.save_as_png('voronoi_rb.png')</ |
pixmap.save_as_png('voronoi_rb.png')</syntaxhighlight> |
||
{{libheader|RubyGems}} |
{{libheader|RubyGems}} |
||
{{libheader|JRubyArt}} |
{{libheader|JRubyArt}} |
||
JRubyArt is a port of processing to ruby |
JRubyArt is a port of processing to ruby |
||
< |
<syntaxhighlight lang="ruby"># frozen_string_literal: true |
||
Tile = Struct.new(:x, :y, :color) do |
Tile = Struct.new(:x, :y, :color) do |
||
Line 2,798: | Line 2,798: | ||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">graphic #g, 400,400 |
||
#g flush() |
#g flush() |
||
spots = 100 |
spots = 100 |
||
Line 2,875: | Line 2,875: | ||
end if |
end if |
||
next y |
next y |
||
end function</ |
end function</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 2,883: | Line 2,883: | ||
The entire code, including the Crate.toml and a precompiled binary for Windows x86_64, can be found at https://github.com/ctrlcctrlv/interactive-voronoi/ |
The entire code, including the Crate.toml and a precompiled binary for Windows x86_64, can be found at https://github.com/ctrlcctrlv/interactive-voronoi/ |
||
< |
<syntaxhighlight lang="rust">extern crate piston; |
||
extern crate opengl_graphics; |
extern crate opengl_graphics; |
||
extern crate graphics; |
extern crate graphics; |
||
Line 3,063: | Line 3,063: | ||
); |
); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
Line 3,069: | Line 3,069: | ||
{{libheader|Scala Java Swing interoperability}} |
{{libheader|Scala Java Swing interoperability}} |
||
{{works with|Scala|2.13}} |
{{works with|Scala|2.13}} |
||
< |
<syntaxhighlight lang="scala">import java.awt.geom.Ellipse2D |
||
import java.awt.image.BufferedImage |
import java.awt.image.BufferedImage |
||
import java.awt.{Color, Graphics, Graphics2D} |
import java.awt.{Color, Graphics, Graphics2D} |
||
Line 3,110: | Line 3,110: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
[[file:Seed7Voronoi.png|thumb|right]] |
[[file:Seed7Voronoi.png|thumb|right]] |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "draw.s7i"; |
include "draw.s7i"; |
||
include "keybd.s7i"; |
include "keybd.s7i"; |
||
Line 3,165: | Line 3,165: | ||
KEYBOARD := GRAPH_KEYBOARD; |
KEYBOARD := GRAPH_KEYBOARD; |
||
readln(KEYBOARD); |
readln(KEYBOARD); |
||
end func;</ |
end func;</syntaxhighlight> |
||
Original source: [http://seed7.sourceforge.net/algorith/graphic.htm#voronoi] |
Original source: [http://seed7.sourceforge.net/algorith/graphic.htm#voronoi] |
||
Line 3,171: | Line 3,171: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="ruby">require('Imager') |
||
func generate_voronoi_diagram(width, height, num_cells) { |
func generate_voronoi_diagram(width, height, num_cells) { |
||
Line 3,193: | Line 3,193: | ||
var img = generate_voronoi_diagram(500, 500, 25) |
var img = generate_voronoi_diagram(500, 500, 25) |
||
img.write(file => 'VoronoiDiagram.png')</ |
img.write(file => 'VoronoiDiagram.png')</syntaxhighlight> |
||
Output image: [https://github.com/trizen/rc/blob/master/img/voronoi-diagram-sidef.png Voronoi diagram] |
Output image: [https://github.com/trizen/rc/blob/master/img/voronoi-diagram-sidef.png Voronoi diagram] |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
< |
<syntaxhighlight lang="tcl">package require Tk |
||
proc r to {expr {int(rand()*$to)}}; # Simple helper |
proc r to {expr {int(rand()*$to)}}; # Simple helper |
||
Line 3,232: | Line 3,232: | ||
# To display while generating, uncomment this line and the other one so commented |
# To display while generating, uncomment this line and the other one so commented |
||
#update |
#update |
||
voronoi demo 60</ |
voronoi demo 60</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|DOME}} |
{{libheader|DOME}} |
||
< |
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color |
||
import "dome" for Window |
import "dome" for Window |
||
import "random" for Random |
import "random" for Random |
||
Line 3,280: | Line 3,280: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
[[File:VoronoiXPL0.gif|right]] |
[[File:VoronoiXPL0.gif|right]] |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations |
||
def N = 15; \number of sites |
def N = 15; \number of sites |
||
Line 3,305: | Line 3,305: | ||
I:= ChIn(1); \wait for keystroke |
I:= ChIn(1); \wait for keystroke |
||
SetVid($03); \restore normal text screen |
SetVid($03); \restore normal text screen |
||
]</ |
]</syntaxhighlight> |
||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
<syntaxhighlight lang="yabasic"> |
|||
<lang Yabasic> |
|||
clear screen |
clear screen |
||
Line 3,385: | Line 3,385: | ||
next y |
next y |
||
return check |
return check |
||
end sub</ |
end sub</syntaxhighlight> |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="yabasic">width = 500 : height = 500 |
||
open window width, height |
open window width, height |
||
Line 3,420: | Line 3,420: | ||
end sub |
end sub |
||
generate_voronoi_diagram(width, height, 25)</ |
generate_voronoi_diagram(width, height, 25)</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Line 3,426: | Line 3,426: | ||
{{trans|Python}} |
{{trans|Python}} |
||
[[File:VoronoiDiagram.zkl.jpg|250px|thumb|right]] |
[[File:VoronoiDiagram.zkl.jpg|250px|thumb|right]] |
||
< |
<syntaxhighlight lang="zkl">fcn generate_voronoi_diagram(width,height,num_cells){ |
||
image,imgx,imgy:=PPM(width,height),width,height; |
image,imgx,imgy:=PPM(width,height),width,height; |
||
nx:=num_cells.pump(List,(0).random.fp(imgx)); |
nx:=num_cells.pump(List,(0).random.fp(imgx)); |
||
Line 3,444: | Line 3,444: | ||
} |
} |
||
image |
image |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">generate_voronoi_diagram(500,500,25).write(File("VoronoiDiagram.ppm","wb"));</syntaxhighlight> |
||
{{omit from|GUISS}} |
{{omit from|GUISS}} |