2

I have a polygon, it has five points like this: enter image description here

then I add another point to polygon (the red one): enter image description here

what's the algorithm to determine two polygons is same one (not just angle/length is same, coordinates also same too).

hzqij1978
  • 263
  • 2
  • 9
  • 1
    Check [this post](http://stackoverflow.com/questions/22159897/how-to-compare-two-shapes) to get the idea how to start. Then here is [this document](http://www.cs.princeton.edu/courses/archive/spr00/cs598b/lectures/polygonsimilarity/polygonsimilarity.pdf) that explains few methods you can use to compare two polygons. – Tim Rijavec Jun 02 '15 at 08:09

3 Answers3

1

As your same means same shape,size,orientation and position

  • then it is really simple
  • you have 2 polygons defined as set of points
  • A={ a0,a1...a(n-1) } and B={ b0,b1,...b(m-1) }

for starters I assume you have no oversampling (line is always 2 points not more)

  1. compare m,n

    • if not equal shapes are different so stop
    • otherwise m==n so I will use just n from now on
  2. find (a(i)==b(j)) where i,j=<0,n)

    • this is needed in case the polygons are not starting from the same point
    • otherwise i=0,j=0
    • for complicated (self intersecting) shapes you need to find unique points
    • (not duplicates, or the same count of duplicates with the same next point)
    • otherwise just set i=0 and find j with single O(n) loop
    • if no common point found stop (not the same polygons)
  3. compare the points

        for (k=0;k<n;k++)
         {
         if (a(i)!=b(j)) return false; // not the same
         i++; if (i>=n) i=0;
         j++; if (j>=n) j=0;
         } return true; // are the same
    
    • the point comparison can be done like this if (|a(i)-b(j)|>=max_difference_treshold)
    • no need to compare sqrt-ed distances the treshold can be powered by 2 instead
    • I usually use something like 1e-6 or 1e-10 values

For oversampled polygon you need to resample points of booth A,B first

  • take 3 neighboring points p(i-1),p(i),p(i+1)
  • compute dx,dy between 2 pairs
  • d1=p(i)-p(i-1); dx1=p1.x; dy1=p1.y;
  • d2=p(i+1)-p(i); dx2=p2.x; dy2=p2.y;
  • if (dx1*dy2==dx1*dy1) then delete p(i) from the set
  • you should handle the zero cases (any dx,dy is zero) separately prior to this
Spektre
  • 49,595
  • 11
  • 110
  • 380
0
 //import turf library
 var turf = require('@turf/turf');

 // create polygon using turf or you can directly use geoJSON
 var polygon1 = turf.polygon(firstPolygonCoordinates);
 var polygon2 = turf.polygon(secondPolygonCoordinates);

 // Compare two polygon using turf's booleanEqual method
 if (turf.booleanEqual(polygon1, polygon2)) {
    // Add your logic here
 }
Amit Gaikwad
  • 516
  • 4
  • 12
-1

Depending on your point of view. Two rectangles may be the same independently of the position.

  • If the position is a requirement you will need to compare the vertex coordinates.
  • If the position is NOT a requirement, you should compare the rectangle size. In this case, you need to obtain the distance between the vertex. In this case, my advice is you should use the rectangle base and height for comparing.
kiuby_88
  • 334
  • 1
  • 6
  • 18
  • this does not answer the OP question he needs to compare polygons with different number of points and the position is a requirement. so simple point comparison or bounding box is far from enough – Spektre Jun 02 '15 at 10:27