0

I have shapes beside each other, some of them are pasted twice on itself, i want to figure out which object is pasted through getting the percentage of intersection of 2 objects, i coded for comparing single object with all other objects, where i'll get like 90% means there is duplicated(pasted twice)
how to find the area of intersect of Recangle2D in java ? the area of rectangle is areaRect=width * Height i tried this, but didnt work :

public void perceOf2Rect(Rectangle2D r1,Rectangle2D r2){
    Rectangle2D rectArea=new Rectangle2D.Double();
    rectArea=r1.getWidth() * r2.getHeight();
}

i am using the Rectangle2D.intersect method

 public void compareLists(List<ObjectItem01> objItm1) {
               for (int i = 0; i < objItm1.size(); i++) {
               for (int j = 0; j < objItm1.size(); j++) {
                 if (objItm1.get(i).objectID == objItm1.get(j).objectID) {
                        continue;
                }    
                 System.out.println("objID1 "+objItm1.get(i).objectID);
                 System.out.println("objID2 "+objItm1.get(j).objectID);
                 Rectangle2D rect1 =objItm1.get(i).getRect();    
                 Rectangle2D rect2 = objItm1.get(j).getRect(); 
                 Rectangle2D rectResult = new Rectangle2D.Double();
                 Rectangle2D.intersect(rect1,rect2,rectResult);  
                   testIntersectResult (rect1,rect2,rectResult);
    }}
}

in this method i got different values which means there is intersection but i want to determine the percentage of that intersection because if the intersection is 10% it is not duplicated in my case whereas 90% is duplicated

private void testIntersectResult(Rectangle2D r1, Rectangle2D r2, Rectangle2D rectDest) {
      System.out.println("beginning of testIntersectResult method");
     Rectangle2D.intersect(r1,r2,rectDest);
    System.out.println("\nRect1:\n "+r1+"\nRect2:\n "+r2+"\nrectResult:\n "+rectDest);
} 
a14
  • 31
  • 15
  • 1
    Maybe I'm misunderstanding the question, but is there something wrong with using [the built-in `Rectangle2D#intersect` method](http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Rectangle2D.html#intersect%28java.awt.geom.Rectangle2D,%20java.awt.geom.Rectangle2D,%20java.awt.geom.Rectangle2D%29)? – FThompson Feb 27 '14 at 08:12
  • I am using it, just i want to get the percentage value of each object to determine is duplicated shape or not – a14 Feb 27 '14 at 08:15
  • That would have been something to include in your question :P You can find the proportion of intersection by doing `intersectArea / (r1Area + r2Area - intersectArea)`. – FThompson Feb 27 '14 at 08:17

1 Answers1

1
public double perceOf2Rect(Rectangle2D r1, Rectangle2D r2){
    Rectangle2D r = new Rectangle2D.Double();
    Rectangle2D.intersect(r1, r2, r);

    double fr1 = r1.getWidth() * r1.getHeight();                // area of "r1"
    double f   = r.getWidth() * r.getHeight();                  // area of "r" - overlap
    return (fr1 == 0 || f <= 0) ? 0 : (f / fr1) * 100;          // overlap percentage
}
m-szalik
  • 3,546
  • 1
  • 21
  • 28
  • the percentage i got starting from 100.0 and increasing based on intersection but it's not between 0 and 100% ? – a14 Feb 27 '14 at 08:52
  • @AboAbdullah what are your parameters / rectangles? – m-szalik Feb 27 '14 at 08:59
  • You need to invert the quotient if the result > 1 (before multiplying * 100), e.g. `ratio = (fr1 == 0 || f <= 0) ? 0 : (f / fr1); if (ratio > 1) return (1/ratio) * 100`. That will keep values between 0-100 – Dan Mar 27 '17 at 21:27