[SOLVED] Voronoi diagram lazarus / 39-Graphics ---------------------------------------------------------------------- Phoenix · 2024-06-08 17:07 Hi, I tried to convert the https://github.com/983/df source but there is an error that I can't see. I attach a picture demonstrating the failure  . I saw from the forum that there is another implementation but I would like to fix this one. If it works correctly, any optimizations are also welcome. ---------------------------------------------------------------------- Eugene Loza · 2024-06-09 06:19 I'm not sure what exactly is the problem you are having. This is exactly how Voronoi diagrams look like Note that you are (to as much as I understand the code) initializing points to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i x:= rand mod nx;y:= rand mod ny; with rand being a random number from 0 to 254 and nx=640, ny=360 -> x and y will always be within 0..254 range, i.e. your points crowd to top-left corner of the image, which is exactly what you observe in the image and I guess this is the part you are calling "error" and "failure" (otherwise please specify what exactly doesn't work for you). Everything beyond this range is just a "tail" of those points, exactly as it should be. e.g. replacing those for Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i x:= Round(Random * nx);y:= Round(Random * ny); And not forgetting Randomize before calling Random results in attached image. Do not rush into optimizations. First make sure you understand the code well and/or the code does exactly what you need it to do. Optimizations most often "reduce the ability to read the code", which is exactly the opposite of what you need now. ---------------------------------------------------------------------- Phoenix · 2024-06-09 08:09 QuoteI'm not sure what exactly is the problem you are havingthe appearance was different from the one presented in the link and this made it unusable to use as a generator in a game. Now I understand the error in the code, thank you very much  QuoteFirst make sure you understand the code well and/or the code does exactly what you need it to do.in fact, looking at it now, the problem was trivial ---------------------------------------------------------------------- Boleeman · 2024-06-09 10:59 Made a little unfilled/Filled sampler of it rendered to a TPaintbox. ---------------------------------------------------------------------- Phoenix · 2024-06-09 14:07 QuoteMade a little unfilled/Filled sampler of it rendered to a TPaintbox.Thanks, I appreciated the extended application ---------------------------------------------------------------------- Boleeman · 2024-06-10 00:45 You are welcome. The Tcheckbox logic still needs a further tweak, but the main parts are working (hopefully). I thought of converting to BgraBmp (and making the non-filled version Anti-aliased) and speeding it up a bit. Also: There was a nice Vb6 version here which I may try to convert: https://www.vbforums.com/showthread.php?893892-Voronoi-Draw ---------------------------------------------------------------------- Dzandaa · 2024-06-10 11:43 Hi, This is a BGRABitmap version of Voronoi. B-> ---------------------------------------------------------------------- circular · 2024-06-10 19:57 It's a pleasure to see all these wonderful rendering with BGRABitmap.  About Voronoi, I remember there is a geometrical way of computing the polygons. This can then be used to draw the diagram with antialiasing. Note that contiguous polygons without border won't render perfectly when drawn sequentially. To get an antialiasing between the polygons, I've provided TBGRAMultishapeFiller class. It can be set up with many shapes and they can be drawn together according to the PolygonOrder property. ---------------------------------------------------------------------- Phoenix · 2024-06-10 22:33 Quote from: Eugene Loza on June 09, 2024, 08:19:01 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i x:= Round(Random * nx);y:= Round(Random * ny);Considering the limit. So: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i x:= Trunc(Random * nx);y:= Trunc(Random * ny);Anyway @Dzandaa introduced an optimization Quote from: Boleeman on June 10, 2024, 02:45:50 amAlso: There was a nice Vb6 version here which I may try to convert: https://www.vbforums.com/showthread.php?893892-Voronoi-Draw Nice link, the graphic aspect is fantastic, it looks like a mosaic window  but I can't evaluate the speed of the algorithm Quote from: Dzandaa on June 10, 2024, 01:43:56 pmThis is a BGRABitmap version of Voronoi. Thanks for this additional application which is more performing thanks to BGRABitmap avoiding "pixels[ ]"  . I saw that you removed the while loop so you also have to consider the +1 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ..for x1:= x1+1 to Pred(nx) do //# +1begin // Load new parabola (x1, dy1). dy1:= Distances[x1 + y*nx]; ..  I've seen that sometimes lines appear, I don't know if this is the optimal solution but they disappear Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ..if k = 0.0 then  x_intersections[n] := 0//Math.Infinity..  Quote from: circular on June 10, 2024, 09:57:55 pmIt's a pleasure to see all these wonderful rendering with BGRABitmap.  +1 ---------------------------------------------------------------------- Josh · 2024-06-11 09:26 made a quick 'code and go' version, it is very very slow(too much calculations), and in no way complete was going to add other things but it got even slower so stopped. I have left that code in even though it is not used. So I posted just for people to have a play and fix/speedup/complete it. ---------------------------------------------------------------------- Dzandaa · 2024-06-11 09:38 Hi, @Phoenix: Here is the modified code. B-> ---------------------------------------------------------------------- Dzandaa · 2024-06-11 12:29 Hi, @Josh: This is a version using TBGRAVirtualScreen, a little faster B-> ---------------------------------------------------------------------- Boleeman · 2024-06-11 14:06 I found that resizing the form on 'Josh/DZandaa latest 3D version produces some pretty impressive results, even on my really slow AMD computer. Amazing. It is great seeing all of these different variations in Voronoi. Like the last picture below in the next thread, with Bubble FX. Wondered if we could get shiny metallic chromium effect, like an all Gold or all Silver effect? My attached Voronoi picture below has the bevels done with the 2nd version of my Bevel Maker. ---------------------------------------------------------------------- Dzandaa · 2024-06-11 14:58 Hi, Here is a new version with Bubble Fx. B-> ---------------------------------------------------------------------- Phoenix · 2024-06-12 10:51 thank you all ---------------------------------------------------------------------- Boleeman · 2024-07-27 01:55 Found a nice Fractal like stained glass like Javascript version here: https://gist.github.com/josdirksen/806e33c678753cd5133c1d40d73e8360 I resized the original GitHub png, as it was too large. Also attached the html, js source. Looks amazing. Would be nice to convert to Bgrabmp ? ---------------------------------------------------------------------- wp · 2024-07-27 10:27 How about this (just pushed the project to my github: https://github.com/wp-xyz/ImagePoster): ---------------------------------------------------------------------- Phoenix · 2024-07-27 20:19 Wow, much appreciated thanks to you both! ---------------------------------------------------------------------- Boleeman · 2024-07-27 23:52 Wow W.P., that posterizer has some great features. Thanks for the upload. Even has Lloyds Algorithm (read a bit about it, but did not see it in action). With the JavaScript version, I tried to convert to BGRAVirtualScreen, but it seems D3.js has some Voronoi seeded function and a random polygons function. I think I may have seen something like the JavaScript version, written in FreeBasic years ago called Fractured Glass. Thanks W.P. for your nice upload. Very creative use of Voronoi and Delaunay. Not sure if anyone else is getting an Invalid Operation error with the W.P program when using other images. I get an error after clicking btnApplyLloydsAlgorithm here:        w[j] := w[j] / wsum ;              in procedure TMainForm.LloydsAlgorithm; Possibly because of a divide by zero error because wsum = 0  ? I don't seem to get that error if I keep on using Lenna.png, but for any other png I get that error. ---------------------------------------------------------------------- wp · 2024-07-28 11:37 Can you post that image? ---------------------------------------------------------------------- Boleeman · 2024-07-29 06:40 Ok W.P.  Attached are the images that had the error come up when Lloyds algorithm was applied. When running the compiled exe and loading either of the 2 attached pngs and applying the Lloyds algorithm I get an Invalid floating point operation. When compiling in the IDE and loading either of the 2 attached pngs and applying the Lloyds algorithm I get an FLT Invalid operation at line 502  which is   w[j] := w[j] / wsum; I had all Tcheckboxes enabled in each case. That error was not coming up when Lenna_(test_image).png  was used. Not exactly sure why this happens. ---------------------------------------------------------------------- wp · 2024-07-29 15:51 Please try the new commit. ---------------------------------------------------------------------- Boleeman · 2024-07-30 07:28 Hi W.P. The functions work for all png pictures, but the Lloyds algorithm seems to not work for jpg. Looks like jpg compression is the problem. I tried the new commit with other png files and have things working correctly (and don't get any floating point error). Also have the console window appearing. ---------------------------------------------------------------------- wp · 2024-07-30 12:01 TBitmap.Assign is doing strange things sometimes... Now painted the loaded bitmap to the draw bitmap, and the issue is gone. Also removed the console window (cause by option -WG in project settings). ---------------------------------------------------------------------- Boleeman · 2024-07-31 07:11 W.P. That latest tweak is working really well for all file types. I like the triangular gradient fill effect. Getting some nice results, as shown in attached files. I used my bevel maker for the framed section. Perhaps might be nice to have a "apply a frame option". 1st attached png: Applied Lloyd's algorithm (Gives a cool skin-like Voronoi texture). 2nd attached png: Applied Delaunay Border to Lloyd algorithm png (Gives a fibrous texture). 3nd attached png: Applied triangular gradient fill effect (Gives a dreamy picture). Thank you W.P. for those extra tweaks.