Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Better maps with Math::Geometry::Voronoi, and a Challenge for Math Monks

by BrowserUk (Patriarch)
on Jul 02, 2008 at 18:34 UTC ( [id://695176]=note: print w/replies, xml ) Need Help??


in reply to Better maps with Math::Geometry::Voronoi, and a Challenge for Math Monks

One thing that may be confusing your attempts to join the polygons, is that the polygons returned from M::G::V->polygons can contain duplicate points. Are you aware of this?

By way of example, the following creates a regular grid of 9x7 equi-spaced points and the uses M::G::V to compute and return the polys. The result is 35 (7x5) "square" polygons. But each of the polys return has 6 rather than 4 points? With 2 points being duplicated in each poly. This doesn't affect the drawing of the polys, but it gets confusing when trying to manipulate the edges.

#! perl -slw use strict; use Data::Dump qw[ pp ]; $Data::Dump::MAX_WIDTH = 200; use Math::Geometry::Voronoi; my @points = map{ my $y = $_; map [ $_, $y ], map $_ * 100, 1 .. 9; } map $_ * 100, 1 .. 7; my $geo = Math::Geometry::Voronoi->new( points => \@points ); $geo->compute; my @geoPolys = $geo->polygons; pp \@geoPolys;

Produces:

C:\test>voronoi [ ## {**** duplicates ****} {**** duplicates ****} [10, [250, 250], [250, 250], [250, 150], [150, 150], [150, 150], [15 +0, 250]], [11, [350, 250], [350, 250], [350, 150], [250, 150], [250, 150], [25 +0, 250]], [12, [450, 250], [450, 250], [450, 150], [350, 150], [350, 150], [35 +0, 250]], [13, [550, 250], [550, 250], [550, 150], [450, 150], [450, 150], [45 +0, 250]], [14, [650, 250], [650, 250], [650, 150], [550, 150], [550, 150], [55 +0, 250]], [15, [750, 250], [750, 250], [750, 150], [650, 150], [650, 150], [65 +0, 250]], [16, [850, 250], [850, 250], [850, 150], [750, 150], [750, 150], [75 +0, 250]], [19, [250, 350], [250, 350], [250, 250], [150, 250], [150, 250], [15 +0, 350]], [20, [350, 350], [350, 350], [350, 250], [250, 250], [250, 250], [25 +0, 350]], [21, [450, 350], [450, 350], [450, 250], [350, 250], [350, 250], [35 +0, 350]], [22, [550, 350], [550, 350], [550, 250], [450, 250], [450, 250], [45 +0, 350]], [23, [650, 350], [650, 350], [650, 250], [550, 250], [550, 250], [55 +0, 350]], [24, [750, 350], [750, 350], [750, 250], [650, 250], [650, 250], [65 +0, 350]], [25, [850, 350], [850, 350], [850, 250], [750, 250], [750, 250], [75 +0, 350]], [28, [250, 450], [250, 450], [250, 350], [150, 350], [150, 350], [15 +0, 450]], [29, [350, 450], [350, 450], [350, 350], [250, 350], [250, 350], [25 +0, 450]], [30, [450, 450], [450, 450], [450, 350], [350, 350], [350, 350], [35 +0, 450]], [31, [550, 450], [550, 450], [550, 350], [450, 350], [450, 350], [45 +0, 450]], [32, [650, 450], [650, 450], [650, 350], [550, 350], [550, 350], [55 +0, 450]], [33, [750, 450], [750, 450], [750, 350], [650, 350], [650, 350], [65 +0, 450]], [34, [850, 450], [850, 450], [850, 350], [750, 350], [750, 350], [75 +0, 450]], [37, [250, 550], [250, 550], [250, 450], [150, 450], [150, 450], [15 +0, 550]], [38, [350, 550], [350, 550], [350, 450], [250, 450], [250, 450], [25 +0, 550]], [39, [450, 550], [450, 550], [450, 450], [350, 450], [350, 450], [35 +0, 550]], [40, [550, 550], [550, 550], [550, 450], [450, 450], [450, 450], [45 +0, 550]], [41, [650, 550], [650, 550], [650, 450], [550, 450], [550, 450], [55 +0, 550]], [42, [750, 550], [750, 550], [750, 450], [650, 450], [650, 450], [65 +0, 550]], [43, [850, 550], [850, 550], [850, 450], [750, 450], [750, 450], [75 +0, 550]], [46, [250, 650], [250, 650], [250, 550], [150, 550], [150, 550], [15 +0, 650]], [47, [350, 650], [350, 650], [350, 550], [250, 550], [250, 550], [25 +0, 650]], [48, [450, 650], [450, 650], [450, 550], [350, 550], [350, 550], [35 +0, 650]], [49, [550, 650], [550, 650], [550, 550], [450, 550], [450, 550], [45 +0, 650]], [50, [650, 650], [650, 650], [650, 550], [550, 550], [550, 550], [55 +0, 650]], [51, [750, 650], [750, 650], [750, 550], [650, 550], [650, 550], [65 +0, 650]], [52, [850, 650], [850, 650], [850, 550], [750, 550], [750, 550], [75 +0, 650]], ]

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Better maps with Math::Geometry::Voronoi, and a Challenge for Math Monks
by samtregar (Abbot) on Jul 02, 2008 at 18:45 UTC
    No I didn't know that. Should be pretty easy to filter out, I'd imagine. The underlying algorithm isn't mine, so I can't say for sure why that might happen. My guess would be very small edges collapsing to a zero-width edge. By producing squares you might very well have constructed a worst-case scenario for the algorithm.

    -sam

Re^2: Better maps with Math::Geometry::Voronoi, and a Challenge for Math Monks
by roboticus (Chancellor) on Jul 03, 2008 at 20:26 UTC
    BrowserUk:

    I used your code and examined the results of all the output arrays. From what I can see, it appears that the extra vertices are due to extra lines. Not extra in the sense that they're not valid, just in that they're unnecessary/unwanted. In the case of a square of four points (Pll, Pul, Plr, and Prr where u=upper, l=lower/left, r=right), you expect a line between Pll and Pul, you also expect a line between Pll and Plr. But a line between Pll and Prr is also valid. Since they intersect at a single point you don't need the last one. I suspect that the code is either experiencing just enough rounding error to insert the extra points, or that the code simply has all those three lines as boundaries, and it simply computes duplicates as it walks through the list of lines.

    ...roboticus
      . In the case of a square of four points (Pll, Pul, Plr, and Prr where u=upper, l=lower/left, r=right), you expect a line between Pll and Pul, you also expect a line between Pll and Plr. But a line between Pll and Prr is also valid.

      No, that cannot happen. A square can only arise from a cross pattern of 5 dots. And a diagonal line connecting the opposite corners would pass through the central dot and could not be 'normal' to any divisor between that dot and any other.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        BrowserUk:

        Sorry, I should've been more clear. I wasn't referring to the vertices, but the input points to the algorithm. So the diagonal line going through two of the points are the bisector for the other two corners. I put this case into the algorithm (just a few minutes ago):

        my @points = ( [0,0], [2, 0], [0, 2], [2, 2] );

        Yields the following lines:

        [ [1, 0, 1, 0, 1], # X=1 for points 0, 1 [0, 1, 1, 0, 2], # Y=1 for points 0, 2 [0, 1, 1, 1, 3], # Y=1 for points 1, 3 [1, 1, 2, 0, 3], # X+Y=2 for points 0, 3 [1, 0, 1, 2, 3], # X=1 for points 2, 3 ]

        That fourth entry is the diagonal I was talking about. It's clearly the bisector for points 0 and 3, but since it intersects at the same point as the first two, it's irrelevant. Looking at the edge list:

        [ [3, 1, 0], # Edge from (1,1) to (1,1) [1, -1, 1], [4, -1, 1], [2, 0, -1], [0, 0, -1] ]

        Other than the first edge, the rest are as you'd expect to see. I'm suspecting some odd comparison (or roundoff) in the C code to be generating that 0-length segment.

        Just for completeness, here's the list of vertices:

        [ [1, 1], [1, 1] ]

        Looking at the results, I'm struck by a couple items:

        • Some lines are identical, except for the point pairs they were generated from.
        • Only one of the diagonal lines is generated, which is why I suspect roundoff or an odd conditional.
        • It might be worthwhile to add disallow duplicate vertices and/or edges of zero length.

        ...roboticus

        Update: I just tried the cross that you suggested, and the data showed *absolutely no* anomalies....odd!

        Update: Fixed HTML (missed closer for UL).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://695176]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-19 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found