Reputation: 31
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);
}
Upvotes: 0
Views: 978
Reputation: 3654
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
}
Upvotes: 1