sroze / PHP-Voronoi-algorithm

Steven Fortune's algorithm in PHP
31 stars 15 forks source link

Another interesting division by zero problem. #3

Closed Innominandum closed 11 years ago

Innominandum commented 11 years ago

I have found an example in which the following bit of code in connectEdge() can also cause a division by zero error. I think it also causes an endless loop in closeCells().

    // get the line equation of the bisector if line is not vertical
    if ($ry !== $ly) {
        $fm = ($lx-$rx)/($ry-$ly);
        $fb = $fy-$fm*$fx;
    }

I haven't quite figured out how your class works yet, so I haven't been able to fix it. But if you want, I can create a project that shows the problem.

Innominandum commented 11 years ago

In general, the results on a 1024x768 canvas with 1000 sites are a bit sketchy, returning odd results about 50% of the time. I've created a project that randomly creates the sites based on a supplied seed, which then spits out a page with an svg that can highlight the polygons on a mouseover and hide them on a mousedown event. It quickly shows the problem when using seed 863036 for example: https://www.dropbox.com/s/5xkyd9v6jofwq4l/sroze.rar

Innominandum commented 11 years ago

Oh, I should note that I made a small adjustment in the sample project I linked to above, and that is that in closeCells() I've added some lines starting on line 1035 that breaks the loop if iLeft goes above 100. That's to prevent from a cell getting an endless amount of edges as border edges get added until the script times out. I have no clue why it's doing that, but it seems to happen frequently.

Innominandum commented 11 years ago

After quite a search, I found it was the blue noise I was adding to the initial site generation that was causing the trouble and not the library itself. Sorry for the false alarm, guys. In case anyone is interested, I did two passes of Lloyds relaxation, but I never checked to see if the new sites stayed within the bounding box, which created some crazy results.