Completed
Branch feature/SRTM1ArcSecond (5f684d)
by Hannes
04:35
created

BilinearInterpolation   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 1
dl 0
loc 33
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A interpolate() 0 11 1
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