Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if(isset($_POST) && !empty($_POST))
- {
- $current_star=(isset($_POST["current_star"]) && !empty($_POST["current_star"])) ? $_POST["current_star"] : false;
- $distance_1=(isset($_POST["distance_1"]) && !empty($_POST["distance_1"])) ? (float)0+$_POST["distance_1"] : 0;
- $distance_2=(isset($_POST["distance_2"]) && !empty($_POST["distance_2"])) ? (float)0+$_POST["distance_2"] : 0;
- $distance_3=(isset($_POST["distance_3"]) && !empty($_POST["distance_3"])) ? (float)0+$_POST["distance_3"] : 0;
- $distance_4=(isset($_POST["distance_4"]) && !empty($_POST["distance_4"])) ? (float)0+$_POST["distance_4"] : 0;
- $distance_5=(isset($_POST["distance_5"]) && !empty($_POST["distance_5"])) ? (float)0+$_POST["distance_5"] : 0;
- $distance_6=(isset($_POST["distance_6"]) && !empty($_POST["distance_6"])) ? (float)0+$_POST["distance_6"] : 0;
- $distance_7=(isset($_POST["distance_7"]) && !empty($_POST["distance_7"])) ? (float)0+$_POST["distance_7"] : 0;
- $distance_8=(isset($_POST["distance_8"]) && !empty($_POST["distance_8"])) ? (float)0+$_POST["distance_8"] : 0;
- $distance_9=(isset($_POST["distance_9"]) && !empty($_POST["distance_9"])) ? (float)0+$_POST["distance_9"] : 0;
- $result = array();
- $combination = array();
- $system["SOL"]=array(0,0,0,0);
- $system["WOLF_497"]=array(0.125,41.46875,11.96875,0);
- $system["HUOKANG"]=array(-12.1875,35.46875,-25.28125,0);
- $system["DEMETER"]=array(-55.34375,40.8125,7.5625,0);
- $system["CLOTTI"]=array(-88.03125,47.1875,-6.15625,0);
- $system["FU_HAITING"]=array(-69.46875,32.125,-48.875,0);
- $system["SAN_GUARALARU"]=array(-124.34375,41.8125,-60.71875,0);
- $system["HARAS"]=array(-118.75,14.40625,-21.40625,0);
- $system["ARABHA"]=array(-120.75,43.78125,-22.125,0);
- $system["SOL"][3]=$distance_1;
- $system["WOLF_497"][3]=$distance_2;
- $system["HUOKANG"][3]=$distance_3;
- $system["DEMETER"][3]=$distance_4;
- $system["CLOTTI"][3]=$distance_5;
- $system["FU_HAITING"][3]=$distance_6;
- $system["SAN_GUARALARU"][3]=$distance_7;
- $system["HARAS"][3]=$distance_8;
- $system["ARABHA"][3]=$distance_9;
- $system_with_distance=array();
- foreach($system as $key => $value)
- {
- if($value[3] > 0)
- {
- $system_with_distance[]=$key;
- }
- }
- if(!$current_star)
- {
- die("Please enter the name of the source star!");
- }
- if(count($system_with_distance) < 4)
- {
- die("Distances to at least 4 of the referenced stars are required!");
- }
- elseif(count($system_with_distance) == 4)
- {
- $star_pos=(trilateration3d($system[$system_with_distance[0]], $system[$system_with_distance[1]], $system[$system_with_distance[2]], $system[$system_with_distance[3]]));
- echo "Estimated position of <b>".$current_star."</b>: ".$star_pos[0].", ".$star_pos[1].", ".$star_pos[2].".";
- }
- elseif(count($system_with_distance) > 4)
- {
- $i=0;
- $x_total=0;
- $y_total=0;
- $z_total=0;
- $permutations=combinations($system_with_distance, 4);
- foreach($permutations as $value)
- {
- $calculated_coordinates[$i]=trilateration3d($system[$value[0]],$system[$value[1]],$system[$value[2]],$system[$value[3]]);
- $calculated_coordinates[$i]["permutation"]=str_replace("_", " ", $value[0]) . " | " . str_replace("_", " ", $value[1]) . " | " . str_replace("_", " ", $value[2]) . " | " . str_replace("_", " ", $value[3]);
- if(isset($calculated_coordinates[$i][0]) && !empty($calculated_coordinates[$i][0]) && isset($calculated_coordinates[$i][1]) && !empty($calculated_coordinates[$i][1]) && isset($calculated_coordinates[$i][2]) && !empty($calculated_coordinates[$i][2]))
- {
- $calculated_coordinates[$i]["x"]=$calculated_coordinates[$i][0];
- $calculated_coordinates[$i]["y"]=$calculated_coordinates[$i][1];
- $calculated_coordinates[$i]["z"]=$calculated_coordinates[$i][2];
- unset($calculated_coordinates[$i][0],$calculated_coordinates[$i][1],$calculated_coordinates[$i][2]);
- $x_total+=$calculated_coordinates[$i]["x"];
- $y_total+=$calculated_coordinates[$i]["y"];
- $z_total+=$calculated_coordinates[$i]["z"];
- }
- $i++;
- }
- foreach($calculated_coordinates as $key => $value)
- {
- if(!isset($value["x"]))
- unset($calculated_coordinates[$key]);
- }
- $n=count($calculated_coordinates);
- echo "<pre>";
- echo "Estimated position of <b>".$current_star."</b>: ".number_format(($x_total/$n),6).", ".number_format(($y_total/$n),6).", ".number_format(($z_total/$n),6).".\n<b>(Coordinates averaged from the results below)</b>\n\n";
- print_r($calculated_coordinates);
- echo "</pre>";
- die;
- }
- }
- else
- die("No data entered!");
- function trilateration3d($p1,$p2,$p3,$p4){
- $ex = vector_unit(vector_diff($p2, $p1));
- $i = vector_dot_product($ex, vector_diff($p3, $p1));
- $ey = vector_unit(vector_diff(vector_diff($p3, $p1), vector_multiply($ex, $i)));
- $ez = vector_cross($ex,$ey);
- $d = vector_length($p2, $p1);
- $r1 = $p1[3]; $r2 = $p2[3]; $r3 = $p3[3]; $r4 = $p4[3];
- if($d - $r1 >= $r2 || $r2 >= $d + $r1){
- return array();
- }
- $j = vector_dot_product($ey, vector_diff($p3, $p1));
- $x = (($r1*$r1) - ($r2*$r2) + ($d*$d)) / (2*$d);
- $y = ((($r1*$r1) - ($r3*$r3) + ($i*$i) + ($j*$j)) / (2*$j)) - (($i*$x) / $j);
- $z = $r1*$r1 - $x*$x - $y*$y;
- if($z < 0){
- return array();
- }
- $z1 = sqrt($z);
- $z2 = $z1 * -1;
- $result1 = $p1;
- $result1 = vector_sum($result1, vector_multiply($ex, $x));
- $result1 = vector_sum($result1, vector_multiply($ey, $y));
- $result1 = vector_sum($result1, vector_multiply($ez, $z1));
- $result2 = $p1;
- $result2 = vector_sum($result2, vector_multiply($ex, $x));
- $result2 = vector_sum($result2, vector_multiply($ey, $y));
- $result2 = vector_sum($result2, vector_multiply($ez, $z2));
- $r1 = vector_length($p4, $result1);
- $r2 = vector_length($p4, $result2);
- $t1 = $r1 - $r4;
- $t2 = $r2 - $r4;
- $coords = array();
- if(abs($t1) < abs($t2)){
- $coords = array(round($result1[0], 5), round($result1[1], 5), round($result1[2], 5));
- }
- else{
- $coords = array(round($result2[0], 5), round($result2[1], 5), round($result2[2], 5));
- }
- return $coords;
- }
- function vector_length($p1, $p2){
- $a1 = $p1[0];
- $a2 = $p2[0];
- $b1 = $p1[1];
- $b2 = $p2[1];
- $c1 = $p1[2];
- $c2 = $p2[2];
- $dist = sqrt((($a2-$a1)*($a2-$a1))+(($b2-$b1)*($b2-$b1))+(($c2-$c1)*($c2-$c1)));
- return round($dist, 3, PHP_ROUND_HALF_EVEN);
- }
- function vector_sum($v1, $v2){
- $v = array();
- $v[0] = $v1[0] + $v2[0];
- $v[1] = $v1[1] + $v2[1];
- $v[2] = $v1[2] + $v2[2];
- return $v;
- }
- function vector_cross($v1, $v2){
- $v = array();
- $v[0] = ($v1[1] * $v2[2]) - ($v1[2] * $v2[1]);
- $v[1] = ($v1[2] * $v2[0]) - ($v1[0] * $v2[2]);
- $v[2] = ($v1[0] * $v2[1]) - ($v1[1] * $v2[0]);
- return $v;
- }
- function vector_multiply($v, $i){
- return array($v[0] * $i, $v[1] * $i, $v[2] * $i);
- }
- function vector_dot_product($v1, $v2){
- $ret = ($v1[0] * $v2[0]) + ($v1[1] * $v2[1]) + ($v1[2] * $v2[2]);
- return $ret;
- }
- function vector_diff($p1,$p2){
- $ret = array($p1[0] - $p2[0], $p1[1] - $p2[1], $p1[2] - $p2[2]);
- return $ret;
- }
- function vector_norm($v){
- $l = sqrt(($v[0]*$v[0])+($v[1]*$v[1])+($v[2]*$v[2]));
- return $l;
- }
- function vector_div($v, $l){
- return array($v[0]/$l, $v[1] / $l, $v[2] / $l);
- }
- function vector_unit($v){
- $l = vector_norm($v);
- if($l == 0){
- return -1;
- }
- return vector_div($v, $l);
- }
- function combinations(array $myArray, $choose) {
- global $result, $combination;
- $n = count($myArray);
- function inner ($start, $choose_, $arr, $n) {
- global $result, $combination;
- if ($choose_ == 0) array_push($result,$combination);
- else for ($i = $start; $i <= $n - $choose_; ++$i) {
- array_push($combination, $arr[$i]);
- inner($i + 1, $choose_ - 1, $arr, $n);
- array_pop($combination);
- }
- }
- inner(0, $choose, $myArray, $n);
- return $result;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment