LinearFunction   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 48
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromTwoPoints() 0 11 2
A evaluate() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the CGI-Calc package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Cgi\Calc\Func;
13
14
use Cgi\Calc\FunctionInterface;
15
use Cgi\Calc\Point;
16
17
class LinearFunction implements FunctionInterface
18
{
19
    /** @var float */
20
    private $k;
21
22
    /** @var float */
23
    private $n;
24
25
    /**
26
     * @param float $k
27
     * @param float $n
28
     */
29
    public function __construct($k, $n)
30
    {
31
        $this->k = $k;
32
        $this->n = $n;
33
    }
34
35
    /**
36
     * @param Point $a
37
     * @param Point $b
38
     *
39
     * @return LinearFunction
40
     *
41
     * @throws \RuntimeException When $a and $b points have same x
42
     */
43
    public static function fromTwoPoints(Point $a, Point $b)
44
    {
45
        if ($a->getX() === $b->getX()) {
46
            throw new \RuntimeException('Line can be defined only by two points with different X');
47
        }
48
49
        $k = ($a->getY() - $b->getY()) / ($a->getX() - $b->getX());
50
        $n = $a->getY() - $k * $a->getX();
51
52
        return new self($k, $n);
53
    }
54
55
    /**
56
     * @param float $x
57
     *
58
     * @return float
59
     */
60
    public function evaluate($x)
61
    {
62
        return $this->k * $x + $this->n;
63
    }
64
}
65