Advertisement
Slyke

Triangulation_Test

Apr 13th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 29.26 KB | None | 0 0
  1. <?php
  2. /*
  3. Got distance formula from here: http://hotmath.com/hotmath_help/topics/distance-formula.html
  4. Got mid-point calculation formula from here: http://onlinemschool.com/math/assistance/cartesian_coordinate/p_center/
  5. Knowing the mid points will help us triangulate if no RSSI radiuses intersect.
  6. Good example of Haversine's formula: http://www.movable-type.co.uk/scripts/latlong.html
  7. (Sorry, my high school days are failing me, I had to look up the formulas and not waste time trying to figure them out).
  8.  
  9. Once we gather data, we'll have to change the $centimetersToRSSI value, perhaps to some type of formula like (with calculating attenuation from environment):
  10. $centimetersToRSSI=(1 / SQR(x));
  11. or
  12. $centimetersToRSSI=(1 / (x^2));
  13. where x is the input RSSI.
  14. See the following link for light propagation in 3 dimensional space: http://en.wikipedia.org/wiki/Inverse-square_law
  15. Line-line intersection: http://en.wikipedia.org/wiki/Line-line_intersection
  16.  
  17. Lat=Y
  18. Long=X
  19.  
  20. Found a good online graphics plotting program here:
  21. https://www.desmos.com/calculator
  22.  
  23. */
  24.  
  25. $centimetersToRSSI=(getQueryData("cmtorssi") >= 0 || getQueryData("cmtorssi") !="") ? getQueryData("cmtorssi") : 3; // Measure the RSSI at point blank from beacon to receiver. Move beacon away from receiver until you are at about -80dBm. Take the distance walked (Accurately in centimeters) and divide by the RSSI difference (eg: (500cms / (-22dBm - -80dBm)) = 8.62). This *must* be as accurate as possible.
  26.  
  27. if (!$included) {
  28.   $nodeLat=NULL;
  29.   $nodeLong=NULL;
  30.   $nodeRecievedRSSI=NULL;
  31. }
  32.  
  33. for ($i=0; $i < count($_GET['nodelat']); $i++) {
  34.   $nodeLat[]=((getQueryData("nodelat", $i) !="") ? floatval(getQueryData("nodelat", $i)) : 0);
  35.   $nodeLong[]=((getQueryData("nodelong", $i) !="") ? floatval(getQueryData("nodelong", $i)) : 0);
  36.   $nodeRecievedRSSI[]=((getQueryData("noderssi", $i) !="") ? getQueryData("noderssi", $i) : -60);
  37. }
  38.  
  39. if (count($nodeLat) != count($nodeLong) || count($nodeLat) != count($nodeRecievedRSSI)) {
  40.   echo "<br />\nWarning: Latitude (" . count($nodeLat) . "), longitude (" . count($nodeLat) . ") and received RSSI (" . count($nodeRecievedRSSI) . ") array data does not match. One or more of these parameters has more or less indexes then the other. There will be errors.<br />\n";
  41. } else {
  42.   $totalScanningNodes=count($nodeLat);
  43. }
  44.  
  45. if ($totalScanningNodes >= 2) {
  46.   $testOut=calculateTricircularIntersectionCoordinates($nodeLat, $nodeLong, $nodeRecievedRSSI);
  47. }
  48.  
  49. if (count($nodeLat) != count($nodeLong) || count($nodeLat) != count($nodeRecievedRSSI)) {
  50.   echo "Warning: Latitude (" . count($nodeLat) . "), longitude (" . count($nodeLat) . ") and received RSSI (" . count($nodeRecievedRSSI) . ") array data does not match. One or more of these parameters has more indexes then the other";
  51. }
  52.  
  53. if (!$testOut) {
  54.   echo "Error";
  55. } else {
  56.   echo "Output:";
  57.   outputDebugValues($testOut);
  58.   //outputDebugValues($testOut, true); // Uncomment this to dump the array without all the special formatting.
  59. }
  60. /*
  61. echo "\n<br />\n nodeLat (Y):";
  62. print_o($nodeLat);
  63. echo "<br />\n nodeLong (X):";
  64. print_o($nodeLong);
  65. echo "<br />\n nodeRecievedRSSI (R):";
  66. print_o($nodeRecievedRSSI);
  67. */
  68. function calculateTricircularIntersectionCoordinates($nodeLatitudes, $nodeLongitudes, $nodeRadiuses)
  69. // Basically pass in an array of X, Y and R and find out where they intersect with each other.
  70. {
  71.   if (count($nodeLatitudes) != count($nodeLongitudes) || count($nodeLatitudes) != count($nodeRadiuses)) { return false; } // X, Y and R amounts do not match, so exit the function, returning false.
  72.  
  73.   $smallRadius=min($nodeRadiuses);
  74.   $bigRadius=max($nodeRadiuses);
  75.  
  76.   $minKey=array_keys($nodeRadiuses, min($nodeRadiuses)); //Find the smallest radius' key in the array.
  77.  
  78.   $totalNodeLoops=count($nodeLatitudes); //One less than the total amount, so that we don't have a node try to calculate the distance to itself.
  79.  
  80.   for ($i=0;$i<$totalNodeLoops;$i++) { //Compare each node with all the other nodes and create a list of how far they are from each other (Along with the midpoints and that from calculateCartesianCoordinateDelta()).
  81.     for ($k=$i+1;$k<$totalNodeLoops;$k++) {
  82.       $theReturn[$i . "_to_" . $k]["cartesiandeltas"]=calculateCartesianCoordinateDelta($nodeLongitudes[$i], $nodeLatitudes[$i], $nodeLongitudes[$k], $nodeLatitudes[$k]); //Stores result from calculateCartesianCoordinateDelta() into '[$i_to_$k] ["cartesiandeltas"]' multidimensional array.
  83.       $theReturn[$i . "_to_" . $k]["rssiintersect"]= calculateRadiiVectorCoordinates($nodeLongitudes[$i], $nodeLatitudes[$i], $nodeRadiuses[$i], $nodeLongitudes[$k], $nodeLatitudes[$k], $nodeRadiuses[$k], $i, $k);
  84.     }
  85.   }
  86.  
  87.   return $theReturn;
  88.  
  89. }
  90.  
  91. function calculateDistance($dX1, $dY1, $dX2, $dY2){
  92.     $xDistance = abs($dX1 - $dX2);
  93.     $yDistance = abs($dY1 - $dY2);
  94.    
  95.     $distance = sqrt($xDistance*$xDistance + $yDistance*$yDistance);
  96.     return $distance;
  97. }
  98. function calculateCartesianCoordinateDelta($dX1, $dY1, $dX2, $dY2)
  99. // Calculates and returns the difference in location.
  100. {
  101.   $dXDelta=$dX2 - $dX1;
  102.   $dYDelta=$dY2 - $dY1;
  103.  
  104.   $deltaDistance = calculateDistance($dX1, $dY1, $dX2, $dY2);
  105.   //$deltaDistance = sqrt(((abs($dXDelta) * abs($dXDelta)) + (abs($dYDelta) * abs($dYDelta)))); // -- Made into another function as this calculation is used in multiple functions
  106.  
  107.   // This returns the midpoint distance between the two scanners
  108.   $midPointX = (abs($dXDelta) / 2) + min($dX1,$dX2); //Midpoint X
  109.   $midPointY = (abs($dYDelta) / 2) + min($dY1,$dY2); //Midpoint Y
  110.  
  111.   $theReturn["distance"]=$deltaDistance;
  112.   $theReturn["midx"]=$midPointX;
  113.   $theReturn["midy"]=$midPointY;
  114.  
  115.   return $theReturn;
  116.  
  117. }
  118.  
  119. function calculateRadiiVectorCoordinates($node1X, $node1Y, $node1R, $node2X, $node2Y, $node2R, $node1Index=1, $node2Index=2, $clockwiseRotation=true)
  120. {
  121.     // Work out the triangle
  122.     $dispX = $node2X - $node1X;
  123.     $dispY = $node2Y - $node1Y;
  124.     $scannerDistance = calculateDistance($node1X,$node1Y,$node2X,$node2Y);
  125.    
  126.     // Find the angle between hypotenuse and X plane
  127.     if($dispY == 0){
  128.         // If dispY is 0, then it's a straight line parallel to the Y axis
  129.         $angle = 0;
  130.     } elseif($dispX == 0){
  131.         // If dispX is 0, then it's a straight line tangent to the X axis
  132.         $angle = deg2rad(90);
  133.     } else {
  134.         $angle = atan(abs($dispY)/abs($dispX));
  135.     }
  136.    
  137.     // Find the distance of the midpoint from node1
  138.     $overlap = abs($node1R + $node2R)-$scannerDistance;
  139.     $midPointDistance = $node1R-($overlap/2);
  140.    
  141.     // Make a new triangle and work out the x and y displacement
  142.     $midX = cos($angle)*$midPointDistance;
  143.     $midY = sin($angle)*$midPointDistance;
  144.    
  145.     // Find the intersection points of the two circles
  146.     // Find angleB
  147.     $angleB = acos($midPointDistance/$node1R);
  148.    
  149.     // Find first intersection point
  150.     $intersectPX = $node1R * (cos($angle + $angleB));
  151.     $intersectPY = $node1R * (sin($angle + $angleB));
  152.    
  153.     // Find second intersection point
  154.     $intersectNX = $node1R * (cos($angle - $angleB));
  155.     $intersectNY = $node1R * (sin($angle - $angleB));
  156.    
  157.     // Calculating the cartesian angle between vector
  158.     $v1 = sqrt(pow($node1X, 2)) + (pow($node1Y,2));
  159.     $v2 = sqrt(pow($node2X, 2)) + (pow($node2Y,2));
  160.     $anglePre=((($node1X * $node2X) + ($node1Y * $node2Y)) / ($v1 * $v2));
  161.  
  162.     if ($node1X==$node2X && $node1Y==$node2Y) {
  163.         $angleRel=0; // or 360
  164.     } else {
  165.         if ($clockwiseRotation) {
  166.             if (($node1X-$node2X)< 0) {
  167.                 $angleRel=atan2(($node1X-$node2X),($node1Y-$node2Y));
  168.                 $angleDegs=rad2deg($angleRel*-1);
  169.             } else {
  170.                 $angleRel=(atan2(($node1X-$node2X),($node1Y-$node2Y))*-1) + (2 * pi());
  171.                 $angleDegs=rad2deg($angleRel);
  172.         if ($angleDegs==360) {
  173.           $angleDegs=0;
  174.         }
  175.             }
  176.     } else {
  177.             if ($dispX < 0) {
  178.                 $angleRel=atan2(($node1X-$node2X),($node1Y-$node2Y)) + (2 * pi());
  179.                 $angleDegs=rad2deg($angleRel);
  180.             } else {
  181.                 $angleRel=atan2(($node1X-$node2X),($node1Y-$node2Y));
  182.                 $angleDegs=rad2deg($angleRel);
  183.             }
  184.         }
  185.     }
  186.  
  187.     // Sort the triangle
  188.     switch ($angleRel){
  189.         case ($angleRel == 0 || $angleRel == (pi() * 2)):
  190.             $quadrantSector = 14; // Vertical line North
  191.             $midX = $node1X;
  192.             $midY = $node1Y - $midPointDistance;
  193.             $intersectPX = $node1X + $intersectPX;
  194.             $intersectPY = $node1X + $intersectPY;
  195.             break;
  196.         case $angleRel == (pi()/2):
  197.             $quadrantSector = 12; // Horizontal line East
  198.             $midX = $node1X + $midPointDistance;
  199.             $midY = $node1Y;
  200.             $intersectPX = $node1X + $intersectPX;
  201.             $intersectPY = $node1X + $intersectPY;
  202.             break;
  203.         case $angleRel == pi():
  204.             $quadrantSector = 23; // Vertical line South
  205.       $midX = $node1X;
  206.             $midY = $node1Y + $midPointDistance;
  207.             $intersectPX = $node1X + $intersectPX;
  208.             $intersectPY = $node1X - $intersectPY;
  209.             break;
  210.         case $angleRel == pi() + (pi()/2):
  211.             $quadrantSector = 34; // Horizontal line West
  212.             $midX = $node1X - $midPointDistance;
  213.             $midY = $node1Y;
  214.             $intersectPX = $node1X - $intersectPX;
  215.             $intersectPY = $node1X + $intersectPY;
  216.             break;
  217.         case $angleRel > 0 && $angleRel < (pi()/2):
  218.             $quadrantSector = 1;
  219.             $midX = $node1X + $midX;
  220.             $midY = $node1Y - $midY;
  221.             $intersectPX = $node1X + $intersectPX;
  222.             $intersectPY = $node1Y - $intersectPY;
  223.             $intersectNX = $node1X + $intersectNX;
  224.             $intersectNY = $node1Y - $intersectNY;
  225.             break;
  226.         case $angleRel > (pi()/2) && $angleRel < pi():
  227.             $quadrantSector = 2;
  228.             $midX = $node1X + $midX;
  229.             $midY = $node1Y + $midY;
  230.             $intersectPX = $node1X + $intersectPX;
  231.             $intersectPY = $node1Y + $intersectPY;
  232.             $intersectNX = $node1X + $intersectNX;
  233.             $intersectNY = $node1Y + $intersectNY;
  234.             break;
  235.         case $angleRel > pi() && $angleRel < (pi() + (pi()/2)):
  236.             $quadrantSector = 3;
  237.             $midX = $node1X - $midX;
  238.             $midY = $node1Y + $midY;
  239.             $intersectPX = $node1X - $intersectPX;
  240.             $intersectPY = $node1Y + $intersectPY;
  241.             $intersectNX = $node1X - $intersectNX;
  242.             $intersectNY = $node1Y + $intersectNY;
  243.             break;
  244.         case $angleRel > (pi() + M) && $angleRel < (pi() * 2):
  245.             $quadrantSector = 4;
  246.             $midX = $node1X - $midX;
  247.             $midY = $node1Y - $midY;
  248.             $intersectPX = $node1X - $intersectPX;
  249.             $intersectPY = $node1Y - $intersectPY;
  250.             $intersectNX = $node1X - $intersectNX;
  251.             $intersectNY = $node1Y - $intersectNY;
  252.             break;
  253.     }
  254.     /* OLD IF STATEMENTS
  255.     if($dispY == 0 && ($node1X > $node2X)){
  256.         // Horizontal line West
  257.         $q = "3&4";
  258.         $midX = $node1X - $midPointDistance;
  259.         $midY = $node1Y;
  260.         $intersect1X = $node1X - $intersect1X;
  261.         $intersect1Y = $node1X + $intersect1Y;
  262.     } elseif($dispY == 0 && ($node1X < $node2X)){
  263.         // Horizontal line East
  264.         $q = "1&2";
  265.         $midX = $node1X + $midPointDistance;
  266.         $midY = $node1Y;
  267.         $intersect1X = $node1X + $intersect1X;
  268.         $intersect1Y = $node1X + $intersect1Y;
  269.     } elseif($dispX == 0 && ($node1Y > $node2Y)){
  270.         // Vertical line North
  271.         $q = "1&4";
  272.         $midX = $node1X;
  273.         $midY = $node1Y - $midPointDistance;
  274.         $intersect1X = $node1X + $intersect1X;
  275.         $intersect1Y = $node1X + $intersect1Y;
  276.     } elseif($dispX == 0 && ($node1Y < $node2Y)){
  277.         // Vertical line South
  278.         $q = "2&3";
  279.         $midX = $node1X;
  280.         $midY = $node1Y + $midPointDistance;
  281.         $intersect1X = $node1X + $intersect1X;
  282.         $intersect1Y = $node1X - $intersect1Y;
  283.     } elseif($dispX>0 && $dispY<0){
  284.         // node2 is NE of node1
  285.         $q = 1;
  286.         $midX = $node1X + $midX;
  287.         $midY = $node1Y - $midY;
  288.         $intersect1X = $node1X + $intersect1X;
  289.         $intersect1Y = $node1Y - $intersect1Y;
  290.         $intersect2X = $node1X + $intersect2X;
  291.         $intersect2Y = $node1Y - $intersect2Y;
  292.     } elseif($dispX>0 && $dispY>0){
  293.         // node2 is SE of node1
  294.         $q = 2;
  295.         $midX = $node1X + $midX;
  296.         $midY = $node1Y + $midY;
  297.         $intersect1X = $node1X + $intersect1X;
  298.         $intersect1Y = $node1Y + $intersect1Y;
  299.         $intersect2X = $node1X + $intersect2X;
  300.         $intersect2Y = $node1Y + $intersect2Y;
  301.     } elseif($dispX<0 && $dispY>0){
  302.         // node2 is SW of node1
  303.         $q = 3;
  304.         $midX = $node1X - $midX;
  305.         $midY = $node1Y + $midY;
  306.         $intersect1X = $node1X - $intersect1X;
  307.         $intersect1Y = $node1Y + $intersect1Y;
  308.         $intersect2X = $node1X - $intersect2X;
  309.         $intersect2Y = $node1Y + $intersect2Y;
  310.     } elseif($dispX<0 && $dispY<0){
  311.         // node2 is NW of node1
  312.         $q = 4;
  313.         $midX = $node1X - $midX;
  314.         $midY = $node1Y - $midY;
  315.         $intersect1X = $node1X - $intersect1X;
  316.         $intersect1Y = $node1Y - $intersect1Y;
  317.         $intersect2X = $node1X - $intersect2X;
  318.         $intersect2Y = $node1Y - $intersect2Y;
  319.     }*/
  320.  
  321.     // Return array
  322.     $theReturn["node" . $node1Index . "X"]=$node1X;
  323.     $theReturn["node" . $node1Index . "Y"]=$node1Y;
  324.     $theReturn["node" . $node2Index . "X"]=$node2X;
  325.     $theReturn["node" . $node2Index . "Y"]=$node2Y;
  326.     $theReturn["node" . $node1Index . "R"]=$node1R;
  327.     $theReturn["node" . $node2Index . "R"]=$node2R;
  328.   $theReturn["nodeIndex1"]=$node1Index;
  329.   $theReturn["nodeIndex2"]=$node2Index;
  330.     $theReturn["scannerDistance"]=$scannerDistance;
  331.     $theReturn["scannerVectorX"]=$dispX;
  332.     $theReturn["scannerVectorY"]=$dispY;
  333.     $theReturn["radiiMidPointDistance"]=$midPointDistance;
  334.     $theReturn["radiiMidpointX"]=$midX;
  335.     $theReturn["radiimidpointY"]=$midY;
  336.   $theReturn["radiiIntersectPX"]=$intersectPX;
  337.     $theReturn["radiiIntersectPY"]=$intersectPY;
  338.     $theReturn["radiiIntersectNX"]=$intersectNX;
  339.     $theReturn["radiiIntersectNY"]=$intersectNY;
  340.     $theReturn["angleRad"]=$angleRel;
  341.     $theReturn["angleDeg"]=$angleDegs;
  342.     $theReturn["relativeQuadrant"]=$quadrantSector;
  343.     $theReturn["overlap"]=$overlap;
  344.     $theReturn["angleBDeg"]=$angleB;
  345.     $theReturn["angle + angleB"]=$angle + $angleB;
  346.     $theReturn["anglePre"]=$anglePre;
  347.   //$theReturn["nodelineintersecttest"]=calculateLineIntersectionCoordinates(15, 12.5, 10, 20, 10,15, 20, 15); //For an input of: 15, 12.5, 10, 20, 10,15, 20, 15 Answer should be around: 13, 15
  348.        
  349.     return $theReturn;
  350. }
  351.  
  352. function calculateLineIntersectionCoordinates($l1x1, $l1y1, $l1x2, $l1y2, $l2x1, $l2y1, $l2x2, $l2y2)
  353. // Provide 2 line coordinates and this will calculate where they intersect.
  354. {
  355.   $theReturn["X"]=((((($l1x1*$l1y2)-($l1y1*$l1x2)) * ($l2x1-$l2x2)) - (($l1x1-$l1x2) * (($l2x1*$l2y2) - ($l2y1*$l2x2)))) / ((($l1x1 - $l1x2) * ($l2y1 - $l2y2)) - (($l1y1-$l1y2)*($l2x1-$l2x2))));
  356.   $theReturn["Y"]=((((($l1x1*$l1y2)-($l1y1*$l1x2)) * ($l2y1-$l2y2)) - (($l1y1-$l1y2) * (($l2x1*$l2y2) - ($l2y1*$l2x2)))) / ((($l1x1 - $l1x2) * ($l2y1 - $l2y2)) - (($l1y1-$l1y2)*($l2x1-$l2x2))));
  357.  
  358.   return $theReturn;
  359. }
  360.  
  361. function outputDebugValues($debugArray, $flatDump=false)
  362. {
  363.  
  364.   if ($flatDump) {
  365.     print_o($debugArray);
  366.     return 0;
  367.   }
  368.  
  369.   $outputText="";
  370.   foreach ($debugArray as $nodeIndex => $nodeSet)
  371.   {
  372.     $outputText[$nodeIndex]["Link"]="Calculate Order: " . "Node " . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex1"] . " to Node " . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex2"];
  373.     $outputText[$nodeIndex]["Node " . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex1"]]["Position"]="X, Y: [" . $debugArray[$nodeIndex]["rssiintersect"]["node" . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex1"] . "X"] . ", " . $debugArray[$nodeIndex]["rssiintersect"]["node" . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex1"]. "Y"] . "]";
  374.     $outputText[$nodeIndex]["Node " . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex1"]]["Radius"]="R: [" . $debugArray[$nodeIndex]["rssiintersect"]["node" . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex1"] . "R"] . "]";
  375.     $outputText[$nodeIndex]["Node " . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex2"]]["Position"]="X, Y: [" . $debugArray[$nodeIndex]["rssiintersect"]["node" . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex2"] . "X"] . ", " . $debugArray[$nodeIndex]["rssiintersect"]["node" . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex2"]. "Y"] . "]";
  376.     $outputText[$nodeIndex]["Node " . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex2"]]["Radius"]="R: [" . $debugArray[$nodeIndex]["rssiintersect"]["node" . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex2"] . "R"] . "]";
  377.    
  378.     $outputText[$nodeIndex]["Relative View"]["Mid-Point Coordinate"]="X, Y: [" . $debugArray[$nodeIndex]["cartesiandeltas"]["midx"] . ", " . $debugArray[$nodeIndex]["cartesiandeltas"]["midy"] . "]";
  379.     $outputText[$nodeIndex]["Relative View"]["Mid-Point Distance"]="Distance: [" . $debugArray[$nodeIndex]["cartesiandeltas"]["distance"] . "]";
  380.     $outputText[$nodeIndex]["Relative View"]["Relative Cartesian Vector"]="X, Y: [" . $debugArray[$nodeIndex]["rssiintersect"]["scannerVectorX"] . ", " . $debugArray[$nodeIndex]["rssiintersect"]["scannerVectorY"] . "]";
  381.     $outputText[$nodeIndex]["Relative View"]["Relative Angle Measure"]="Degrees: [" . $debugArray[$nodeIndex]["rssiintersect"]["angleDeg"] . "]";
  382.     $outputText[$nodeIndex]["Relative View"]["Relative Angle Calculator"]="Radians: [" . $debugArray[$nodeIndex]["rssiintersect"]["angleRad"] . "]";
  383.     $outputText[$nodeIndex]["Relative View"]["Relative Quadrant"]="Sector: [" . $debugArray[$nodeIndex]["rssiintersect"]["relativeQuadrant"] . "]";
  384.     $outputText[$nodeIndex]["Relative View"]["Relative Quadrant Angle"]="Radians: [" . $debugArray[$nodeIndex]["rssiintersect"]["angle + angleB"] . "]";
  385.    
  386.     $outputText[$nodeIndex]["Radii"]["Mid-Point Coordinate"]="X, Y: [" . $debugArray[$nodeIndex]["rssiintersect"]["radiiMidpointX"] . ", " . $debugArray[$nodeIndex]["rssiintersect"]["radiimidpointY"] . "]";
  387.     $outputText[$nodeIndex]["Radii"]["Mid-Point Distance"]="Distance: [" . $debugArray[$nodeIndex]["rssiintersect"]["radiiMidPointDistance"] . "]";
  388.     $outputText[$nodeIndex]["Radii Intersection"]["Intersect Positive Coordinate"]="X, Y: [" . $debugArray[$nodeIndex]["rssiintersect"]["radiiIntersectPX"] . ", " . $debugArray[$nodeIndex]["rssiintersect"]["radiiIntersectPY"] . "]";
  389.     $outputText[$nodeIndex]["Radii Intersection"]["Intersect Negative Coordinate"]="X, Y: [" . $debugArray[$nodeIndex]["rssiintersect"]["radiiIntersectNX"] . ", " . $debugArray[$nodeIndex]["rssiintersect"]["radiiIntersectNY"] . "]";
  390.  
  391.   }
  392.  
  393.   foreach ($debugArray as $nodeIndex => $nodeSet)
  394.   {
  395.     $outputText["Link List Calculation Order"][]=$nodeIndex;
  396.   }
  397.  
  398.   foreach ($debugArray as $nodeIndex => $nodeSet)
  399.   {
  400.     $outputText["Calculation Data"]["Node " . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex1"]]="X, Y, R: [" . $debugArray[$nodeIndex]["rssiintersect"]["node" . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex1"] . "X"] . ", " . $debugArray[$nodeIndex]["rssiintersect"]["node" . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex1"]. "Y"] . ", " . $debugArray[$nodeIndex]["rssiintersect"]["node" . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex1"]. "R"] . "]";
  401.     $outputText["Calculation Data"]["Node " . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex2"]]="X, Y, R: [" . $debugArray[$nodeIndex]["rssiintersect"]["node" . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex2"] . "X"] . ", " . $debugArray[$nodeIndex]["rssiintersect"]["node" . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex2"]. "Y"] . ", " . $debugArray[$nodeIndex]["rssiintersect"]["node" . $debugArray[$nodeIndex]["rssiintersect"]["nodeIndex2"]. "R"] . "]";
  402.   }
  403.  
  404.   print_o($outputText);
  405. }
  406.  
  407. if (!function_exists('getQueryData')) {
  408.   function getQueryData($dataToGet, $queryIndex=-1, $noFilter=false, $requestPriority="get", $filterType=FILTER_SANITIZE_SPECIAL_CHARS)
  409.   {
  410.     // WARNING: Setting $filterInput to false WILL NOT sanitize any data.
  411.     if ($noFilter=="") { $noFilter=false; }
  412.     if ($filterType=="") { $filterType=FILTER_SANITIZE_SPECIAL_CHARS; }
  413.     $requestPriority=strtolower($requestPriority);
  414.    
  415.     $outputString="";
  416.     if ($queryIndex > -1) {
  417.       if ((isset($_REQUEST[$dataToGet][$queryIndex])) && (is_array($_REQUEST[$dataToGet]))) {
  418.         if ($noFilter!=true) {
  419.           if ($requestPriority=="get") {
  420.             $outputString = filter_input(INPUT_GET, $dataToGet, $filterType, FILTER_REQUIRE_ARRAY);
  421.             $outputString=$outputString[$queryIndex];
  422.             if ($outputString=="") {
  423.               $outputString = filter_input(INPUT_POST, $dataToGet, $filterType, FILTER_REQUIRE_ARRAY);
  424.               $outputString=$outputString[$queryIndex];
  425.             }
  426.           } else {
  427.             $outputString = filter_input(INPUT_POST, $dataToGet, $filterType, FILTER_REQUIRE_ARRAY);
  428.             $outputString=$outputString[$queryIndex];
  429.             if ($outputString=="") {
  430.               $outputString = filter_input(INPUT_GET, $dataToGet, $filterType, FILTER_REQUIRE_ARRAY);
  431.               $outputString=$outputString[$queryIndex];
  432.             }
  433.           }
  434.         } else {
  435.           $outputString=$_REQUEST[$dataToGet][$queryIndex];
  436.         }
  437.       } else {
  438.         if ($noFilter!=true) {
  439.           if ($requestPriority=="get") {
  440.             $outputString = filter_input(INPUT_GET, $dataToGet, $filterType);
  441.             if ($outputString=="") {
  442.               $outputString = filter_input(INPUT_POST, $dataToGet, $filterType);
  443.             }
  444.           } else {
  445.             $outputString = filter_input(INPUT_POST, $dataToGet, $filterType);
  446.             if ($outputString=="") {
  447.               $outputString = filter_input(INPUT_GET, $dataToGet, $filterType);
  448.             }
  449.           }
  450.         } else {
  451.           $outputString=$_REQUEST[$dataToGet];
  452.         }
  453.       }
  454.     } else {
  455.       if ($noFilter!=true) {
  456.         if ($requestPriority=="get") {
  457.           $outputString = filter_input(INPUT_GET, $dataToGet, $filterType);
  458.           if ($outputString=="") {
  459.             $outputString = filter_input(INPUT_POST, $dataToGet, $filterType);
  460.           }
  461.         } else {
  462.           $outputString = filter_input(INPUT_POST, $dataToGet, $filterType);
  463.           if ($outputString=="") {
  464.             $outputString = filter_input(INPUT_GET, $dataToGet, $filterType);
  465.           }
  466.         }
  467.       } else {
  468.         $outputString=$_REQUEST[$dataToGet];
  469.       }
  470.     }
  471.  
  472.     return $outputString;
  473.   }
  474. }
  475.  
  476. function print_o($dataToPrint)
  477. {
  478.   echo "<pre>";
  479.   print_r($dataToPrint);
  480.   echo "</pre>";
  481. }
  482.  
  483. /*
  484. OLD STUFF, WILL BE REMOVED SOON.
  485. function calculateRadiiVectorCoordinatesOld2($node1X, $node1Y, $node1R, $node $node2X, $node2Y, $node2R){
  486.     // Work out the triangle
  487.     $dispX = $node2X - $node1X;
  488.     $dispY = $node2Y - $node1Y;
  489.     $scannerDistance = calculateDistance($node1X,$node1Y,$node2X,$node2Y);
  490.    
  491.     // Find the angle between hypotenuse and X plane
  492.     if($dispY == 0){
  493.         // If dispY is 0, then it's a straight line parallel to the Y axis
  494.         $angle = 0;
  495.     } elseif($dispX == 0){
  496.         // If dispX is 0, then it's a straight line tangent to the X axis
  497.         $angle = deg2rad(90);
  498.     } else {
  499.         $angle = atan(abs($dispY)/abs($dispX));
  500.     }
  501.    
  502.     // Find the distance of the midpoint from node1
  503.     $overlap = abs($node1R + $node2R)-$scannerDistance;
  504.     $midPointDistance = $node1R-($overlap/2);
  505.    
  506.     // Make a new triangle and work out the x and y displacement
  507.     $midX = cos($angle)*$midPointDistance;
  508.     $midY = sin($angle)*$midPointDistance;
  509.    
  510.     // Find the intersection points of the two circles
  511.     // Find angleB
  512.     $angleB = acos($midPointDistance/$node1R);
  513.    
  514.     // Find first intersection point
  515.     $intersect1X = $node1R * (cos($angle + $angleB));
  516.     $intersect1Y = $node1R * (sin($angle + $angleB));
  517.    
  518.     // Find second intersection point
  519.     $intersect2X = $node1R * (cos($angle - $angleB));
  520.     $intersect2Y = $node1R * (sin($angle - $angleB));
  521.    
  522.     // Sort the triangle
  523.     if($dispY == 0 && ($node1X > $node2X)){
  524.         // Horizontal line West
  525.         $q = "3&4";
  526.         $midX = $node1X - $midPointDistance;
  527.         $midY = $node1Y;
  528.     } elseif($dispY == 0 && ($node1X < $node2X)){
  529.         // Horizontal line East
  530.         $q = "1&2";
  531.         $midX = $node1X + $midPointDistance;
  532.         $midY = $node1Y;
  533.     } elseif($dispX == 0 && ($node1Y > $node2Y)){
  534.         // Vertical line North
  535.         $q = "1&4";
  536.         $midX = $node1X;
  537.         $midY = $node1Y - $midPointDistance;
  538.     } elseif($dispX == 0 && ($node1Y < $node2Y)){
  539.         // Vertical line South
  540.         $q = "2&3";
  541.         $midX = $node1X;
  542.         $midY = $node1Y + $midPointDistance;
  543.     } elseif($dispX>0 && $dispY<0){
  544.         // node2 is NE of node1
  545.         $q = 1;
  546.         $midX = $node1X + $midX;
  547.         $midY = $node1Y - $midY;
  548.         $intersect1X = $node1X + $intersect1X;
  549.         $intersect1Y = $node1Y - $intersect1Y;
  550.         $intersect2X = $node1X + $intersect2X;
  551.         $intersect2Y = $node1Y - $intersect2Y;
  552.     } elseif($dispX>0 && $dispY>0){
  553.         // node2 is SE of node1
  554.         $q = 2;
  555.         $midX = $node1X + $midX;
  556.         $midY = $node1Y + $midY;
  557.         $intersect1X = $node1X + $intersect1X;
  558.         $intersect1Y = $node1Y + $intersect1Y;
  559.         $intersect2X = $node1X + $intersect2X;
  560.         $intersect2Y = $node1Y + $intersect2Y;
  561.     } elseif($dispX<0 && $dispY>0){
  562.         // node2 is SW of node1
  563.         $q = 3;
  564.         $midX = $node1X - $midX;
  565.         $midY = $node1Y + $midY;
  566.         $intersect1X = $node1X - $intersect1X;
  567.         $intersect1Y = $node1Y + $intersect1Y;
  568.         $intersect2X = $node1X - $intersect2X;
  569.         $intersect2Y = $node1Y + $intersect2Y;
  570.     } elseif($dispX<0 && $dispY<0){
  571.         // node2 is NW of node1
  572.         $q = 4;
  573.         $midX = $node1X - $midX;
  574.         $midY = $node1Y - $midY;
  575.         $intersect1X = $node1X - $intersect1X;
  576.         $intersect1Y = $node1Y - $intersect1Y;
  577.         $intersect2X = $node1X - $intersect2X;
  578.         $intersect2Y = $node1Y - $intersect2Y;
  579.     }
  580.    
  581.     // Return array
  582.     $theReturn["node1"]="[".$node1X.",".$node1Y."]";
  583.     $theReturn["node2"]="[".$node2X.",".$node2Y."]";
  584.     $theReturn["scannerDistance"]=$scannerDistance;
  585.     $theReturn["scannerVector"]="[".$dispX.",".$dispY."]";
  586.     //$theReturn["dispX"]=$dispX;
  587.     //$theReturn["dispY"]=$dispY;
  588.     //$theReturn["angleRad"]=$angle;
  589.     //$theReturn["angleDeg"]=rad2deg($angle);
  590.     $theReturn["quadrant"]=$q;
  591.     //$theReturn["node1R"]=$node1R;
  592.     //$theReturn["node2R"]=$node2R;
  593.     $theReturn["overlap"]=$overlap;
  594.     $theReturn["midPointDistance"]=$midPointDistance;
  595.     $theReturn["midpoint"]="[".$midX.",".$midY."]";
  596.     //$theReturn["angleBDeg"]=rad2deg($angleB);
  597.     //$theReturn["angle + angleB"]=rad2deg($angle + $angleB);
  598.     $theReturn["intersect1"]="[".$intersect1X.",".$intersect1Y."]";
  599.     $theReturn["intersect2"]="[".$intersect2X.",".$intersect2Y."]";
  600.        
  601.     return $theReturn;
  602. }
  603. function calculateRadiiVectorCoordinatesOLD($node1X, $node1Y, $node1R, $node2X, $node2Y, $node2R)
  604. {
  605.     // THIS IS THE OLD VERSION OF THE PREV FUNCTION -- Ryoma
  606.     //Calculates where the cooords of the vector line where it intersects each point's circle's circumference.
  607.     // Some explanation of the paramters -- Ryoma
  608.     //$node1X & $node1Y = scanner1 location
  609.     //$node2X & $node2Y = scanner2 location
  610.     //$node1R is the distance scanner1 detected
  611.     //$node2R is the distance scanner2 detected
  612.     // First find the vector node1.node2
  613.     $dispX = $node1X - $node2X;
  614.     $dispY = $node1Y - $node2Y;
  615.     $vector = array($dispX, ($dispY*-1));
  616.  
  617.   //$angle=acos((($node1X * $node2X) + ($node1Y * $node2Y)) / ((sqrt((pow($node1X, 2)) + (pow($node1Y, 2)))) * (sqrt((pow($node2X, 2)) + (pow($node2Y, 2))))));
  618.   //$angle=acos((($node1X * $node2X) + ($node1Y * $node2Y)) / ((sqrt((pow($node1X, 2)) + (pow($node1Y, 2)))) * (sqrt((pow($node2X, 2)) + (pow($node2Y, 2))))));
  619.   $anglePre=((($node1X * $node2X) + ($node1Y * $node2Y)) / ((sqrt((pow($node1X, 2)) + (pow($node1Y, 2)))) * (sqrt((pow($node2X, 2)) + (pow($node2Y, 2))))));
  620.   $angle=acos($anglePre);
  621.   //$angle=acos(-0.5); //
  622.  
  623.     // Get the angle from node1 to node2
  624.   /*if ($dispX==0) { //Don't divide by 0... world will end.
  625.     if ($node2Y > $node1Y) {
  626.       $angle = (pi()/2)*-1;
  627.     } else {
  628.       $angle = pi()/2;
  629.     }
  630.   } else {
  631.     $angle = atan($dispY / $dispX);
  632.   } // */
  633.  
  634.   //$angle = atan($dispY / $dispX);
  635.    
  636.   /* if ($angle > 0) {
  637.     $angle= abs($angle);
  638.   } else {
  639.     $angle= (2 * pi())+$angle;
  640.   } // *./
  641.  
  642.     //$angleRads=$angle* (180/pi());
  643.   $clockwiseRotation=true;
  644.  
  645.   if ($node1X==$node2X && $node1Y==$node2Y) {
  646.     $angle=0; // or 360
  647.   } else {
  648.   if ($clockwiseRotation) {
  649.       if ($dispX < 0) {
  650.         $angle=atan2($dispX,$dispY);
  651.         $angleDegs=rad2deg($angle*-1);
  652.       } else {
  653.         $angle=(atan2($dispX,$dispY)*-1) + (2 * pi());
  654.         $angleDegs=rad2deg($angle);
  655.         if ($angleDegs==360) {
  656.           $angleDegs=0;
  657.         }
  658.       }
  659.     } else {
  660.       if ($dispX < 0) {
  661.         $angle=atan2($dispX,$dispY) + (2 * pi());
  662.         $angleDegs=rad2deg($angle);
  663.       } else {
  664.         $angle=atan2($dispX,$dispY);
  665.         $angleDegs=rad2deg($angle);
  666.       }
  667.     }
  668.   }
  669.  
  670.   $anglePre=((($node1X * $node2X) + ($node1Y * $node2Y)) / ((sqrt((pow($node1X, 2)) + (pow($node1Y, 2)))) * (sqrt((pow($node2X, 2)) + (pow($node2Y, 2))))));
  671.  
  672.     // $node1R location
  673.     $nodeRadius1CoordX = sin($angle)*$node1R;
  674.     $nodeRadius1CoordY = cos($angle)*$node1R;
  675.    
  676.     // Find the distance between node1.node2
  677.     $scannerDistance = abs(sqrt(($dispX^2)+($dispY^2)));
  678.    
  679.     // I'm going to be lazy and find the distance of Node2R from Node1 to find the location using node1 as a reference point.
  680.     $node2Distance = abs($scannerDistance - $node2R);
  681.     $nodeRadius2CoordX = sin($angle)*$node2Distance;
  682.     $nodeRadius2CoordY = cos($angle)*$node2Distance;
  683.    
  684.     // To calculate the mid point, find the distance of the midpoint from node1
  685.     $midpointDistance = abs((($scannerDistance - ($node1R + $node2R))/2)+$node1R);
  686.     // Now find the X, Y coords
  687.     $midpointX = sin($angle)*$midpointDistance;
  688.     $midpointY = sin($angle)*$midpointDistance;
  689.    
  690.     // Return array
  691.     $theReturn["noder1x"]=$nodeRadius1CoordX;
  692.     $theReturn["noder1y"]=$nodeRadius1CoordY;
  693.     $theReturn["noder2x"]=$nodeRadius2CoordX;
  694.     $theReturn["noder2y"]=$nodeRadius2CoordY;
  695.  
  696.     $theReturn["vectorx"]=$dispX;
  697.     $theReturn["vectory"]=$dispY;
  698.     $theReturn["vectoranglerads"]=$angle;
  699.     $theReturn["vectorangledegrees"]=$angleDegs;
  700.     $theReturn["vectoranglepre"]=$anglePre;
  701.    
  702.     $theReturn["radiimid"]=$midpointDistance;
  703.     $theReturn["radiix"]=$midpointX;
  704.     $theReturn["radiiy"]=$midpointY;
  705.    
  706.     return $theReturn;
  707.  
  708. }
  709. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement