This looks like university homework, and it isn't that hard -- just involved.
You'll need to keep a list of non-overlapping rectangles.
Given two rectangles like this:
┌─────────┐
│ │
│ a ┌───┼───┐
│ │ i │ b │
└─────┼───┘ │
└───────┘
|
Each rectangle has an
area... including the rectangle
i -- which is the intersection of rectangles
a and
b.
I'm sure you know enough math to also know that the
extent of
a and
b is
A( a ) + A( b ) - A( i ). If you add another overlapping rectangle the formula gets more complicated. That is a lot of work, especially when you have many rectangles overlapping the same region.
Hence, it is important to keep the list a list of
non-overlapping rectangles. When you add a rectangle to the list, make sure to split it up (if necessary). Our given rectangles then become something like this:
┌─────────┐
│ │
├─────┬───┴───┐
│ │ │
└─────┤ │
└───────┘
|
(It could have been split many different ways; Just pick one and stick to it.)
We now have
three non-overlapping rectangles, and the extent is simply the sum of their areas. That's much simpler, no?
One last thing to consider: What happens if you add a rectangle that completely encompasses others?
┌───────┐
┌────┼────┐ │
│ │ │ │
├────┼┬───┴──┼┐
│ ││ ││
└────┼┤ ││
│└──────┼┘
└───────┘
|
Hopefully, you should adjust to this
┌───────┐
┌────┤ │
│ │ │
├────┤ ├┐
│ │ ││
└────┤ ││
│ ├┘
└───────┘
|
These two operations are actually one and the same: first split and add, second prune. (This is easy if you do it for every rectangle you add -- since you know which rectangle has to encompass others to do pruning.
As far as I can see, your assignment doesn't require you to actually
draw anything to the console, just print the total extent of your rectangles.
Hope this helps.