PHP code example of f1r3starter / kdtree

1. Go to this page and download the library: Download f1r3starter/kdtree library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

f1r3starter / kdtree example snippets




use KDTree\Structure\KDTree;  
use KDTree\ValueObject\Point;

$kdTree = new KDTree(2); // 2 for two-dimensional points, eg cities
$kdTree->put(new Point(35.0844, 106.6504)); 
$kdTree->put(new Point(41.2865, 174.7762));

// if you need somehow connect point to your application, you can use setName method
$point = new Point(46.8117, 33.4902);
$point->setName('Kakhovka');
$kdTree->put($point);
//...
$points = $kdTree->points(); // returns list of all points, which can be iterated through

$kdTree->contains(new Point(46.8117, 33.4902)); // will return "true"



use KDTree\Search\NearestSearch;
use KDTree\ValueObject\Point;

$search = new NearestSearch($kdTree);
$nearestPoint = $search->nearest((new Point(41.2865, 174.7762)));



use KDTree\Structure\PointsList;
use KDTree\Search\PartitionSearch;
use KDTree\ValueObject\{Partition, Point};

$pointsList = new PointsList(2);  
$pointsList->addPoint(new Point(46.8117, 33.4902));  
$pointsList->addPoint(new Point(31.3142, 42.5245));  
$pointsList->addPoint(new Point(22.2525, 41.3412));  
$pointsList->addPoint(new Point(55.4245, 52.5134));  

$search = new PartitionSearch($kdTree);  
$foundPoints = $search->find(new Partition($pointsList));