CheckIfItIsAStraightLine::checkStraightLine()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 19
rs 9.6111
cc 5
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class CheckIfItIsAStraightLine
8
{
9
    public static function checkStraightLine(array $coordinates): bool
10
    {
11
        if (empty($coordinates)) {
12
            return false;
13
        }
14
        if (($n = count($coordinates)) === 2) {
15
            return true;
16
        }
17
        [$x1, $y1] = [$coordinates[0][0], $coordinates[0][1]];
18
        [$x2, $y2] = [$coordinates[1][0], $coordinates[1][1]];
19
        for ($i = 2; $i < $n; $i++) {
20
            [$x, $y] = [$coordinates[$i][0], $coordinates[$i][1]];
21
            // (y - y1) / (y2 - y1) = (x - x1) / (x2 - x1)
22
            if (($y - $y1) * ($x2 - $x1) !== ($y2 - $y1) * ($x - $x1)) {
23
                return false;
24
            }
25
        }
26
27
        return true;
28
    }
29
}
30