CheckIfItIsAStraightLine   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 21
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A checkStraightLine() 0 19 5
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