Covered area problem is a geometry problem represented in Cartesian product. Many rectangles are located in a two dimensional environment. Each rectangle has its upper-left coordinate and lower-right coordinate to indicate its position. One rectangle can be placed separated from other rectangles, can be placed inside some others, or can be placed where part of it overlaps other rectangles.

Covered Area Problem is a classic algorithm problem used for academic teaching purposes. The basic idea is to give several pairs of Cartesian coordinates as the input. Each pair of coordinate represents the upper-left corner and lower-right corner of a rectangle. The goal of the system is to find out the total area covered by all those rectangles. Please note that we can not just add all the covered area of each rectangles since the rectangles can be overlapped each other in any order. Common solution usually implements two dimensional flag mapping. Each rectangle will map several flags covered by it into value 1 (covered). If the flag in a location is already 1, then no change needed. At the end of the process, the program will count how many flags have value 1 and that would be the answer.

This common solution has several weaknesses :
1. It consumes a huge size of memory, larger Cartesian coordinates can not be calculated.
2. Two dimensional array is very difficult to be implemented in logic and functional programming.

The algorithm used in this article will work on the all coordinates of integer numbers, meaning the top-left coordinate is (-32768,32767) and the lower-right coordinate is (32767,-32768). This algorithm will divide the region into 4 equal subsections. Instead of marking flags in a two dimensional array (65526 x 65536), this algorithm will compare each subsection with the rectangles. When a subsection is totally uncovered, the value 0 will be given. When a subsection is totally covered, the value 1 will be given. When a subsection is partially covered, the value 2 will be given and that subsection will be divided into 4 smaller equal subsections, and the same calculation will be performed recursively.

[toggle title=”C++ version”]

```#include
#include
#include

int CompareLine(int a,int b,int c,int d)
{
int pos=0;
if (c&lt;a)
{
if (d=a) pos=0; else if ((d&gt;a) &amp;&amp; (d&lt;b)) pos=2; else pos=1;
}
else if (c==a)
{
if (d&gt;=b) pos=1; else pos=2;
}
else if ((c&gt;a) &amp;&amp; (c&lt;b)) pos=2;
return pos;
}

int CompareTwoAreas(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
int dx; int dy;
dx=CompareLine(x1,x2,x3,x4);
dy=CompareLine(y2,y1,y4,y3);
if ((dx==0) || (dy==0)) return 0;
else if ((dx==1) &amp;&amp; (dy==1)) return 1;
else return 2;
}

class cTree
{
char  isCovered;
int   x1,y1,x2,y2;
cTree * ChildArea;
public:
cTree(void);
void Init(int vx1, int vy1, int vx2, int vy2);
void AddRect(int vx1, int vy1, int vx2, int vy2);
long CountArea();
};

cTree::cTree (void)
{
isCovered=0;
}

void cTree::Init(int vx1, int vy1, int vx2, int vy2)
{
x1=vx1; y1=vy1; x2=vx2; y2=vy2;
}

void cTree::AddRect(int vx1, int vy1, int vx2, int vy2)
{
int i;
int oldValue, newValue;
oldValue=isCovered;
newValue=CompareTwoAreas(x1,y1,x2,y2,vx1,vy1,vx2,vy2);
if (newValue==1) isCovered=1;
if ((newValue==2) &amp;&amp; (oldValue!=1)) isCovered=2;

if ((isCovered!=2) &amp;&amp; (oldValue==2)) delete ChildArea;
if (isCovered==2)
{
if (oldValue2)
{
ChildArea = new cTree;
ChildArea.Init(x1,y1,(x1+x2)/2,(y1+y2)/2);
ChildArea.Init((x1+x2)/2,y1,x2,(y1+y2)/2);
ChildArea.Init(x1,(y1+y2)/2,(x1+x2)/2,y2);
ChildArea.Init((x1+x2)/2,(y1+y2)/2,x2,y2);
}
}
}

long cTree::CountArea()
{
long temp=0;
if (isCovered==1) temp=(x2-x1)*(y1-y2);
if (isCovered==2) temp=ChildArea.CountArea()+ChildArea.CountArea()+
ChildArea.CountArea()+ChildArea.CountArea();
return temp;
}

void main() {
int i, NumOfRect, x1, y1, x2, y2;
cTree WorkSpace;
clrscr();
WorkSpace.Init(-32768,32767,32767,-32768);
cout  "    Covered Area Calculator -  programmed by Robert Setiadi\n";
cout &lt;&lt; "===============================================================\n";
cout &lt;&lt; "this program accept any number of rectangles as input\n";
cout &lt;&lt; "and calculate total area covered by all those rectangles\n";
cout &lt;&lt; "a rectangle is defined by top left and bottom right coordinates\n";
cout &lt;&lt; "accepted range from -32768 to +32767\n\n";
cout &lt;&lt; "Please input the number of rectangles : "; cin &gt;&gt; NumOfRect;
for (i=0; i&lt;NumOfRect; i++)
{
cout  "Rectangle "  i+1 &lt;&lt; " [x1,y1,x2,y2] = ";
scanf("%d,%d,%d,%d",&amp;x1,&amp;y1,&amp;x2,&amp;y2);
fflush(stdin);
}
cout &lt;&lt; "\nTotal covered area = " &lt;&lt; WorkSpace.CountArea();
getch();
}```

[/toggle]
```compareLine :: Int -&gt; Int -&gt; Int -&gt; Int -&gt; Int
compareLine a b c d = if c &lt; a
then if d &lt;= a
then 0
else if d &gt;= b
then 1
else 2
else if c == a
then if d &gt;= b
then 1
else 2
else if (c &gt; a) &amp;&amp; (c &lt; b)
then 2
else 0

compareTwoAreas :: Int -&gt; Int -&gt; Int -&gt; Int -&gt; Int -&gt; Int -&gt; Int -&gt; Int -&gt; Int
compareTwoAreas x1 y1 x2 y2 x3 y3 x4 y4
| ((compareLine x1 x2 x3 x4)==0) || ((compareLine y2 y1 y4 y3)==0) = 0
| ((compareLine x1 x2 x3 x4)==1) &amp;&amp; ((compareLine y2 y1 y4 y3)==1) = 1
| otherwise                                                        = 2

data Rectangle = Rect Int Int Int Int
data CTree = Leaf Int Int Int Int Int | Branch Int Int Int Int Int CTree CTree CTree CTree
deriving (Show,Eq)

addRectangle :: CTree -&gt; Int -&gt; Int -&gt; Int -&gt; Int -&gt; CTree
addRectangle (Leaf i x1 y1 x2 y2) nx1 ny1 nx2 ny2
| (compareTwoAreas x1 y1 x2 y2 nx1 ny1 nx2 ny2)==1 = Leaf 1 x1 y1 x2 y2
| ((compareTwoAreas x1 y1 x2 y2 nx1 ny1 nx2 ny2)==2) &amp;&amp; (i/=1) =
Branch 2 x1 y1 x2 y2
(addRectangle (Leaf 0 x1 y1 (div (x1+x2) 2) (div (y1+y2) 2)) nx1 ny1 nx2 ny2)
(addRectangle (Leaf 0 (div (x1+x2) 2) y1 x2 (div (y1+y2) 2)) nx1 ny1 nx2 ny2)
(addRectangle (Leaf 0 x1 (div (y1+y2) 2) (div (x1+x2) 2) y2) nx1 ny1 nx2 ny2)
(addRectangle (Leaf 0 (div (x1+x2) 2) (div (y1+y2) 2) x2 y2) nx1 ny1 nx2 ny2)
| otherwise = Leaf i x1 y1 x2 y2
addRectangle (Branch i x1 y1 x2 y2 z1 z2 z3 z4) nx1 ny1 nx2 ny2
| (compareTwoAreas x1 y1 x2 y2 nx1 ny1 nx2 ny2)==1 = Leaf 1 x1 y1 x2 y2
| ((compareTwoAreas x1 y1 x2 y2 nx1 ny1 nx2 ny2)==2) &amp;&amp; (i/=1) =
Branch 2 x1 y1 x2 y2
(addRectangle z1 nx1 ny1 nx2 ny2)
(addRectangle z2 nx1 ny1 nx2 ny2)
(addRectangle z3 nx1 ny1 nx2 ny2)
(addRectangle z4 nx1 ny1 nx2 ny2)
| otherwise = Branch i x1 y1 x2 y2 z1 z2 z3 z4

buildTree :: CTree -&gt; [Rectangle] -&gt; CTree
buildTree t [] = t
buildTree t ((Rect a b c d):zs) = buildTree (addRectangle t a b c d) zs

initTree :: [Rectangle] -&gt; CTree
initTree t = buildTree (Leaf 0 (-32768) 32767 32767 (-32768)) t

privCountArea :: CTree -&gt; Int
privCountArea (Leaf i x1 y1 x2 y2)
| i==1      = (x2-x1)*(y1-y2)
| otherwise = 0
privCountArea (Branch i x1 y1 x2 y2 a b c d) = (privCountArea a) + (privCountArea b) + (privCountArea c) + (privCountArea d)

countArea :: [Rectangle] -&gt; Int
countArea i = privCountArea (initTree i)```

[/toggle]
[toggle title=”Prolog version”]
```compareLine(A,B,C,D,Result) :-
( C &lt; A -&gt;
( D =&lt; A       -&gt; Result is 0 ;
D &gt; A, D &lt; B -&gt; Result is 2 ;
D &gt;= B       -&gt; Result is 1
) ;
C = A -&gt;
(
D &gt;= B       -&gt; Result is 1 ;
D &lt; B        -&gt; Result is 2
) ;
C &gt; A -&gt;
(
C &lt; B        -&gt; Result is 2 ;
C &gt;= B       -&gt; Result is 0
)
).

compareTwoAreas(X1,Y1,X2,Y2,X3,Y3,X4,Y4,Result) :-
compareLine(X1,X2,X3,X4,R1), compareLine(Y2,Y1,Y4,Y3,R2),
( ((R1 = 0); (R2 = 0)) -&gt; Result is 0 ;
((R1 = 1), (R2 = 1)) -&gt; Result is 1 ;
((R1 + R2) &gt; 2) -&gt; Result is 2).

leaf(_,_,_,_,_).

compareTwoAreas(X1,Y1,X2,Y2,NX1,NY1,NX2,NY2,R),
( R = 1 -&gt; Result = leaf(1,X1,Y1,X2,Y2) ;
R = 2, Filled \= 1 -&gt;
( Xm is (X1 + X2) // 2,
Ym is (Y1 + Y2) // 2,
Result = branch(2,X1,Y1,X2,Y2,R1,R2,R3,R4) );
R = 2, Filled = 1 -&gt; Result = leaf(1,X1,Y1,X2,Y2) ;
R = 0 -&gt; Result = leaf(Filled,X1,Y1,X2,Y2)).

compareTwoAreas(X1,Y1,X2,Y2,NX1,NY1,NX2,NY2,R),
( R = 1 -&gt; Result = leaf(1,X1,Y1,X2,Y2) ;
R = 2, Filled \= 1 -&gt;
Result = branch(2,X1,Y1,X2,Y2,R1,R2,R3,R4) );
R = 2, Filled = 1 -&gt; Result = leaf(1,X1,Y1,X2,Y2) ;
R = 0 -&gt; Result = branch(Filled,X1,Y1,X2,Y2,Z1,Z2,Z3,Z4)).

buildTree(T,[],T2) :- T2 = T.
buildTree(T,[rect(A,B,C,D)|ZS],T2) :-
buildTree(T3,ZS,T2).

initTree(L,Result) :- buildTree(leaf(0,(-32768),32767,32767,(-32768)),L,Result).

privCountArea(leaf(Filled,X1,Y1,X2,Y2), Result) :-
( Filled = 1 -&gt; Result is (X2 - X1) * (Y1 - Y2) ;
Filled = 0 -&gt; Result is 0 ).

privCountArea(branch(_,_,_,_,_,Z1,Z2,Z3,Z4), Result) :-
( privCountArea(Z1,Z1r),
privCountArea(Z2,Z2r),
privCountArea(Z3,Z3r),
privCountArea(Z4,Z4r),
Result is Z1r + Z2r + Z3r + Z4r).

countArea(A,Result) :-
initTree(A,Tree),
privCountArea(Tree,Result).```

[/toggle]
Just some simple codes, they calculate everything in recursion so the same algorithm can be implemented in imperative programming language (C++), functional programming language (Haskell) and logic programming language (Prolog).
C++ code is tested and compiled using Turbo C++ 3.
Haskell code is tested and compiled using Hugs98.
Prolog code is tested and compiled using SWI-Prolog.