aboutsummaryrefslogtreecommitdiff
path: root/challenge-152/lubos-kolouch/php/ch-2.php
blob: d115137e0b770479460de8759bf82274e5f2358b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php

class Point {

    public int $x;
    public int $y;

    public function __construct(int $x, int $y = 0) {
        $this->x = $x;
        $this->y = $y;
    }

}

class Rectangle {

    public Point $left;
    public Point $right;

    public function __construct(Point $left, Point $right) {
        $this->left = $left;
        $this->right = $right;
    }
    public function get_area() {
        return abs( $this->left->x - $this->right->x ) * abs( $this->left->y - $this->right->y );
    }
}



function get_total_area(Rectangle $first, Rectangle $second) {

    $area1 = $first->get_area();
    $area2 = $second->get_area();

    # calculate the overlapping area
    $x_dist = min( $first->right->x, $second->right->x ) - max( $first->left->x, $second->left->x );
    $y_dist = min( $first->right->y, $second->right->y ) - max( $first->left->y, $second->left->y );
    $area_i = 0;
    if (( $x_dist > 0 ) and ( $y_dist > 0 )) {
         $area_i = $x_dist * $y_dist;
    }

       return $area1 + $area2 - $area_i;
}

$first  = new Rectangle(new Point(-1, 0), new Point(2, 2) );
$second = new Rectangle(new Point(0, -1), new Point(4, 4) );

get_total_area( $first, $second ) == 22 or throw new Exception("Test 1 failed");

$first  = new Rectangle(new Point(-3, -1), new Point(1, 3) );
$second = new Rectangle(new Point(-1, -3), new Point(2, 2) );

get_total_area( $first, $second ) == 25 or throw new Exception("Test 2 failed");
?>