Algorithm 找到重叠矩形区域的有效算法是什么 我的处境 输入:一组矩形 每个矩形由如下4个双精度组成:(x0,y0,x1,y1) 它们不会以任何角度“旋转”,它们都是相对于屏幕“上/下”和“左/右”的“正常”矩形 它们是随机放置的-它们可能在边缘接触、重叠或没有任何接触 我将有几百个矩形 这是用C语言实现的# 我需要找到 由其重叠形成的区域-画布中多个矩形“覆盖”的所有区域(例如,两个矩形的交点) 我不需要重叠的几何图形,只需要面积(例如:4平方英寸) 重叠不应该被计数多次-例如,想象3个具有相同大小和位置的矩形-它们正好位于彼此的顶部-该区域应该被计数一次(而不是三次) 例子 下图包含三个矩形:A、B、C A和B重叠(如虚线所示) B和C重叠(如虚线所示) 我要找的是显示破折号的区域

Algorithm 找到重叠矩形区域的有效算法是什么 我的处境 输入:一组矩形 每个矩形由如下4个双精度组成:(x0,y0,x1,y1) 它们不会以任何角度“旋转”,它们都是相对于屏幕“上/下”和“左/右”的“正常”矩形 它们是随机放置的-它们可能在边缘接触、重叠或没有任何接触 我将有几百个矩形 这是用C语言实现的# 我需要找到 由其重叠形成的区域-画布中多个矩形“覆盖”的所有区域(例如,两个矩形的交点) 我不需要重叠的几何图形,只需要面积(例如:4平方英寸) 重叠不应该被计数多次-例如,想象3个具有相同大小和位置的矩形-它们正好位于彼此的顶部-该区域应该被计数一次(而不是三次) 例子 下图包含三个矩形:A、B、C A和B重叠(如虚线所示) B和C重叠(如虚线所示) 我要找的是显示破折号的区域,algorithm,optimization,geometry,performance,Algorithm,Optimization,Geometry,Performance,- 您可以找到x轴和y轴上的重叠,并将它们相乘 int LineOverlap(int line1a, line1b, line2a, line2b) { // assume line1a <= line1b and line2a <= line2b if (line1a < line2a) { if (line1b > line2b) return line2b-line2a; else if (line1b > lin

-


您可以找到x轴和y轴上的重叠,并将它们相乘

int LineOverlap(int line1a, line1b, line2a, line2b) 
{
  // assume line1a <= line1b and line2a <= line2b
  if (line1a < line2a) 
  {
    if (line1b > line2b)
      return line2b-line2a;
    else if (line1b > line2a)
      return line1b-line2a;
    else 
      return 0;
  }
  else if (line2a < line1b)
    return line2b-line1a;
  else 
    return 0;
}


int RectangleOverlap(Rect rectA, rectB) 
{
  return LineOverlap(rectA.x1, rectA.x2, rectB.x1, rectB.x2) *
    LineOverlap(rectA.y1, rectA.y2, rectB.y1, rectB.y2);
}
int-LineOverlap(int-line1a、line1b、line2a、line2b)
{
//假设第1a行(第2a行)
回流管1B-2A;
其他的
返回0;
}
否则如果(第2a行<第1b行)
回流管2B-1A;
其他的
返回0;
}
int矩形重叠(Rect rectA,rectB)
{
返回线重叠(rectA.x1、rectA.x2、rectB.x1、rectB.x2)*
线重叠(rectA.y1、rectA.y2、rectB.y1、rectB.y2);
}

一种解决方法是将其绘制到画布上!使用半透明颜色绘制每个矩形。NET运行时将使用优化的本机代码进行绘图,甚至使用硬件加速器

然后,你必须读回像素。每个像素是背景色、矩形色还是其他颜色?如果两个或多个矩形重叠,它就可以成为另一种颜色


如果这是一个太多的欺骗,我会推荐四叉树作为另一个回答,或。

这是一些快速和肮脏的代码,我在TopCoder SRM 160 Div 2中使用

t=顶部
b=博特托姆
l=左
r=右

public class Rect
{
    public int t, b, l, r;

    public Rect(int _l, int _b, int _r, int _t)
    {
        t = _t;
        b = _b;
        l = _l;
        r = _r;
    }   

    public bool Intersects(Rect R)
    {
        return !(l > R.r || R.l > r || R.b > t || b > R.t);
    }

    public Rect Intersection(Rect R)
    {
        if(!this.Intersects(R))
            return new Rect(0,0,0,0);
        int [] horiz = {l, r, R.l, R.r};
        Array.Sort(horiz);
        int [] vert = {b, t, R.b, R.t};
        Array.Sort(vert);

        return new Rect(horiz[1], vert[1], horiz[2], vert[2]);
    } 

    public int Area()
    {
        return (t - b)*(r-l);
    }

    public override string ToString()
    {
        return l + " " + b + " " + r + " " + t;
    }
}

如果您的矩形是稀疏的(大部分不是相交的),那么值得一看递归维度集群。否则,四叉树似乎是最好的选择(正如其他海报所提到的那样)

这是电脑游戏中碰撞检测中的常见问题,因此不缺乏解决方法的资源

这是一篇很好的总结RCD的博客文章


这是一篇Dobbs博士的文章,总结了各种适合的碰撞检测算法。

这种类型的碰撞检测通常被称为AABB(轴对齐边界框),这是一个很好的起点。

下面是一些我脑海中听上去可能有用的东西:

  • 创建一个具有双键和矩形+布尔值列表的字典,如下所示:

    0. empty set, zero sum
    1. enter a, add a to set (1 rectangle in set)
    2. enter d, add d to set (>1 rectangles in set = overlap, store this y-coordinate)
    3. leave a, remove a from set (now back from >1 rectangles in set, add to sum: y - stored_y
    4. enter c, add c to set (>1 rectangles in set = overlap, store this y-coordinate)
    5. leave d, remove d from set (now back from >1 rectangles in set, add to sum: y - stored_y)
    6. multiply sum with width of strip to get overlapping areas
    
    字典>>矩形

  • 对于集合中的每个矩形,找到x0和x1值的对应列表,并将该矩形添加到该列表中,其中x0的布尔值为true,x1的布尔值为false。这样,您现在就拥有了每个矩形输入(true)或离开(false)x方向的所有x坐标的完整列表

  • 从该字典中获取所有键(所有不同的x坐标),对它们进行排序,并按顺序循环,确保可以同时获得当前x值和下一个x值(两者都需要)。这将为您提供单独的矩形条

  • 维护一组当前正在查看的矩形,这些矩形一开始为空。对于在第3点中迭代的每个x值,如果矩形注册为真值,则将其添加到该矩形集中,否则将其删除
  • 对于条形图,按矩形的y坐标对其进行排序
  • 在条带中的矩形中循环,计算重叠距离(我还不清楚如何有效地进行此操作)
  • 计算条带宽度乘以重叠距离的高度以获得面积
  • 例如,5个矩形,从a到e相互重叠绘制:

    aaaaaaaaaaaaaaaa          bbbbbbbbbbbbbbbbb
    aaaaaaaaaaaaaaaa          bbbbbbbbbbbbbbbbb
    aaaaaaaaaaaaaaaa          bbbbbbbbbbbbbbbbb
    aaaaaaaaaaaaaaaa          bbbbbbbbbbbbbbbbb
    aaaaaaaadddddddddddddddddddddddddddddbbbbbb
    aaaaaaaadddddddddddddddddddddddddddddbbbbbb
            ddddddddddddddddddddddddddddd
            ddddddddddddddddddddddddddddd
            ddddddddddddddeeeeeeeeeeeeeeeeee
            ddddddddddddddeeeeeeeeeeeeeeeeee
            ddddddddddddddeeeeeeeeeeeeeeeeee
    ccccccccddddddddddddddeeeeeeeeeeeeeeeeee
    ccccccccddddddddddddddeeeeeeeeeeeeeeeeee
    cccccccccccc          eeeeeeeeeeeeeeeeee
    cccccccccccc          eeeeeeeeeeeeeeeeee
    cccccccccccc
    cccccccccccc
    
    以下是x坐标列表:

    v       v  v   v      v   v         v  v  v   
    |aaaaaaa|aa|aaaa      |   bbbbbbbbbb|bb|bbb
    |aaaaaaa|aa|aaaa      |   bbbbbbbbbb|bb|bbb
    |aaaaaaa|aa|aaaa      |   bbbbbbbbbb|bb|bbb
    |aaaaaaa|aa|aaaa      |   bbbbbbbbbb|bb|bbb
    |aaaaaaaddd|dddddddddd|ddddddddddddddbb|bbb
    |aaaaaaaddd|dddddddddd|ddddddddddddddbb|bbb
    |       ddd|dddddddddd|dddddddddddddd  |
    |       ddd|dddddddddd|dddddddddddddd  |
    |       ddd|ddddddddddeeeeeeeeeeeeeeeeee
    |       ddd|ddddddddddeeeeeeeeeeeeeeeeee
    |       ddd|ddddddddddeeeeeeeeeeeeeeeeee
    ccccccccddd|ddddddddddeeeeeeeeeeeeeeeeee
    ccccccccddd|ddddddddddeeeeeeeeeeeeeeeeee
    cccccccccccc          eeeeeeeeeeeeeeeeee
    cccccccccccc          eeeeeeeeeeeeeeeeee
    cccccccccccc
    cccccccccccc
    
    该列表将是(其中每个v仅给出一个从0开始向上的坐标):

    因此,每个条带将是(从上到下排序的矩形):

    对于每条带,重叠部分应为:

    0-1: none
    1-2: a/d, d/c
    2-3: a/d
    3-4: none
    4-5: d/e
    5-6: b/d, d/e
    6-7: none
    7-8: none
    
    我可以想象,用于上下检查的sort+enter/leave算法的变体也是可行的:

  • 对我们当前在条带中分析的矩形进行排序,从上到下,对于具有相同顶部坐标的矩形,也按底部坐标进行排序
  • 迭代y坐标,当您输入矩形时,将其添加到集合中;当您离开矩形时,将其从集合中移除
  • 每当集合中有多个矩形时,就有重叠(如果确保添加/删除所有具有当前查看的相同上/下坐标的矩形,则多个重叠矩形不会有问题)
  • 对于上面的1-2条,您可以这样迭代:

    0. empty set, zero sum
    1. enter a, add a to set (1 rectangle in set)
    2. enter d, add d to set (>1 rectangles in set = overlap, store this y-coordinate)
    3. leave a, remove a from set (now back from >1 rectangles in set, add to sum: y - stored_y
    4. enter c, add c to set (>1 rectangles in set = overlap, store this y-coordinate)
    5. leave d, remove d from set (now back from >1 rectangles in set, add to sum: y - stored_y)
    6. multiply sum with width of strip to get overlapping areas
    
    实际上,你也不需要在这里维护一个实际的集合,只要你在里面的矩形的计数,每当它从1变为2,存储y,每当它从2变为1,计算当前y存储的y,并求和这个差值


    希望这是可以理解的,正如我所说的,这是我脑子里想不出来的,没有经过任何测试。

    如果你拆分每个矩形,你可以简化这个问题
    0-1: none
    1-2: a/d, d/c
    2-3: a/d
    3-4: none
    4-5: d/e
    5-6: b/d, d/e
    6-7: none
    7-8: none
    
    0. empty set, zero sum
    1. enter a, add a to set (1 rectangle in set)
    2. enter d, add d to set (>1 rectangles in set = overlap, store this y-coordinate)
    3. leave a, remove a from set (now back from >1 rectangles in set, add to sum: y - stored_y
    4. enter c, add c to set (>1 rectangles in set = overlap, store this y-coordinate)
    5. leave d, remove d from set (now back from >1 rectangles in set, add to sum: y - stored_y)
    6. multiply sum with width of strip to get overlapping areas
    
    1 2 3 4 5 6 1 +---+---+ | | 2 + A +---+---+ | | B | 3 + + +---+---+ | | | | | 4 +---+---+---+---+ + | | 5 + C + | | 6 +---+---+ 1 3 4 5 6 1 2 3 4 6 4 * 4 1 3 4 5 6 1 +---+ | 1 | 0 0 0 2 +---+---+---+ | 1 | 1 | 1 | 0 3 +---+---+---+---+ | 1 | 1 | 2 | 1 | 4 +---+---+---+---+ 0 0 | 1 | 1 | 6 +---+---+
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    
    class Rectangle {
    public:
        int x[2], y[2];
    
        Rectangle(int x1, int y1, int x2, int y2) {
            x[0] = x1;
            y[0] = y1;
            x[1] = x2;
            y[1] = y2; 
        };
        void print(void) {
            cout << "Rect: " << x[0] << " " << y[0] << " " << x[1] << " " << y[1] << " " <<endl;
        };
    };
    
    // return the iterator of rec in list
    vector<Rectangle *>::iterator bin_search(vector<Rectangle *> &list, int begin, int end, Rectangle *rec) {
        cout << begin << " " <<end <<endl;
        int mid = (begin+end)/2;
        if (list[mid]->y[0] == rec->y[0]) {
            if (list[mid]->y[1] == rec->y[1])
                return list.begin() + mid;
            else if (list[mid]->y[1] < rec->y[1]) {
                if (mid == end)
                    return list.begin() + mid+1;
                return bin_search(list,mid+1,mid,rec);
            }
            else {
                if (mid == begin)
                    return list.begin()+mid;
                return bin_search(list,begin,mid-1,rec);
            }
        }
        else if (list[mid]->y[0] < rec->y[0]) {
            if (mid == end) {
                return list.begin() + mid+1;
            }
            return bin_search(list, mid+1, end, rec);
        }
        else {
            if (mid == begin) {
                return list.begin() + mid;
            }
            return bin_search(list, begin, mid-1, rec);
        }
    }
    
    // add rect to rects
    void add_rec(Rectangle *rect, vector<Rectangle *> &rects) {
        if (rects.size() == 0) {
            rects.push_back(rect);
        }
        else {
            vector<Rectangle *>::iterator it = bin_search(rects, 0, rects.size()-1, rect);
            rects.insert(it, rect);
        }
    }
    
    // remove rec from rets
    void remove_rec(Rectangle *rect, vector<Rectangle *> &rects) {
        vector<Rectangle *>::iterator it = bin_search(rects, 0, rects.size()-1, rect);
        rects.erase(it);
    }
    
    // calculate the total vertical length covered by rectangles in the active set
    int vert_dist(vector<Rectangle *> as) {
        int n = as.size();
    
        int totallength = 0;
        int start, end;
    
        int i = 0;
        while (i < n) {
            start = as[i]->y[0];
            end = as[i]->y[1];
            while (i < n && as[i]->y[0] <= end) {
                if (as[i]->y[1] > end) {
                    end = as[i]->y[1];
                }
                i++;
            }
            totallength += end-start;
        }
        return totallength;
    }
    
    bool mycomp1(Rectangle* a, Rectangle* b) {
        return (a->x[0] < b->x[0]);
    }
    
    bool mycomp2(Rectangle* a, Rectangle* b) {
        return (a->x[1] < b->x[1]);
    }
    
    int findarea(vector<Rectangle *> rects) {
        vector<Rectangle *> start = rects;
        vector<Rectangle *> end = rects;
        sort(start.begin(), start.end(), mycomp1);
        sort(end.begin(), end.end(), mycomp2);
    
        // active set
        vector<Rectangle *> as;
    
        int n = rects.size();
    
        int totalarea = 0;
        int current = start[0]->x[0];
        int next;
        int i = 0, j = 0;
        // big loop
        while (j < n) {
            cout << "loop---------------"<<endl;
            // add all recs that start at current
            while (i < n && start[i]->x[0] == current) {
                cout << "add" <<endl;
                // add start[i] to AS
                add_rec(start[i], as);
                cout << "after" <<endl;
                i++;
            }
            // remove all recs that end at current
            while (j < n && end[j]->x[1] == current) {
                cout << "remove" <<endl;
                // remove end[j] from AS
                remove_rec(end[j], as);
                cout << "after" <<endl;
                j++;
            }
    
            // find next event x
            if (i < n && j < n) {
                if (start[i]->x[0] <= end[j]->x[1]) {
                    next = start[i]->x[0];
                }
                else {
                    next = end[j]->x[1];
                }
            }
            else if (j < n) {
                next = end[j]->x[1];
            }
    
            // distance to next event
            int horiz = next - current;
            cout << "horiz: " << horiz <<endl;
    
            // figure out vertical dist
            int vert = vert_dist(as);
            cout << "vert: " << vert <<endl;
    
            totalarea += vert * horiz;
    
            current = next;
        }
        return totalarea;
    }
    
    int main() {
        vector<Rectangle *> rects;
        rects.push_back(new Rectangle(0,0,1,1));
    
        rects.push_back(new Rectangle(1,0,2,3));
    
        rects.push_back(new Rectangle(0,0,3,3));
    
        rects.push_back(new Rectangle(1,0,5,1));
    
        cout << findarea(rects) <<endl;
    }
    
    GridLocation gl = new GridLocation(curX, curY);
    if(usedLocations.contains(gl) && usedLocations2.add(gl)) {
      ret += width*height;
    } else {
      usedLocations.add(gl);
    }
    
    import numpy as np
    
    A = np.zeros((100, 100))
    B = np.zeros((100, 100))
    
    A[rect1.top : rect1.bottom,  rect1.left : rect1.right] = 1
    B[rect2.top : rect2.bottom,  rect2.left : rect2.right] = 1
    
    area_of_union     = np.sum((A + B) > 0)
    area_of_intersect = np.sum((A + B) > 1)
    
    import java.io.*;
    import java.util.*;
    
    class Solution {
    
    static class Rectangle{
             int x;
             int y;
             int dx;
             int dy;
    
             Rectangle(int x, int y, int dx, int dy){
               this.x = x;
               this.y = y;
               this.dx = dx;
               this.dy = dy;
             }
    
             Range getBottomLeft(){
                return new Range(x, y);
             }
    
             Range getTopRight(){
                return new Range(x + dx, y + dy);
             }
    
             @Override
             public int hashCode(){
                return (x+y+dx+dy)/4;
             }
    
             @Override
             public boolean equals(Object other){
                Rectangle o = (Rectangle) other;
                return o.x == this.x && o.y == this.y && o.dx == this.dx && o.dy == this.dy;
             }
    
            @Override
            public String toString(){
                return String.format("X = %d, Y = %d, dx : %d, dy : %d", x, y, dx, dy);
            }
         }     
    
         static class RW{
             Rectangle r;
             boolean start;
    
             RW (Rectangle r, boolean start){
               this.r = r;
               this.start = start;
             }
    
             @Override
             public int hashCode(){
                 return r.hashCode() + (start ? 1 : 0);
             }
    
             @Override
             public boolean equals(Object other){
                  RW o = (RW)other;
                 return o.start == this.start && o.r.equals(this.r);
             }
    
            @Override
            public String toString(){
                return "Rectangle : " + r.toString() + ", start = " + this.start;
            }
         }
    
         static class Range{
             int l;
             int u;   
    
           public Range(int l, int u){
             this.l = l;
             this.u = u;
           }
    
             @Override
             public int hashCode(){
                return (l+u)/2;
             }
    
             @Override
             public boolean equals(Object other){
                Range o = (Range) other;
                return o.l == this.l && o.u == this.u;
             }
    
            @Override
            public String toString(){
                return String.format("L = %d, U = %d", l, u);
            }
         }
    
         static class XComp implements Comparator<RW>{
                 @Override
                 public int compare(RW rw1, RW rw2){
                     //TODO : revisit these values.
                     Integer x1 = -1;
                     Integer x2 = -1;
    
                     if(rw1.start){
                         x1 = rw1.r.x;
                     }else{
                         x1 = rw1.r.x + rw1.r.dx;
                     }   
    
                     if(rw2.start){
                         x2 = rw2.r.x;
                     }else{
                         x2 = rw2.r.x + rw2.r.dx;
                     }
    
                     return x1.compareTo(x2);
                 }
         }
    
         static class YComp implements Comparator<RW>{
                 @Override
                 public int compare(RW rw1, RW rw2){
                     //TODO : revisit these values.
                     Integer y1 = -1;
                     Integer y2 = -1;
    
                     if(rw1.start){
                         y1 = rw1.r.y;
                     }else{
                         y1 = rw1.r.y + rw1.r.dy;
                     }   
    
                     if(rw2.start){
                         y2 = rw2.r.y;
                     }else{
                         y2 = rw2.r.y + rw2.r.dy;
                     }
    
                     return y1.compareTo(y2);
                 }
         }
    
         public static void main(String []args){
             Rectangle [] rects = new Rectangle[4];
    
             rects[0] = new Rectangle(10, 10, 10, 10);
             rects[1] = new Rectangle(15, 10, 10, 10);
             rects[2] = new Rectangle(20, 10, 10, 10);
             rects[3] = new Rectangle(25, 10, 10, 10);
    
             int totalArea = getArea(rects, false);
             System.out.println("Total Area : " + totalArea);
    
             int overlapArea = getArea(rects, true);              
             System.out.println("Overlap Area : " + overlapArea);
         }
    
    
         static int getArea(Rectangle []rects, boolean overlapOrTotal){
             printArr(rects);
    
             // step 1: create two wrappers for every rectangle
             RW []rws = getWrappers(rects);       
    
             printArr(rws);        
    
             // steps 2 : sort rectangles by their x-coordinates
             Arrays.sort(rws, new XComp());   
    
             printArr(rws);        
    
             // step 3 : group the rectangles in every range.
             Map<Range, List<Rectangle>> rangeGroups = groupRects(rws, true);
    
             for(Range xrange : rangeGroups.keySet()){
                 List<Rectangle> xRangeRects = rangeGroups.get(xrange);
                 System.out.println("Range : " + xrange);
                 System.out.println("Rectangles : ");
                 for(Rectangle rectx : xRangeRects){
                    System.out.println("\t" + rectx);               
                 }
             }   
    
             // step 4 : iterate through each of the pairs and their rectangles
    
             int sum = 0;
             for(Range range : rangeGroups.keySet()){
                 List<Rectangle> rangeRects = rangeGroups.get(range);
                 sum += getOverlapOrTotalArea(rangeRects, range, overlapOrTotal);
             }
             return sum;         
         }    
    
         static Map<Range, List<Rectangle>> groupRects(RW []rws, boolean isX){
             //group the rws with either x or y coordinates.
    
             Map<Range, List<Rectangle>> rangeGroups = new HashMap<Range, List<Rectangle>>();
    
             List<Rectangle> rangeRects = new ArrayList<Rectangle>();            
    
             int i=0;
             int prev = Integer.MAX_VALUE;
    
             while(i < rws.length){
                 int curr = isX ? (rws[i].start ? rws[i].r.x : rws[i].r.x + rws[i].r.dx): (rws[i].start ? rws[i].r.y : rws[i].r.y + rws[i].r.dy);
    
                 if(prev < curr){
                    Range nRange = new Range(prev, curr);
                    rangeGroups.put(nRange, rangeRects);
                    rangeRects = new ArrayList<Rectangle>(rangeRects);
                 }
                 prev = curr;
    
                 if(rws[i].start){
                   rangeRects.add(rws[i].r);
                 }else{
                   rangeRects.remove(rws[i].r);
                 }
    
               i++;
             }
           return rangeGroups;
         }
    
         static int getOverlapOrTotalArea(List<Rectangle> rangeRects, Range range, boolean isOverlap){
             //create horizontal sweep lines similar to vertical ones created above
    
             // Step 1 : create wrappers again
             RW []rws = getWrappers(rangeRects);
    
             // steps 2 : sort rectangles by their y-coordinates
             Arrays.sort(rws, new YComp());
    
             // step 3 : group the rectangles in every range.
             Map<Range, List<Rectangle>> yRangeGroups = groupRects(rws, false);
    
             //step 4 : for every range if there are more than one rectangles then computer their area only once.
    
             int sum = 0;
             for(Range yRange : yRangeGroups.keySet()){
                 List<Rectangle> yRangeRects = yRangeGroups.get(yRange);
    
                 if(isOverlap){
                     if(yRangeRects.size() > 1){
                         sum += getArea(range, yRange);
                     }
                 }else{
                     if(yRangeRects.size() > 0){
                         sum += getArea(range, yRange);
                     }
                 }
             }         
             return sum;
         } 
    
        static int getArea(Range r1, Range r2){
          return (r2.u-r2.l)*(r1.u-r1.l);      
        }
    
        static RW[] getWrappers(Rectangle []rects){
             RW[] wrappers = new RW[rects.length * 2];
    
             for(int i=0,j=0;i<rects.length;i++, j+=2){
                 wrappers[j] = new RW(rects[i], true); 
                 wrappers[j+1] = new RW(rects[i], false); 
             }
             return wrappers;
         }
    
        static RW[] getWrappers(List<Rectangle> rects){
             RW[] wrappers = new RW[rects.size() * 2];
    
             for(int i=0,j=0;i<rects.size();i++, j+=2){
                 wrappers[j] = new RW(rects.get(i), true); 
                 wrappers[j+1] = new RW(rects.get(i), false); 
             }
             return wrappers;
         }
    
      static void printArr(Object []a){
        for(int i=0; i < a.length;i++){
          System.out.println(a[i]);
        }
        System.out.println();
      }     
    
        #include <iostream>
    using namespace std;
    
    int rectoverlap (int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2)
    {
        int width, heigh, area;
    
        if (ax2<bx1 || ay2<by1 || ax1>bx2 || ay1>by2) {
            cout << "Rectangles are not overlapped" << endl;
            return 0;
        }
        if (ax2>=bx2 && bx1>=ax1){
            width=bx2-bx1;
            heigh=by2-by1;
        } else if (bx2>=ax2 && ax1>=bx1) {
            width=ax2-ax1;
            heigh=ay2-ay1;
        } else {
            if (ax2>bx2){
                width=bx2-ax1;
            } else {
                width=ax2-bx1;
            }
            if (ay2>by2){
                heigh=by2-ay1;
            } else {
                heigh=ay2-by1;
            }
        }
        area= heigh*width;
        return (area);
    }
    
    int main()
    {
        int ax1,ay1,ax2,ay2,bx1,by1,bx2,by2;
        cout << "Inter the x value for bottom left for rectangle A" << endl;
        cin >> ax1;
        cout << "Inter the y value for bottom left for rectangle A" << endl;
        cin >> ay1;
        cout << "Inter the x value for top right for rectangle A" << endl;
        cin >> ax2;
        cout << "Inter the y value for top right for rectangle A" << endl;
        cin >> ay2;
        cout << "Inter the x value for bottom left for rectangle B" << endl;
        cin >> bx1;
        cout << "Inter the y value for bottom left for rectangle B" << endl;
        cin >> by1;
        cout << "Inter the x value for top right for rectangle B" << endl;
        cin >> bx2;
        cout << "Inter the y value for top right for rectangle B" << endl;
        cin >> by2;
        cout << "The overlapped area is " <<  rectoverlap (ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) << endl;
    }
    
    var totArea = rects.Sum(x => x.Width * x.Height);
    
    var overlappingArea =totArea-GetArea(rects)
    
       #region rectangle overlapping
            /// <summary>
            /// see algorithm for detecting overlapping areas here: https://stackoverflow.com/a/245245/3225391
            /// or easier here:
            /// http://codercareer.blogspot.co.il/2011/12/no-27-area-of-rectangles.html
            /// </summary>
            /// <param name="dim"></param>
            /// <returns></returns>
            public static float GetArea(RectangleF[] rects)
            {
                List<float> xs = new List<float>();
                foreach (var item in rects)
                {
                    xs.Add(item.X);
                    xs.Add(item.Right);
                }
                xs = xs.OrderBy(x => x).Cast<float>().ToList();
                rects = rects.OrderBy(rec => rec.X).Cast<RectangleF>().ToArray();
                float area = 0f;
                for (int i = 0; i < xs.Count - 1; i++)
                {
                    if (xs[i] == xs[i + 1])//not duplicate
                        continue;
                    int j = 0;
                    while (rects[j].Right < xs[i])
                        j++;
                    List<Range> rangesOfY = new List<Range>();
                    var rangeX = new Range(xs[i], xs[i + 1]);
                    GetRangesOfY(rects, j, rangeX, out rangesOfY);
                    area += GetRectArea(rangeX, rangesOfY);
                }
                return area;
            }
    
            private static void GetRangesOfY(RectangleF[] rects, int rectIdx, Range rangeX, out List<Range> rangesOfY)
            {
                rangesOfY = new List<Range>();
                for (int j = rectIdx; j < rects.Length; j++)
                {
                    if (rangeX.less < rects[j].Right && rangeX.greater > rects[j].Left)
                    {
                        rangesOfY = Range.AddRange(rangesOfY, new Range(rects[j].Top, rects[j].Bottom));
    #if DEBUG
                        Range rectXRange = new Range(rects[j].Left, rects[j].Right);
    #endif
                    }
                }
            }
    
            static float GetRectArea(Range rangeX, List<Range> rangesOfY)
            {
                float width = rangeX.greater - rangeX.less,
                    area = 0;
    
                foreach (var item in rangesOfY)
                {
                    float height = item.greater - item.less;
                    area += width * height;
                }
                return area;
            }
    
            internal class Range
            {
                internal static List<Range> AddRange(List<Range> lst, Range rng2add)
                {
                    if (lst.isNullOrEmpty())
                    {
                        return new List<Range>() { rng2add };
                    }
    
                    for (int i = lst.Count - 1; i >= 0; i--)
                    {
                        var item = lst[i];
                        if (item.IsOverlapping(rng2add))
                        {
                            rng2add.Merge(item);
                            lst.Remove(item);
                        }
                    }
                    lst.Add(rng2add);
                    return lst;
                }
                internal float greater, less;
                public override string ToString()
                {
                    return $"ln{less} gtn{greater}";
                }
    
                internal Range(float less, float greater)
                {
                    this.less = less;
                    this.greater = greater;
                }
    
                private void Merge(Range rng2add)
                {
                    this.less = Math.Min(rng2add.less, this.less);
                    this.greater = Math.Max(rng2add.greater, this.greater);
                }
                private bool IsOverlapping(Range rng2add)
                {
                    return !(less > rng2add.greater || rng2add.less > greater);
                    //return
                    //    this.greater < rng2add.greater && this.greater > rng2add.less
                    //    || this.less > rng2add.less && this.less < rng2add.greater
    
                    //    || rng2add.greater < this.greater && rng2add.greater > this.less
                    //    || rng2add.less > this.less && rng2add.less < this.greater;
                }
            }
            #endregion rectangle overlapping
    
    int rectoverlap (int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2)
    {
        int width, height, area;
    
        if (ax2<bx1 || ay2<by1 || ax1>bx2 || ay1>by2) {
            cout << "Rectangles are not overlapped" << endl;
            return 0;
        }
    
        if (ax2>=bx2 && bx1>=ax1){
            width=bx2-bx1;
        } else if (bx2>=ax2 && ax1>=bx1) {
            width=ax2-ax1;
        } else if (ax2>bx2) {
            width=bx2-ax1;
        } else {
            width=ax2-bx1;
        }
    
        if (ay2>=by2 && by1>=ay1){
            height=by2-by1;
        } else if (by2>=ay2 && ay1>=by1) {
            height=ay2-ay1;
        } else if (ay2>by2) {
            height=by2-ay1;
        } else {
            height=ay2-by1;
        }
    
        area = heigh*width;
        return (area);
    }