Kate
Kate

Reputation: 109

intersection and union of polygons

I have polygons defined with their vertices's, and i need to calculate areas of their union and intersection. The most upsetting thing is that it is implemented in Mapping Toolbox, but i can't buy it. Does anyone knows how to make a fast algorithm to calculate it? Thank you for your time.

Upvotes: 8

Views: 8482

Answers (4)

waTer
waTer

Reputation: 21

The idea is to break every intersect edge into four parts and form a new polygon with these. When you want the union, take the two outer edges. If you want intersection, take the two inner edges.

Upvotes: -1

Kate
Kate

Reputation: 109

I found intersection points of my polygons and added vertices that are inside/outside polygons for intersection/union task (check if any of vertices of polygon 1 lies inside a polygon 2 and vice versa using 'inpolygon'). Then all points were transformed into polar coordinates with center in the mean coordinates of the matrix and sorted by angle, so that now they form consecutive closed contour. Knowing this it is easy to find intersection/union area using 'polyarea'.

Upvotes: -1

Jacob
Jacob

Reputation: 34601

You just need to find the area of intersection ; the area of the union is trivially obtained from that. The PolygonIntersection package from FEX might be useful.

enter image description here

Upvotes: 3

aioobe
aioobe

Reputation: 420971

I would do like this:

  1. Let S be the set of vertices from both polygons.
  2. For each edge e1 in polygon 1
    1. For each edge e2 in polygon 2
      1. If e1 intersects with e2
        1. Add the intersection point to S
  3. Remove all vertices in S that are inside polygon 1 or 2.

The resulting set of vertices should make up the union of the polygons.

For the intersection you simply remove all vertices in S that are outside of both polygon 1 and 2 (in the third step).

(You can look up point intersection and "inside-polygon"-checks elsewhere ;-)

Upvotes: 1

Related Questions