Completed
Pull Request — master (#3)
by Hannes
02:16
created

BilinearInterpolation::interpolate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Runalyze DEM Reader.
5
 *
6
 * (c) RUNALYZE <[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 Runalyze\DEM\Interpolation;
13
14
class BilinearInterpolation implements InterpolationInterface
15
{
16
    use ArgumentsCheckTrait;
17
18
    /**
19
     * Interpolate.
20
     *
21
     * p0------------p1
22
     * |      |
23
     * |      y
24
     * |      |
25
     * |--x-- Z
26
     * |
27
     * p2------------p3
28
     *
29
     * @param  float                    $x                      x position of Z within bounding box, required: $x in [0.0, 1.0]
30
     * @param  float                    $y                      y position of Z within bounding box, required: $y in [0.0, 1.0]
31
     * @param  array                    $elevationOnBoundingBox elevation data on [p0, p1, p2, p3]
32
     * @return int                      estimated elevation on point Z
33
     * @throws InvalidArgumentException
34
     */
35
    public function interpolate($x, $y, array $elevationOnBoundingBox)
36
    {
37
        $this->checkArguments($x, $y, $elevationOnBoundingBox);
38
39
        return (int) round(
40
            $elevationOnBoundingBox[0] * (1 - $x) * (1 - $y)
41
            + $elevationOnBoundingBox[1] * $x * (1 - $y)
42
            + $elevationOnBoundingBox[2] * $y * (1 - $x)
43
            + $elevationOnBoundingBox[3] * $x * $y
44
        );
45
    }
46
}
47