Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. <?php
  2. //fire on random place
  3. $connect = mysql_connect("localhost","root","") or die ("Couldn't connect!");
  4. mysql_select_db("detection") or die ("Couldn't find db");
  5. $counter=0;
  6. $max=0;
  7. $xsum=0;
  8. $ysum=0;
  9. $xres=0;
  10. $yres=0;
  11. $angle1=0;
  12.  
  13. //localize territory and shoot
  14. $query=mysql_query ("SELECT MAX(x) AS maxx,MAX(y) AS maxy FROM nodes");
  15. while ($row=mysql_fetch_assoc($query))
  16. {
  17. $maxx=$row['maxx'];
  18. $maxy=$row['maxy'];
  19. }
  20.  
  21. $xrand=$maxx-rand(0,$maxx);
  22. $yrand=$maxy-rand(0,$maxy);
  23. echo "shot location: ";echo "<br>";
  24. echo "x: ".$xrand;
  25. echo "<br>";
  26. echo "y: ".$yrand;
  27.  
  28. $shotpower=110+rand(-10,10);
  29. echo "<br>";
  30. echo "Shot power:".$shotpower;
  31.  
  32. $timestamp = round(microtime(true) * 1000);
  33. echo "<br>"."actual timestamp(ms): ";
  34. echo $timestamp;
  35. $temperature=20+rand(-5,5);
  36. echo "<br>"."temperature(oC): ";
  37. echo $temperature;
  38. echo "<br>"."sound speed(m/s): ";
  39. $speed=331.5+0.6*$temperature;
  40. echo $speed;
  41. echo "<br>";
  42. echo "<br>";
  43.  
  44. $query=mysql_query ("SELECT * FROM nodes ORDER BY RAND()");
  45. while ($row=mysql_fetch_assoc($query))
  46. {
  47. $nodid=$row['nodid'];
  48. $xdb=$row['x'];
  49. $ydb=$row['y'];
  50.  
  51. $x1 = $xrand;
  52. $y1 = $yrand;
  53.  
  54. $x2 = $xdb;
  55. $y2 = $ydb;
  56.  
  57. $x = ( pow($x2-$x1,2));
  58. $y = ( pow($y2-$y1,2));
  59.  
  60. $distance = ( sqrt($x + $y));
  61. $distance = round($distance,2);
  62.  
  63. $traveltime=($distance/$speed)*1000;
  64. $volumedecrease=$shotpower-$distance;
  65.  
  66. //$angle = rad2deg(atan2($y1-$y2,$x1-$x2));
  67. //$angle = atan2($y1-$y2,$x1-$x2);
  68. $angle=($y1-$y2)/($x1-$x2);
  69.  
  70. //insert a little measure error
  71. $angle = $angle+(rand(-1,1)/50);
  72. $angle = round($angle,2);
  73.  
  74. echo "<br>";
  75.  
  76. if ($distance<$shotpower)
  77. {
  78. echo "<font color=red>";
  79. echo "NodID:"."$nodid "."Nodx:$xdb "."Nody:$ydb "; //,";
  80. echo "Distance: $distance";
  81. echo " Volume decrease: ";
  82. echo "$volumedecrease";
  83. $newtimestamp=$timestamp+$traveltime;
  84. echo " TimeStamp: "."$newtimestamp";
  85. echo " Angle:";
  86. echo "$angle";
  87. echo "</font>";
  88. $counter++;
  89. //create list of nodes
  90. $nodesneer[]=$nodid;
  91.  
  92. $name="ac".$nodid;
  93.  
  94. //create new table for each nod
  95. $createtable = mysql_query ("
  96. CREATE TABLE IF NOT EXISTS `".$name."` (
  97. `aid` int(5) NOT NULL,
  98. `x` double NOT NULL,
  99. `y` double NOT NULL,
  100. `volumedecrease` double NOT NULL,
  101. `timestamp` double NOT NULL,
  102. `angle` double NOT NULL,
  103. PRIMARY KEY (`aid`)
  104. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  105. ");
  106.  
  107. //put own data in table
  108. $queryinsertrow = mysql_query ("
  109. INSERT INTO $name VALUES ('$nodid','$xdb','$ydb','$volumedecrease','$newtimestamp','$angle')
  110. ");
  111.  
  112. //retreive
  113. //select neighbours
  114. $query1=mysql_query ("SELECT * FROM station".$nodid." WHERE isnb=1 ORDER BY RAND()");
  115. while ($row1=mysql_fetch_assoc($query1))
  116. {
  117. $nodid=$row1['nid'];
  118. $x=$row1['x'];
  119. $y=$row1['y'];
  120. $namme="ac".$nodid;
  121.  
  122. if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$namme."'"))==1) //if table exists
  123. {
  124. $query2=mysql_query ("SELECT * FROM ac".$nodid." ORDER BY RAND() ");
  125. while ($row2=mysql_fetch_assoc($query2))
  126. {
  127. $acaid=$row2['aid'];
  128. $acx=$row2['x'];
  129. $acy=$row2['y'];
  130. $acvolumedecrease=$row2['volumedecrease'];
  131. $actimestamp=$row2['timestamp'];
  132. $acangle=$row2['angle'];
  133. $targettable="ac".$nodid;
  134.  
  135. //put neighbours data in table
  136. $queryinsertrow = mysql_query ("
  137. INSERT INTO $name VALUES ('$acaid','$acx','$acy','$acvolumedecrease','$actimestamp','$acangle')
  138. ");
  139. }
  140. }
  141. }
  142. }
  143. else
  144. {
  145. echo "NodID:"."$nodid "."Nodx:$xdb "."Nody:$ydb "; //,";
  146. echo "Distance: $distance";
  147. }
  148. }
  149. echo "<br>";echo "<br>";echo "<br>";
  150. foreach($nodesneer as $nodelist)
  151. {
  152. //echo "$nodelist: ";
  153. //share number of rows - only with in a circle.
  154. $query3=mysql_query ("SELECT COUNT(*) AS nu FROM ac".$nodelist." ");
  155. while ($row3=mysql_fetch_assoc($query3))
  156. {
  157. $num=$row3['nu'];
  158.  
  159. if ($num>$max)
  160. {
  161. $max=$num;
  162. $maxid=$nodelist;
  163. }
  164. }
  165. }
  166. echo "<br>";echo "<br>";
  167. echo "Node with highest number of data (leader)";
  168. echo "$maxid: $max";
  169.  
  170. echo "<br>";
  171. echo "<br>";
  172. echo "Nodes in the ring of the shot: ".$counter ;
  173. echo " (";
  174. foreach($nodesneer as $nodelist)
  175. {
  176. echo $nodelist.",";
  177. }
  178. echo ")";
  179.  
  180. echo "<br>";
  181. echo "<br>";
  182. echo "PreRESULTS:";
  183.  
  184. $query4=mysql_query ("SELECT * FROM ac".$maxid." ");
  185. while ($row4=mysql_fetch_assoc($query4))
  186. {
  187. $aid=$row4['aid'];
  188. $xres=$row4['x'];
  189. $yres=$row4['y'];
  190. $angleres=$row4['angle'];
  191.  
  192. $x2=$xres;
  193. $y2=$yres;
  194. $angle2=$angleres;
  195. //if (isset ($xres, $yres) )
  196.  
  197. if ($xres>0 AND $yres>0)
  198. {
  199.  
  200. //calculate intersection
  201. $b1=$y1-$angle1*$x1;
  202. $b2=$y2-$angle2*$x2;
  203. //($angle1*$xmetsz+$b1)-($angle2*xmetsz+$b2)=0;
  204. $xmetsz=($b2-$b1)/($angle1-$angle2);
  205. $ymetsz=$angle1*$xmetsz+$b1;
  206.  
  207. $xmetszr=round($xmetsz,2);
  208. $ymetszr=round($ymetsz,2);
  209. echo "x: $xmetszr ";
  210. echo "y: $ymetszr ";
  211.  
  212. $y1=$yres;
  213. $x1=$xres;
  214. $angle1=$angleres;
  215. }
  216.  
  217. $xsum+=$xmetsz;
  218. $ysum+=$ymetsz;
  219. }
  220. $numrows = mysql_num_rows($query4);
  221. $xavg=$xsum/$numrows; $xavg=round($xavg,2);
  222. $yavg=$ysum/$numrows; $yavg=round($yavg,2);
  223. echo "<br>"; echo "<br>";echo "Calculated: ";echo "<br>";
  224. echo "x: $xavg";echo "<br>";
  225. echo "y: $yavg";echo "<br>";
  226.  
  227. //delete temporary files/tables from nodes
  228. foreach($nodesneer as $nodelist)
  229. {
  230. //echo "$nodelist ";
  231. $query5=mysql_query ("DROP TABLE ac".$nodelist." ");
  232. }
  233.  
  234. $xerr=round($xavg-$xrand,2);
  235. $yerr=round($yavg-$yrand,2);
  236. echo "<br>";
  237.  
  238. echo "Error: x: $xerr, y: $yerr";
  239. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement