Guest User

php script to process data from form

a guest
Oct 14th, 2014
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.64 KB | None | 0 0
  1. <?php
  2.  
  3. if(isset($_POST) && !empty($_POST))
  4. {
  5.     $current_star=(isset($_POST["current_star"]) && !empty($_POST["current_star"])) ? $_POST["current_star"] : false;
  6.     $distance_1=(isset($_POST["distance_1"]) && !empty($_POST["distance_1"])) ? (float)0+$_POST["distance_1"] : 0;
  7.     $distance_2=(isset($_POST["distance_2"]) && !empty($_POST["distance_2"])) ? (float)0+$_POST["distance_2"] : 0;
  8.     $distance_3=(isset($_POST["distance_3"]) && !empty($_POST["distance_3"])) ? (float)0+$_POST["distance_3"] : 0;
  9.     $distance_4=(isset($_POST["distance_4"]) && !empty($_POST["distance_4"])) ? (float)0+$_POST["distance_4"] : 0;
  10.     $distance_5=(isset($_POST["distance_5"]) && !empty($_POST["distance_5"])) ? (float)0+$_POST["distance_5"] : 0;
  11.     $distance_6=(isset($_POST["distance_6"]) && !empty($_POST["distance_6"])) ? (float)0+$_POST["distance_6"] : 0;
  12.     $distance_7=(isset($_POST["distance_7"]) && !empty($_POST["distance_7"])) ? (float)0+$_POST["distance_7"] : 0;
  13.     $distance_8=(isset($_POST["distance_8"]) && !empty($_POST["distance_8"])) ? (float)0+$_POST["distance_8"] : 0;
  14.     $distance_9=(isset($_POST["distance_9"]) && !empty($_POST["distance_9"])) ? (float)0+$_POST["distance_9"] : 0;
  15.  
  16.     $result = array();
  17.     $combination = array();
  18.  
  19.     $system["SOL"]=array(0,0,0,0);
  20.     $system["WOLF_497"]=array(0.125,41.46875,11.96875,0);
  21.     $system["HUOKANG"]=array(-12.1875,35.46875,-25.28125,0);
  22.     $system["DEMETER"]=array(-55.34375,40.8125,7.5625,0);
  23.     $system["CLOTTI"]=array(-88.03125,47.1875,-6.15625,0);
  24.     $system["FU_HAITING"]=array(-69.46875,32.125,-48.875,0);
  25.     $system["SAN_GUARALARU"]=array(-124.34375,41.8125,-60.71875,0);
  26.     $system["HARAS"]=array(-118.75,14.40625,-21.40625,0);
  27.     $system["ARABHA"]=array(-120.75,43.78125,-22.125,0);
  28.  
  29.     $system["SOL"][3]=$distance_1;
  30.     $system["WOLF_497"][3]=$distance_2;
  31.     $system["HUOKANG"][3]=$distance_3;
  32.     $system["DEMETER"][3]=$distance_4;
  33.     $system["CLOTTI"][3]=$distance_5;
  34.     $system["FU_HAITING"][3]=$distance_6;
  35.     $system["SAN_GUARALARU"][3]=$distance_7;
  36.     $system["HARAS"][3]=$distance_8;
  37.     $system["ARABHA"][3]=$distance_9;
  38.  
  39.     $system_with_distance=array();
  40.  
  41.     foreach($system as $key => $value)
  42.     {
  43.         if($value[3] > 0)
  44.         {
  45.             $system_with_distance[]=$key;
  46.         }
  47.     }
  48.     if(!$current_star)
  49.     {
  50.         die("Please enter the name of the source star!");
  51.     }
  52.     if(count($system_with_distance) < 4)
  53.     {
  54.         die("Distances to at least 4 of the referenced stars are required!");
  55.     }
  56.     elseif(count($system_with_distance) == 4)
  57.     {
  58.         $star_pos=(trilateration3d($system[$system_with_distance[0]], $system[$system_with_distance[1]], $system[$system_with_distance[2]], $system[$system_with_distance[3]]));
  59.         echo "Estimated position of <b>".$current_star."</b>: ".$star_pos[0].", ".$star_pos[1].", ".$star_pos[2].".";
  60.     }
  61.     elseif(count($system_with_distance) > 4)
  62.     {
  63.         $i=0;
  64.         $x_total=0;
  65.         $y_total=0;
  66.         $z_total=0;
  67.  
  68.         $permutations=combinations($system_with_distance, 4);
  69.  
  70.         foreach($permutations as $value)
  71.         {
  72.             $calculated_coordinates[$i]=trilateration3d($system[$value[0]],$system[$value[1]],$system[$value[2]],$system[$value[3]]);
  73.             $calculated_coordinates[$i]["permutation"]=str_replace("_", " ", $value[0]) . " | " . str_replace("_", " ", $value[1]) . " | " . str_replace("_", " ", $value[2]) . " | " . str_replace("_", " ", $value[3]);
  74.             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]))
  75.             {
  76.                 $calculated_coordinates[$i]["x"]=$calculated_coordinates[$i][0];
  77.                 $calculated_coordinates[$i]["y"]=$calculated_coordinates[$i][1];
  78.                 $calculated_coordinates[$i]["z"]=$calculated_coordinates[$i][2];
  79.                 unset($calculated_coordinates[$i][0],$calculated_coordinates[$i][1],$calculated_coordinates[$i][2]);
  80.                 $x_total+=$calculated_coordinates[$i]["x"];
  81.                 $y_total+=$calculated_coordinates[$i]["y"];
  82.                 $z_total+=$calculated_coordinates[$i]["z"];
  83.             }
  84.             $i++;
  85.         }
  86.         foreach($calculated_coordinates as $key => $value)
  87.         {
  88.             if(!isset($value["x"]))
  89.                 unset($calculated_coordinates[$key]);
  90.                
  91.         }
  92.  
  93.         $n=count($calculated_coordinates);
  94.         echo "<pre>";
  95.         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";
  96.         print_r($calculated_coordinates);
  97.         echo "</pre>";
  98.         die;
  99.     }
  100. }
  101. else
  102.     die("No data entered!");
  103.  
  104. function trilateration3d($p1,$p2,$p3,$p4){
  105.         $ex = vector_unit(vector_diff($p2, $p1));
  106.         $i = vector_dot_product($ex, vector_diff($p3, $p1));
  107.         $ey = vector_unit(vector_diff(vector_diff($p3, $p1), vector_multiply($ex, $i)));
  108.         $ez = vector_cross($ex,$ey);
  109.         $d = vector_length($p2, $p1);
  110.         $r1 = $p1[3]; $r2 = $p2[3]; $r3 = $p3[3]; $r4 = $p4[3];
  111.         if($d - $r1 >= $r2 || $r2 >= $d + $r1){
  112.                 return array();
  113.         }
  114.         $j = vector_dot_product($ey, vector_diff($p3, $p1));
  115.         $x = (($r1*$r1) - ($r2*$r2) + ($d*$d)) / (2*$d);
  116.         $y = ((($r1*$r1) - ($r3*$r3) + ($i*$i) + ($j*$j)) / (2*$j)) - (($i*$x) / $j);
  117.         $z = $r1*$r1 - $x*$x - $y*$y;
  118.  
  119.         if($z < 0){
  120.                 return array();
  121.         }
  122.         $z1 = sqrt($z);
  123.         $z2 = $z1 * -1;
  124.  
  125.         $result1 = $p1;
  126.         $result1 = vector_sum($result1, vector_multiply($ex, $x));
  127.         $result1 = vector_sum($result1, vector_multiply($ey, $y));
  128.         $result1 = vector_sum($result1, vector_multiply($ez, $z1));
  129.         $result2 = $p1;
  130.         $result2 = vector_sum($result2, vector_multiply($ex, $x));
  131.         $result2 = vector_sum($result2, vector_multiply($ey, $y));
  132.         $result2 = vector_sum($result2, vector_multiply($ez, $z2));
  133.  
  134.         $r1 = vector_length($p4, $result1);
  135.         $r2 = vector_length($p4, $result2);
  136.         $t1 = $r1 - $r4;
  137.         $t2 = $r2 - $r4;
  138.         $coords = array();
  139.         if(abs($t1) < abs($t2)){
  140.                 $coords = array(round($result1[0], 5), round($result1[1], 5), round($result1[2], 5));
  141.         }
  142.         else{
  143.                 $coords = array(round($result2[0], 5), round($result2[1], 5), round($result2[2], 5));
  144.         }
  145.         return $coords;
  146. }
  147.  
  148. function vector_length($p1, $p2){
  149.         $a1 = $p1[0];
  150.         $a2 = $p2[0];
  151.         $b1 = $p1[1];
  152.         $b2 = $p2[1];
  153.         $c1 = $p1[2];
  154.         $c2 = $p2[2];
  155.         $dist = sqrt((($a2-$a1)*($a2-$a1))+(($b2-$b1)*($b2-$b1))+(($c2-$c1)*($c2-$c1)));
  156.  
  157.         return round($dist, 3, PHP_ROUND_HALF_EVEN);
  158. }
  159.  
  160. function vector_sum($v1, $v2){
  161.         $v = array();
  162.         $v[0] = $v1[0] + $v2[0];
  163.         $v[1] = $v1[1] + $v2[1];
  164.         $v[2] = $v1[2] + $v2[2];
  165.         return $v;
  166. }
  167.  
  168. function vector_cross($v1, $v2){
  169.         $v = array();
  170.         $v[0] = ($v1[1] * $v2[2]) - ($v1[2] * $v2[1]);
  171.         $v[1] = ($v1[2] * $v2[0]) - ($v1[0] * $v2[2]);
  172.         $v[2] = ($v1[0] * $v2[1]) - ($v1[1] * $v2[0]);
  173.         return $v;
  174. }
  175.  
  176. function vector_multiply($v, $i){
  177.         return array($v[0] * $i, $v[1] * $i, $v[2] * $i);
  178. }
  179.  
  180. function vector_dot_product($v1, $v2){
  181.         $ret = ($v1[0] * $v2[0]) + ($v1[1] * $v2[1]) + ($v1[2] * $v2[2]);
  182.         return $ret;
  183. }
  184.  
  185. function vector_diff($p1,$p2){
  186.         $ret = array($p1[0] - $p2[0], $p1[1] - $p2[1], $p1[2] - $p2[2]);
  187.         return $ret;
  188. }
  189.  
  190. function vector_norm($v){
  191.         $l = sqrt(($v[0]*$v[0])+($v[1]*$v[1])+($v[2]*$v[2]));
  192.         return $l;
  193. }
  194.  
  195. function vector_div($v, $l){
  196.         return array($v[0]/$l, $v[1] / $l, $v[2] / $l);
  197. }
  198.  
  199. function vector_unit($v){
  200.         $l = vector_norm($v);
  201.         if($l == 0){
  202.                 return -1;
  203.         }
  204.         return vector_div($v, $l);
  205. }
  206.  
  207. function combinations(array $myArray, $choose) {
  208.  
  209.   global $result, $combination;
  210.  
  211.   $n = count($myArray);
  212.  
  213.   function inner ($start, $choose_, $arr, $n) {
  214.     global $result, $combination;
  215.  
  216.     if ($choose_ == 0) array_push($result,$combination);
  217.     else for ($i = $start; $i <= $n - $choose_; ++$i) {
  218.            array_push($combination, $arr[$i]);
  219.            inner($i + 1, $choose_ - 1, $arr, $n);
  220.            array_pop($combination);
  221.          }
  222.   }
  223.   inner(0, $choose, $myArray, $n);
  224.   return $result;
  225. }
  226.  
  227. ?>
Advertisement
Add Comment
Please, Sign In to add comment