GeometryUtils::calculateCenterCoord()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
namespace Jackal\ImageMerge\Utils;
4
5
use Jackal\ImageMerge\Command\Options\MultiCoordinateCommandOption;
6
use Jackal\ImageMerge\ValueObject\Coordinate;
7
8
class GeometryUtils
9
{
10
    const TOP_LEFT = 0;
11
    const TOP_RIGHT = 1;
12
    const BOTTOM_RIGHT = 2;
13
    const BOTTOM_LEFT = 3;
14
15
    public static function getClockwiseOrder(MultiCoordinateCommandOption $multiCoordinateCommandOption)
16
    {
17
        $coords = $multiCoordinateCommandOption->getCoordinates();
18
19
        $mostTop = self::getTopCoord($coords, 2);
20
        $mostBottom = self::getBottomCoord($coords, 2);
21
22
        $topLeft = self::getLeftCoord($mostTop)[0];
23
        $topRight = self::getRightCoord($mostTop)[0];
24
        $bottomLeft = self::getLeftCoord($mostBottom)[0];
25
        $bottomRight = self::getRightCoord($mostBottom)[0];
26
27
        return new MultiCoordinateCommandOption([
28
            self::TOP_LEFT => $topLeft,
29
            self::TOP_RIGHT => $topRight,
30
            self::BOTTOM_RIGHT => $bottomRight,
31
            self::BOTTOM_LEFT => $bottomLeft,
32
        ]);
33
    }
34
35
    /**
36
     * @param $coords
37
     * @param int $limit
38
     * @return Coordinate[]
39
     */
40
    public static function getLeftCoord($coords, $limit = 1)
41
    {
42
        $c = $coords;
43
        usort($c, function (Coordinate $coordA, Coordinate $coordB) {
44
            if ($coordA->getX() == $coordB->getX()) {
45
                return 0;
46
            }
47
48
            return ($coordA->getX() >= $coordB->getX()) ? 1 : -1;
49
        });
50
51
        return array_slice($c, 0, $limit);
52
    }
53
54
    /**
55
     * @param $coords
56
     * @param int $limit
57
     * @return Coordinate[]
58
     */
59
    public static function getTopCoord($coords, $limit = 1)
60
    {
61
        $c = $coords;
62
        usort($c, function (Coordinate $coordA, Coordinate $coordB) {
63
            if ($coordA->getY() === $coordB->getY()) {
64
                return 0;
65
            }
66
67
            return ($coordA->getY() >= $coordB->getY()) ? 1 : -1;
68
        });
69
70
        return array_slice($c, 0, $limit);
71
    }
72
73
    /**
74
     * @param $coords
75
     * @param int $limit
76
     * @return Coordinate[]
77
     */
78
    public static function getRightCoord($coords, $limit = 1)
79
    {
80
        $c = $coords;
81
        usort($c, function (Coordinate $coordA, Coordinate $coordB) {
82
            if ($coordA->getX() === $coordB->getX()) {
83
                return 0;
84
            }
85
86
            return ($coordA->getX() >= $coordB->getX()) ? -1 : 1;
87
        });
88
89
        return array_slice($c, 0, $limit);
90
    }
91
92
    /**
93
     * @param $coords
94
     * @param int $limit
95
     * @return Coordinate[]
96
     */
97
    public static function getBottomCoord($coords, $limit = 1)
98
    {
99
        $c = $coords;
100
        usort($c, function (Coordinate $coordA, Coordinate $coordB) {
101
            if ($coordA->getY() === $coordB->getY()) {
102
                return 0;
103
            }
104
105
            return ($coordA->getY() >= $coordB->getY()) ? -1 : 1;
106
        });
107
108
        return array_slice($c, 0, $limit);
109
    }
110
111
    public static function calculateCenterCoord($coords)
112
    {
113
        $sumX = 0;
114
        $sumY = 0;
115
        foreach ($coords as $coord) {
116
            $sumX += $coord->getX();
117
            $sumY += $coord->getY();
118
        }
119
120
        return new Coordinate(
121
            round($sumX / count($coords)),
122
            round($sumY / count($coords))
123
        );
124
    }
125
}
126