Skip to content

Instantly share code, notes, and snippets.

@Bloodb0ne
Created January 10, 2016 14:25
Show Gist options
  • Save Bloodb0ne/c6bc8928fb53757b9ad4 to your computer and use it in GitHub Desktop.
Save Bloodb0ne/c6bc8928fb53757b9ad4 to your computer and use it in GitHub Desktop.
<?php
$x = range(40,45,0.25);
$y = range(20,29,0.25);
// var_dump($x);
$v = 43.1;
$v2 = 21.4;
function get4GridPoints($xrange,$yrange,$x,$y,$eps = 0.25){
$count = 0;
$xvals = [];
foreach ($xrange as $value) {
if($count == 2) break;
if(abs( $x - $value ) <= $eps){
$xvals[] = $value;
$count ++;
}
}
$count = 0;
$yvals = [];
foreach ($yrange as $value) {
if($count == 2) break;
if(abs( $y - $value ) <= $eps){
$yvals[] = $value;
$count ++;
}
}
$res = [];
foreach ($xvals as $xv) {
foreach ($yvals as $yv) {
$res[] = [$xv,$yv];
}
}
return $res;
}
function get4GridIndices($xrange,$yrange,$x,$y,$eps = 0.25){
$count = 0;
$xvals = [];
foreach ($xrange as $key => $value) {
if($count == 2) break;
if(abs( $x - $value ) <= $eps){
$xvals[] = $key;
$count ++;
}
}
$count = 0;
$yvals = [];
foreach ($yrange as $key => $value) {
if($count == 2) break;
if(abs( $y - $value ) <= $eps){
$yvals[] = $key;
$count ++;
}
}
$res = [];
foreach ($xvals as $xv) {
foreach ($yvals as $yv) {
$res[] = [$xv + $yv*count($yrange)];
}
}
return $res;
}
var_dump(get4GridPoints($x,$y,$v,$v2));
var_dump(get4GridIndices($x,$y,$v,$v2));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment