_Title "Voronoi Diagram [RC]" ' AndyA https://www.qb64.org/forum/index.php?topic=3831.msg131791#msg131791 ' 2021-04-20 b+ mod in attempt to make things clearer, showing how check row is working
Dim As Integer sites
, xEdge
, yEdge
, i
, x
, y
, nearest
, startX
, startY
, endY
'=====================================================================
' Changes number of sites and screen size here
'=====================================================================
sites = 100
xEdge = 700
yEdge = 700
'=====================================================================
'_FullScreen _SquarePixels
'Randomize 0.14159
col
(i
) = _RGB(Rand
(0, 255), Rand
(0, 255), Rand
(0, 255)) CircleFill townX(i), townY(i), 2, col(i)
'col(i) = _RGB32(155 * Rnd + 100, -(Rnd < .5) * 255 * Rnd, -(Rnd < .5) * 255 * Rnd)
'fill distance table with squared distances from the first site
'store the distance squared between sites in dist() array to avoid SQR()
dist(x, y) = (townX(1) - x) * (townX(1) - x) + (townY(1) - y) * (townY(1) - y)
nearestIndex(x, y) = 1
'for all the other sites
endY = yEdge - 1
'check left side
'check right side
For x
= townX
(i
) + 1 To xEdge
- 1 CircleFill townX(j), townY(j), 2, col(j)
'col(i) = _RGB32(155 * Rnd + 100, -(Rnd < .5) * 255 * Rnd, -(Rnd < .5) * 255 * Rnd)
'fill in region colors and draw region outlines
startX = x
nearest = nearestIndex(x, y)
Line (startX
, y
)-(x
+ 1, y
), col
(nearest
) 'draw region outline x locations
PSet (startX
, y
), &HFF000000 PSet (x
+ 1, y
), &HFF000000 '_Limit 10
startY = y
nearest = nearestIndex(x, y)
'draw region outline y locations
PSet (x
, startY
), &HFF000000 PSet (x
, y
+ 1), &HFF000000
showSites = 1
'show the original random sites after regions are filled
'Draw a 4 pixel circle at each of the random points in px(), py() arrays
Call CircleFill
(townX
(i
), townY
(i
), 2, &HFF000000) Line (0, 0)-(xEdge
- 1, yEdge
- 1), &HFF000000, B
dxSquared& = (townX(site%) - x) * (townX(site%) - x)
dSquared& = (townY(site%) - yt%) * (townY(site%) - yt%) + dxSquared&
If dSquared&
<= dist
(x
, yt%
) Then dist(x, yt%) = dSquared&
nearestIndex(x, yt%) = site%
PSet (x
, yt%
), col
(site%
) checkRow = 1
Rand
= Int(Rnd * (hi%
- lo%
+ 1) + lo%
)
'=============================
' CX = center x coordinate
' CY = center y coordinate
' R = radius
' C = fill color
'=============================
RadiusError = -Radius
X = Radius
Y = 0
Line (CX
- X
, CY
)-(CX
+ X
, CY
), C
, BF
RadiusError = RadiusError + Y * 2 + 1
Line (CX
- Y
, CY
- X
)-(CX
+ Y
, CY
- X
), C
, BF
Line (CX
- Y
, CY
+ X
)-(CX
+ Y
, CY
+ X
), C
, BF
X = X - 1
RadiusError = RadiusError - X * 2
Y = Y + 1
Line (CX
- X
, CY
- Y
)-(CX
+ X
, CY
- Y
), C
, BF
Line (CX
- X
, CY
+ Y
)-(CX
+ X
, CY
+ Y
), C
, BF