Author Topic: A New Wrinkle with Voronoi  (Read 4741 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
A New Wrinkle with Voronoi
« on: May 10, 2021, 02:41:48 am »
Shading gives us a lovely psuedo-3D effect!
Code: QB64: [Select]
  1. _Title "Shading Voronoi Demo" 'b+ 2019-12-11  shading 2021-05-10
  2. Const xymax = 700, nPoints = 50
  3. Type pType
  4.     x As Single
  5.     y As Single
  6.     c As _Unsigned Long
  7. Screen _NewImage(xymax, xymax, 32)
  8. _ScreenMove 300, 20
  9. Dim pts(1 To nPoints) As pType
  10. For i = 1 To nPoints
  11.     pts(i).x = xymax * Rnd
  12.     pts(i).y = xymax * Rnd
  13.     pts(i).c = _RGB32(155 * Rnd + 100, -(Rnd < .5) * 255 * Rnd, -(Rnd < .5) * 255 * Rnd)
  14. For i = 1 To nPoints
  15.     Circle (pts(i).x, pts(i).y), 5, pts(i).c
  16. For y = 0 To xymax
  17.     For x = 0 To xymax
  18.         minD = 49000
  19.         For p = 1 To nPoints
  20.             d = ((pts(p).x - x) ^ 2 + (pts(p).y - y) ^ 2) ^ .5
  21.             If d < minD Then minD = d: saveP = p
  22.         Next
  23.         PSet (x, y), Ink~&(pts(saveP).c, &HFF000000, minD / 85)
  24.     Next
  25.  
  26. Sub cAnalysis (c As _Unsigned Long, outRed, outGrn, outBlu, outAlp)
  27.     outRed = _Red32(c): outGrn = _Green32(c): outBlu = _Blue32(c): outAlp = _Alpha32(c)
  28.  
  29. Function Ink~& (c1 As _Unsigned Long, c2 As _Unsigned Long, fr##)
  30.     Dim R1, G1, B1, A1, R2, G2, B2, A2
  31.     cAnalysis c1, R1, G1, B1, A1
  32.     cAnalysis c2, R2, G2, B2, A2
  33.     Ink~& = _RGB32(R1 + (R2 - R1) * fr##, G1 + (G2 - G1) * fr##, B1 + (B2 - B1) * fr##, A1 + (A2 - A1) * fr##)
  34.  
  35.  

 
Shading Voronoi.PNG

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A New Wrinkle with Voronoi
« Reply #1 on: May 10, 2021, 02:48:06 am »
So what happens if we go the other way?

Same code except here:
Code: QB64: [Select]
  1. PSet (x, y), Ink~&(pts(saveP).c, &HFF000000, minD / 85)
  2.  

Make this:
Code: QB64: [Select]
  1. PSet (x, y), Ink~&(pts(saveP).c, &HFFFFFFFF, minD / 85)
  2.  

 
Reverse shading.PNG

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A New Wrinkle with Voronoi
« Reply #2 on: May 10, 2021, 02:56:06 am »
Here's one for johnno56 in the key of blue

 
One for johnno56.PNG


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A New Wrinkle with Voronoi
« Reply #3 on: May 11, 2021, 10:58:32 pm »
Someone at Syntax Bomb alerted me to this method for Voronoi
https://en.wikipedia.org/wiki/Fortune%27s_algorithm

Looks interesting! Who can code it first?

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: A New Wrinkle with Voronoi
« Reply #4 on: May 12, 2021, 02:41:21 am »
The small animation reminded me of the creation of soap bubbles...
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A New Wrinkle with Voronoi
« Reply #5 on: May 12, 2021, 01:03:03 pm »
Yeah, I am wondering if Andy Amaya's method is using any of this or is it an entirely different 3rd approach.

The Wiki is way over my head with formula jargon but (ironically) there is a link to something more readable from AMS.
 https://www.ams.org/publicoutreach/feature-column/fcarc-voronoi

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: A New Wrinkle with Voronoi
« Reply #6 on: May 12, 2021, 04:06:42 pm »
The Wiki is "over your head" but the AMS is more readable? Aw man. The AMS example may as well have been written in Romulan... Says a lot about my ability to understand 'jargon'... *sigh*
Logic is the beginning of wisdom.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: A New Wrinkle with Voronoi
« Reply #7 on: May 13, 2021, 06:17:27 am »
I found a C++ implementation of the Fortune's Algorithm  https://www.cs.hmc.edu/~mbrubeck/voronoi.html
but an easier explanation of the algorithm is found here https://jacquesheunis.com/post/fortunes-algorithm/
« Last Edit: May 13, 2021, 06:56:11 am by jack »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A New Wrinkle with Voronoi
« Reply #8 on: May 13, 2021, 09:24:28 am »
Thanks Jack, I saw the C++ code before Yikes! the other looks readable and may help supplement with AMS paper.

At moment I am dogging 2 ball collisions with vectors.