Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //fire on random place
- $connect = mysql_connect("localhost","root","") or die ("Couldn't connect!");
- mysql_select_db("detection") or die ("Couldn't find db");
- $counter=0;
- $max=0;
- $xsum=0;
- $ysum=0;
- $xres=0;
- $yres=0;
- $angle1=0;
- //localize territory and shoot
- $query=mysql_query ("SELECT MAX(x) AS maxx,MAX(y) AS maxy FROM nodes");
- while ($row=mysql_fetch_assoc($query))
- {
- $maxx=$row['maxx'];
- $maxy=$row['maxy'];
- }
- $xrand=$maxx-rand(0,$maxx);
- $yrand=$maxy-rand(0,$maxy);
- echo "shot location: ";echo "<br>";
- echo "x: ".$xrand;
- echo "<br>";
- echo "y: ".$yrand;
- $shotpower=110+rand(-10,10);
- echo "<br>";
- echo "Shot power:".$shotpower;
- $timestamp = round(microtime(true) * 1000);
- echo "<br>"."actual timestamp(ms): ";
- echo $timestamp;
- $temperature=20+rand(-5,5);
- echo "<br>"."temperature(oC): ";
- echo $temperature;
- echo "<br>"."sound speed(m/s): ";
- $speed=331.5+0.6*$temperature;
- echo $speed;
- echo "<br>";
- echo "<br>";
- $query=mysql_query ("SELECT * FROM nodes ORDER BY RAND()");
- while ($row=mysql_fetch_assoc($query))
- {
- $nodid=$row['nodid'];
- $xdb=$row['x'];
- $ydb=$row['y'];
- $x1 = $xrand;
- $y1 = $yrand;
- $x2 = $xdb;
- $y2 = $ydb;
- $x = ( pow($x2-$x1,2));
- $y = ( pow($y2-$y1,2));
- $distance = ( sqrt($x + $y));
- $distance = round($distance,2);
- $traveltime=($distance/$speed)*1000;
- $volumedecrease=$shotpower-$distance;
- //$angle = rad2deg(atan2($y1-$y2,$x1-$x2));
- //$angle = atan2($y1-$y2,$x1-$x2);
- $angle=($y1-$y2)/($x1-$x2);
- //insert a little measure error
- $angle = $angle+(rand(-1,1)/50);
- $angle = round($angle,2);
- echo "<br>";
- if ($distance<$shotpower)
- {
- echo "<font color=red>";
- echo "NodID:"."$nodid "."Nodx:$xdb "."Nody:$ydb "; //,";
- echo "Distance: $distance";
- echo " Volume decrease: ";
- echo "$volumedecrease";
- $newtimestamp=$timestamp+$traveltime;
- echo " TimeStamp: "."$newtimestamp";
- echo " Angle:";
- echo "$angle";
- echo "</font>";
- $counter++;
- //create list of nodes
- $nodesneer[]=$nodid;
- $name="ac".$nodid;
- //create new table for each nod
- $createtable = mysql_query ("
- CREATE TABLE IF NOT EXISTS `".$name."` (
- `aid` int(5) NOT NULL,
- `x` double NOT NULL,
- `y` double NOT NULL,
- `volumedecrease` double NOT NULL,
- `timestamp` double NOT NULL,
- `angle` double NOT NULL,
- PRIMARY KEY (`aid`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
- ");
- //put own data in table
- $queryinsertrow = mysql_query ("
- INSERT INTO $name VALUES ('$nodid','$xdb','$ydb','$volumedecrease','$newtimestamp','$angle')
- ");
- //retreive
- //select neighbours
- $query1=mysql_query ("SELECT * FROM station".$nodid." WHERE isnb=1 ORDER BY RAND()");
- while ($row1=mysql_fetch_assoc($query1))
- {
- $nodid=$row1['nid'];
- $x=$row1['x'];
- $y=$row1['y'];
- $namme="ac".$nodid;
- if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$namme."'"))==1) //if table exists
- {
- $query2=mysql_query ("SELECT * FROM ac".$nodid." ORDER BY RAND() ");
- while ($row2=mysql_fetch_assoc($query2))
- {
- $acaid=$row2['aid'];
- $acx=$row2['x'];
- $acy=$row2['y'];
- $acvolumedecrease=$row2['volumedecrease'];
- $actimestamp=$row2['timestamp'];
- $acangle=$row2['angle'];
- $targettable="ac".$nodid;
- //put neighbours data in table
- $queryinsertrow = mysql_query ("
- INSERT INTO $name VALUES ('$acaid','$acx','$acy','$acvolumedecrease','$actimestamp','$acangle')
- ");
- }
- }
- }
- }
- else
- {
- echo "NodID:"."$nodid "."Nodx:$xdb "."Nody:$ydb "; //,";
- echo "Distance: $distance";
- }
- }
- echo "<br>";echo "<br>";echo "<br>";
- foreach($nodesneer as $nodelist)
- {
- //echo "$nodelist: ";
- //share number of rows - only with in a circle.
- $query3=mysql_query ("SELECT COUNT(*) AS nu FROM ac".$nodelist." ");
- while ($row3=mysql_fetch_assoc($query3))
- {
- $num=$row3['nu'];
- if ($num>$max)
- {
- $max=$num;
- $maxid=$nodelist;
- }
- }
- }
- echo "<br>";echo "<br>";
- echo "Node with highest number of data (leader)";
- echo "$maxid: $max";
- echo "<br>";
- echo "<br>";
- echo "Nodes in the ring of the shot: ".$counter ;
- echo " (";
- foreach($nodesneer as $nodelist)
- {
- echo $nodelist.",";
- }
- echo ")";
- echo "<br>";
- echo "<br>";
- echo "PreRESULTS:";
- $query4=mysql_query ("SELECT * FROM ac".$maxid." ");
- while ($row4=mysql_fetch_assoc($query4))
- {
- $aid=$row4['aid'];
- $xres=$row4['x'];
- $yres=$row4['y'];
- $angleres=$row4['angle'];
- $x2=$xres;
- $y2=$yres;
- $angle2=$angleres;
- //if (isset ($xres, $yres) )
- if ($xres>0 AND $yres>0)
- {
- //calculate intersection
- $b1=$y1-$angle1*$x1;
- $b2=$y2-$angle2*$x2;
- //($angle1*$xmetsz+$b1)-($angle2*xmetsz+$b2)=0;
- $xmetsz=($b2-$b1)/($angle1-$angle2);
- $ymetsz=$angle1*$xmetsz+$b1;
- $xmetszr=round($xmetsz,2);
- $ymetszr=round($ymetsz,2);
- echo "x: $xmetszr ";
- echo "y: $ymetszr ";
- $y1=$yres;
- $x1=$xres;
- $angle1=$angleres;
- }
- $xsum+=$xmetsz;
- $ysum+=$ymetsz;
- }
- $numrows = mysql_num_rows($query4);
- $xavg=$xsum/$numrows; $xavg=round($xavg,2);
- $yavg=$ysum/$numrows; $yavg=round($yavg,2);
- echo "<br>"; echo "<br>";echo "Calculated: ";echo "<br>";
- echo "x: $xavg";echo "<br>";
- echo "y: $yavg";echo "<br>";
- //delete temporary files/tables from nodes
- foreach($nodesneer as $nodelist)
- {
- //echo "$nodelist ";
- $query5=mysql_query ("DROP TABLE ac".$nodelist." ");
- }
- $xerr=round($xavg-$xrand,2);
- $yerr=round($yavg-$yrand,2);
- echo "<br>";
- echo "Error: x: $xerr, y: $yerr";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement