Advertisement
Guest User

Calculate the area of rectangles which may overlap

a guest
Dec 27th, 2013
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. import java.awt.geom.Rectangle2D;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.NavigableSet;
  7. import java.util.SortedSet;
  8. import java.util.TreeSet;
  9.  
  10. /**
  11.  * A new approach to calculate the area of rectangles.
  12.  *
  13.  * Problem: We have a collection of Rectangle2D objects. We want to know
  14.  * the area which they occupy. We must respect that they may overlap.
  15.  *
  16.  * Solution: Assume that the rectangles are all rectangular arranged; so the minX, minY,
  17.  * maxX and maxY lines form a rectangular irregular grid. We iterate through
  18.  * all the rectangles and determine which fields of the grid are filled by the
  19.  * content of least one rectangle. In a final loop we will collect together
  20.  * all grid fields which are filled. The sum of the area of these grid fields
  21.  * will be the total area the given rectangles will occupy.
  22.  *
  23.  */
  24. public class AreaCalculator
  25. {
  26.     /**
  27.      * The calculation function. Takes a collection of Rectangle2D as argument and returns
  28.      * the occupied area
  29.      *
  30.      * @param rectangles
  31.      * @return
  32.      */
  33.     public static double getArea(Collection<Rectangle2D> rectangles)
  34.     {
  35.         // check boundaries
  36.         if (rectangles == null || rectangles.isEmpty())
  37.             return 0;
  38.         if (rectangles.size() == 1) {
  39.             Rectangle2D r = rectangles.iterator().next();
  40.             return r.getWidth() * r.getHeight();
  41.         }
  42.         // determine the grid
  43.         NavigableSet<Double> horizGridLines = new TreeSet<Double>();
  44.         NavigableSet<Double> vertGridLines = new TreeSet<Double>();
  45.         for(Rectangle2D r : rectangles) {
  46.             horizGridLines.add(r.getMinX());
  47.             horizGridLines.add(r.getMaxX());
  48.             vertGridLines.add(r.getMinY());
  49.             vertGridLines.add(r.getMaxY());
  50.         }
  51.         // store the indizes of filled grid fields in a tree set
  52.         SortedSet<GridLocation> usedLocations = new TreeSet<AreaCalculator.GridLocation>();
  53.         // iterate through the given rectangles and determine the filled grid fields
  54.         double ret = 0; // the value which will be returned
  55.         for(Rectangle2D rect : rectangles) {
  56.             NavigableSet<Double> xValues =
  57.                     horizGridLines.subSet(rect.getMinX(), true, rect.getMaxX(), true);
  58.             NavigableSet<Double> yValues =
  59.                     vertGridLines.subSet(rect.getMinY(), true, rect.getMaxY(), true);
  60.             Iterator<Double> xit = xValues.iterator();
  61.             double curX = xit.next();
  62.             while(xit.hasNext()) {
  63.                 double nextX = xit.next();
  64.                 double width = nextX - curX;
  65.                 Iterator<Double> yit = yValues.iterator();
  66.                 double curY = yit.next();
  67.                 while(yit.hasNext()) {
  68.                     double nextY = yit.next();
  69.                     double height = nextY - curY;
  70.                     if (usedLocations.add(new GridLocation(curX, curY))) {
  71.                         ret += width*height;
  72.                     }
  73.                     curY = nextY;
  74.                 }
  75.                 curX = nextX;
  76.             }
  77.         }
  78.         return ret;
  79.     }
  80.    
  81.     /**
  82.      * A help class for storing a pair of two double values in a navigable set.
  83.      */
  84.     private static class GridLocation implements Comparable<GridLocation>
  85.     {
  86.         private final double x;
  87.         private final double y;
  88.         public GridLocation(double x, double y)
  89.         {
  90.             this.x = x;
  91.             this.y = y;
  92.         }
  93.         @Override
  94.         public int compareTo(GridLocation o)
  95.         {
  96.             int order = Double.compare(x, o.x);
  97.             if (order != 0)
  98.                 return order;
  99.             return Double.compare(y, o.y);
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement