# tuupola/trilateration

Latest commit 5d8a577 Jan 3, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/Trilateration Jun 22, 2017
tests Jan 3, 2019
.gitignore May 14, 2017
.travis.yml Jan 3, 2019
CHANGELOG.md Nov 12, 2018
Makefile Aug 20, 2017
composer.json Nov 12, 2018
phpunit.xml.dist May 14, 2017

# Trilateration

PHP implementation of Trilateration algorithm. See Wifi Trilateration With Three or More Points for walkthrough.

## Install

Install the library using Composer.

`\$ composer require tuupola/trilateration`

## Usage

### Pure PHP Version

Pure PHP implementation calculates the position by finding the intersection of three spheres using traditional algebra. If the spheres do not intersect algorithm enlarges the spheres until they do intersect.

```use Tuupola\Trilateration\Intersection;
use Tuupola\Trilateration\Sphere;

\$sphere1 = new Sphere(60.1695, 24.9354, 81175);
\$sphere2 = new Sphere(58.3806, 26.7251, 162311);
\$sphere3 = new Sphere(58.3859, 24.4971, 116932);

\$trilateration = new Intersection(\$sphere1, \$sphere2, \$sphere3);
\$point = \$trilateration->position();

print_r(\$point);

/*
Tuupola\Trilateration\Point Object
(
[latitude:protected] => 59.418775152143
[longitude:protected] => 24.753287172291
)
*/

\$url = "https://appelsiini.net/circles/"
. "?c={\$sphere1}&c={\$sphere2}&c={\$sphere3}&m={\$point}";

print '<a href="{\$url}">Open in map</a>';```

Open in map

### Non Linear Least Squares Using R

R version uses non linear least squares fitting. It can use three or more locations for the calculation and gives better results when given data is less accurate. Before using this version you must install the R language and geosphere package.

``````\$ sudo yum -y install R
\$ sudo su - -c "R -e \"install.packages('geosphere', repos='http://cran.rstudio.com/')\""
``````
```use Tuupola\Trilateration\NonLinearLeastSquares;
use Tuupola\Trilateration\Sphere;

\$sphere1 = new Sphere(60.1695, 24.9354, 81175);
\$sphere2 = new Sphere(58.3806, 26.7251, 162311);
\$sphere3 = new Sphere(58.3859, 24.4971, 116932);

\$trilateration = new NonLinearLeastSquares(\$sphere1, \$sphere2, \$sphere3);
\$point = \$trilateration->position();

print_r(\$point);

/*
Tuupola\Trilateration\Point Object
(
[latitude:protected] => 59.4355408765689
[longitude:protected] => 24.7747644991839

)
*/

\$url = "https://appelsiini.net/circles/"
. "?c={\$sphere1}&c={\$sphere2}&c={\$sphere3}&m={\$point}";

print '<a href="{\$url}">Open in map</a>';```

Open in map

## Testing

You can run tests either manually or automatically on every code change. Automatic tests require entr to work.

`\$ make test`
```\$ brew install entr
\$ make watch```