Basically, a number of points are chosen (1000, in my example, but you can change it at your will), and every pixel will get the color of the closest point.
The resulting effect may resemble a crystal, or a scaly skin, and could be useful for texture generation.
const MaxAreas=1000
randomize timer
type voronoi
x as integer
y as integer
c as integer
end type
Screenres 800,600,32
dim areas(maxareas) as voronoi
for i as integer=0 to maxareas
areas(i).x=rnd(1)*800
areas(i).y=rnd(1)*600
areas(i).c=rgb(rnd(1)*256,rnd(1)*256,rnd(1)*256)
next
dim as unsigned integer ptr target=screenptr
screenlock
for y as integer=1 to 600
for x as integer=1 to 800
dim as integer dist=800*600,d,dx,dy
for i as integer=0 to maxareas
dx=areas(i).x-x
dy=areas(i).y-y
d=dx*dx+dy*dy
if d<dist then dist=d:*target=areas(i).c
next
target+=1
next
next
screenunlock
sleep
Const MaxAreas=6000
Randomize Timer
Type voronoi
x As Integer
y As Integer
c As Integer
End Type
Screenres 640,480,32
Dim areas(maxareas) As voronoi
Dim As Unsigned Integer Ptr target=screenptr
bload "C:\FreeBasic\bitmaps\bitmaps\boat2.bmp"
For i As Integer=0 To maxareas
areas(i).x=Rnd(1)*640
areas(i).y=Rnd(1)*480
areas(i).c=point(areas(i).x,areas(i).y)
Next
screenlock
For y As Integer=1 To 480
For x As Integer=1 To 640
Dim As Integer dist=640*480,d,dx,dy
For i As Integer=0 To maxareas
dx=areas(i).x-x
dy=areas(i).y-y
d=dx*dx+dy*dy
If d<dist Then dist=d:*target=areas(i).c
Next
target+=1
Next
Next
screenunlock
Sleep
Basically, a number of points are chosen (1000, in my example, but you can change it at your will), and every pixel will get the color of the closest point.
The resulting effect may resemble a crystal, or a scaly skin, and could be useful for texture generation.
Thanks for reminding me of this. I had actually encountered them in University Computer Sci., but that was.... years ago.
I am now using it to design random room shapes in the "dungeon" of a "Roguelike" game I am working on.
Interesting aside: This algorithm has saved hundreds,maybe thousands of lives. It was used (with pen and paper, not a computer) by John Snow in 1854 to identify cholera contaminated public water taps in London England that were the cause of an epidemic of fatal illness
const iter=20
const v=8
function col (x as integer,y as integer) as unsigned integer
return point(x,y) and 255
end function
screenres 800,600,32
for i as integer=0 to 5
circle (rnd(1)*800,rnd(1)*600),rnd(1)*300,,,,rnd(1)*2.0,F
next
dim size as integer=iter+1
dim c as unsigned integer
for i as integer=1 to iter
for x as integer =0 to 800 step size
for y as integer =0 to 600 step size
c=(col(x,y)+col(x+size,y)+col(x,y)+col(x+size,y+size))/4
if c>v then c=c-rnd(1)*v
if c<256-v then c=c+rnd(1)*v
line (x,y)-(x+size-1,y+size-1),rgb(c,c,c),bf
next
next
size=size-1
sleep
next
sleep