Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: combine(merge) polygons

by haj (Vicar)
on Jul 25, 2018 at 11:47 UTC ( [id://1219248]=note: print w/replies, xml ) Need Help??


in reply to combine(merge) polygons

This problem looks so easy if you tackle it with a pencil and paper, and yet it is rather complex to do by a computer program. There's a lot of tedious geometry to do. On the other hand, geometry isn't Perl specific. There's an Article on stackoverflow which explains the steps.

While writing this, I saw that choroba recommends Math::Geometry::Planar which helps a lot.

There are some more issues to consider:

  • Be aware of rounding errors, for example if vertices are "almost parallel".
  • Be aware that the union of the two shapes can contain a hole unless both polygons are convex. A merge of two polygons isn't necessarily convex, as your examples show. Therefore, the library returns a structure which contain more than just a list of points.
  • Be aware of self-intersecting polygons, like these:
    my @poly1 = ([0,0], [1,0], [0,1], [1,1], [0,0]); my @poly2 = ([0,0], [0,1], [1,0], [1,1], [0,0]); # A possible result could be: my @merge = ([0,0], [1,0], [1,1], [0,1], [0,0]);

Fun fact: Math::Polygon::Calc will return 0 as the area of @poly1 and @poly2, while the merged polygon has an area of 1.

Replies are listed 'Best First'.
Re^2: combine(merge) polygons
by AnomalousMonk (Archbishop) on Jul 25, 2018 at 13:42 UTC
    ... the union of the two shapes can contain a hole unless both polygons are concave. [emphasis added]

    Shouldn't "concave" be "convex" in the quoted passage?


    Give a man a fish:  <%-{-{-{-<

      Ooops... Thanks, fixed!
Re^2: combine(merge) polygons
by dideod.yang (Sexton) on Jul 26, 2018 at 01:12 UTC
    Thanks a lot:) I search Math::Geometry::Planar in CPAN. But I don't know which function make merge polygons. Did I miss function??? I am not used to work perl modules yet.. Can you tell me which function is merge polygon. Thanks you:)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-20 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found