ClarkeRubber

Projectile Motion

Oct 23rd, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.04 KB | None | 0 0
  1. <?php
  2. function projectileMotion($x, $velocity1, $velocity2, $gravity){
  3.     //$velocity1 is the initial upwards velocity (eg 10)
  4.     //$velocity2 is the initial horizontal velocity (eg 50)
  5.     //$gravity is the downwards acceleration (eg 9.8)
  6.     //$y = $velocity1*$time + 0.5*$gravity*$time^2; //s = ut + 1/2*at^2
  7.     //shit.... working out the X-pos isn't going to work.
  8.     // x = v*t, y = ut + 0.5*at^2
  9.     // t = x/v, therefore; y = u(x/v) + 0.5a(x/v)^2
  10.     return $velocity1*($x/$velocity2) + 0.5*$gravity*pow(($x/$velocity2), 2);
  11. }
  12.  
  13. $velocity1 = 50;
  14. $velocity2 = 5;
  15. $gravity = -9.8;
  16. //THE FILE PRINTER STARTS HERE
  17.  
  18. //Your parameters
  19. $domain = array("neg" => 0, "pos" => 75); //Please don't use decimal values. :\
  20. $lineThickness = 2; //Decimals are cool here.
  21. $accuracy = 0.1; //lower the number = higher the accuracy, longer the processing time, larger the image. Please only use values of 10^x
  22.  
  23. //---Begin main program
  24. $maxheight = 0;
  25. $minheight = 0;
  26. for($n = -$domain["neg"]/$accuracy; $n <= $domain["pos"]/$accuracy; $n++){
  27.   $j++;
  28.   $i = projectileMotion(abs($n*$accuracy), $velocity1, $velocity2, $gravity); //This is where the values are loaded
  29.   if($i > $maxheight || $j == 0){ //if the result is larger than all the  rest, the new result will become the largest result.
  30.     $maxheight = $i;
  31.   }
  32.   if($i < $minheight || $j == 0){
  33.     $minheight = $i;
  34.   }
  35.   echo $n*$accuracy." = $i\n";
  36.   $x[$j] = $i; //$n is the position along the x-axis, $i is the position along the y-axis.
  37. }
  38. $x[0] = $x[1]; //<same as the line below me>
  39. $x[$j+1] = $x[$j]; //This is because of line 72, where $dif is set. It will try to reference an array with no entry, so dif would otherwise become $x[$CurPx]
  40. //var_dump($x);
  41.  
  42. //---begin writing file
  43. unlink("image.ppm"); //file that we will be editing, we are are deleting the file so we have a clean file to work on
  44. $fh = fopen("image.ppm", 'a+'); //creates the image file that we will be editting and opens the file for appending
  45.  
  46. $domain["dif"] = $domain["neg"]+$domain["pos"];
  47. $width = floor($domain["dif"]/$accuracy); //width of the image is the largest number that was tested
  48. $height = floor(abs($minheight - $maxheight)+1); //height is the largest amount of steps that it took to reach zero
  49.  
  50. $startWrite = "P3\n# image.ppm\n".$width." ".$height."\n255"; //fomatting the ppm file
  51. fwrite($fh, $startWrite);
  52.  
  53. $j = 0;
  54. for($k = -$domain["neg"]/$accuracy; $k < $width*$height; $k++){
  55.   $CurPx = $j%$width; //basic modulus to decide when a new line must be made
  56.   if($j%5 == 0){ //basic formatting decides when a new line must be placed
  57.     $Nl = " \n";
  58.   }else{
  59.     $Nl = " ";
  60.   }
  61.   if($CurPx == 0){
  62.     $CurPy++;
  63.   }
  64.   $dist = $x[$CurPx] - ($maxheight-$CurPy+1);
  65.   $dif = abs($x[$CurPx]-$x[$CurPx+1])+$lineThickness;
  66.   if($dist > -$dif && $dist < $dif){ //basic algebra, fuck I hate zero being counted as an integer
  67.     $red = "255"; //if the if statement above reflects the array which was made on line 23, the pixel will be white
  68.     $green = "255";
  69.     $blue = "255";
  70.   }elseif($CurPy >= $maxheight+1){ //if Y >= 0
  71.     $clr = str_pad(abs(255-abs(floor(255*(($CurPy-$maxheight-2)/$minheight)))), 3, "0", STR_PAD_LEFT);
  72.     if(0 == $CurPx%($width/$domain["dif"])){
  73.       $red = "128";
  74.       $green = "128";
  75.       $blue = "128";
  76.     }elseif($CurPx <= $domain["neg"]/$accuracy){
  77.       $blue = $clr;
  78.       $red = $clr;
  79.       $green = "000";
  80.     }else{
  81.       $green = $clr;
  82.       $blue = "000";
  83.       $red = "000";
  84.     }
  85.   }elseif($CurPy < $maxheight+1){ //if Y < 0
  86.     $clr = str_pad(abs(floor(255*($CurPy/$maxheight))), 3, "0", STR_PAD_LEFT);
  87.     if(0 == $CurPx%($width/$domain["dif"])){
  88.       $red = "128";
  89.       $green = "128";
  90.       $blue = "128";
  91.     }elseif($CurPx <= $domain["neg"]/$accuracy){
  92.       $blue = $clr;
  93.       $red = "000";
  94.       $green = "000";
  95.     }else{
  96.       $red = $clr;
  97.       $blue = "000";
  98.       $green = "000";
  99.     }
  100.   }
  101.   $write = "$Nl$red $green $blue"; //the line that is to be outputted
  102.     fwrite($fh, $write); //write the pixel to the file.
  103.     $j++;
  104. }
  105. fclose($fh); //close the file, done!
  106. ?>
  107.  
Advertisement
Add Comment
Please, Sign In to add comment