LineSegment::length()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Nubs\Geometron;
3
4
use Exception;
5
6
/**
7
 * This class represents an immutable line segment in euclidean geometry and its
8
 * associated operations.
9
 *
10
 * Instances of this class will not change state.  Any operations on the line
11
 * segment will return a new line segment with the new state.
12
 */
13
class LineSegment implements Finite
14
{
15
    /** @type \Nubs\Geometron\Point One endpoint of the line segment. */
16
    protected $_a;
17
18
    /** @type \Nubs\Geometron\Point One endpoint of the line segment. */
19
    protected $_b;
20
21
    /**
22
     * Initalize the line segment with its two endpoints.
23
     *
24
     * @api
25
     * @param \Nubs\Geometron\Point $a One endpoint of the line segment.
26
     * @param \Nubs\Geometron\Point $b One endpoint of the line segment.
27
     */
28
    public function __construct(Point $a, Point $b)
29
    {
30
        if (!$a->isSameSpace($b)) {
31
            throw new Exception('The two points must be in the same geometric space');
32
        }
33
34
        $this->_a = $a;
35
        $this->_b = $b;
36
    }
37
38
    /**
39
     * Get an endpoint of the line segment.
40
     *
41
     * @api
42
     * @return \Nubs\Geometron\Point One endpoint of the line segment.
43
     */
44
    public function a()
45
    {
46
        return $this->_a;
47
    }
48
49
    /**
50
     * Get an endpoint of the line segment.
51
     *
52
     * @api
53
     * @return \Nubs\Geometron\Point One endpoint of the line segment.
54
     */
55
    public function b()
56
    {
57
        return $this->_b;
58
    }
59
60
    /**
61
     * Determine whether this line segment is degenerate.
62
     *
63
     * A line segment is degenerate if its two points are the same.
64
     *
65
     * @api
66
     * @return bool True if the line segment is degenerate, false otherwise.
67
     */
68
    public function isDegenerate()
69
    {
70
        return $this->a()->isEqual($this->b());
71
    }
72
73
    /**
74
     * Calculates the vector between the two endpoints of the line segment.
75
     *
76
     * @api
77
     * @return \Nubs\Vectorix\Vector The vector from a() to b().
78
     */
79
    public function vector()
80
    {
81
        return $this->b()->vector()->subtract($this->a()->vector());
82
    }
83
84
    /**
85
     * Calculates the length of the line segment.
86
     *
87
     * @api
88
     * @return float The length of the line segment.
89
     */
90
    public function length()
91
    {
92
        return $this->vector()->length();
93
    }
94
95
    /**
96
     * The center of a line segment is the point halfway along the line.
97
     *
98
     * @api
99
     * @return \Nubs\Geometron\Point The center-point of the line segment.
100
     * @see \Nubs\Geometron\Finite::center()
101
     */
102
    public function center()
103
    {
104
        return new Point($this->a()->vector()->add($this->vector()->divideByScalar(2)));
105
    }
106
}
107