Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function projectileMotion($x, $velocity1, $velocity2, $gravity){
- //$velocity1 is the initial upwards velocity (eg 10)
- //$velocity2 is the initial horizontal velocity (eg 50)
- //$gravity is the downwards acceleration (eg 9.8)
- //$y = $velocity1*$time + 0.5*$gravity*$time^2; //s = ut + 1/2*at^2
- //shit.... working out the X-pos isn't going to work.
- // x = v*t, y = ut + 0.5*at^2
- // t = x/v, therefore; y = u(x/v) + 0.5a(x/v)^2
- return $velocity1*($x/$velocity2) + 0.5*$gravity*pow(($x/$velocity2), 2);
- }
- $velocity1 = 50;
- $velocity2 = 5;
- $gravity = -9.8;
- //THE FILE PRINTER STARTS HERE
- //Your parameters
- $domain = array("neg" => 0, "pos" => 75); //Please don't use decimal values. :\
- $lineThickness = 2; //Decimals are cool here.
- $accuracy = 0.1; //lower the number = higher the accuracy, longer the processing time, larger the image. Please only use values of 10^x
- //---Begin main program
- $maxheight = 0;
- $minheight = 0;
- for($n = -$domain["neg"]/$accuracy; $n <= $domain["pos"]/$accuracy; $n++){
- $j++;
- $i = projectileMotion(abs($n*$accuracy), $velocity1, $velocity2, $gravity); //This is where the values are loaded
- if($i > $maxheight || $j == 0){ //if the result is larger than all the rest, the new result will become the largest result.
- $maxheight = $i;
- }
- if($i < $minheight || $j == 0){
- $minheight = $i;
- }
- echo $n*$accuracy." = $i\n";
- $x[$j] = $i; //$n is the position along the x-axis, $i is the position along the y-axis.
- }
- $x[0] = $x[1]; //<same as the line below me>
- $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]
- //var_dump($x);
- //---begin writing file
- unlink("image.ppm"); //file that we will be editing, we are are deleting the file so we have a clean file to work on
- $fh = fopen("image.ppm", 'a+'); //creates the image file that we will be editting and opens the file for appending
- $domain["dif"] = $domain["neg"]+$domain["pos"];
- $width = floor($domain["dif"]/$accuracy); //width of the image is the largest number that was tested
- $height = floor(abs($minheight - $maxheight)+1); //height is the largest amount of steps that it took to reach zero
- $startWrite = "P3\n# image.ppm\n".$width." ".$height."\n255"; //fomatting the ppm file
- fwrite($fh, $startWrite);
- $j = 0;
- for($k = -$domain["neg"]/$accuracy; $k < $width*$height; $k++){
- $CurPx = $j%$width; //basic modulus to decide when a new line must be made
- if($j%5 == 0){ //basic formatting decides when a new line must be placed
- $Nl = " \n";
- }else{
- $Nl = " ";
- }
- if($CurPx == 0){
- $CurPy++;
- }
- $dist = $x[$CurPx] - ($maxheight-$CurPy+1);
- $dif = abs($x[$CurPx]-$x[$CurPx+1])+$lineThickness;
- if($dist > -$dif && $dist < $dif){ //basic algebra, fuck I hate zero being counted as an integer
- $red = "255"; //if the if statement above reflects the array which was made on line 23, the pixel will be white
- $green = "255";
- $blue = "255";
- }elseif($CurPy >= $maxheight+1){ //if Y >= 0
- $clr = str_pad(abs(255-abs(floor(255*(($CurPy-$maxheight-2)/$minheight)))), 3, "0", STR_PAD_LEFT);
- if(0 == $CurPx%($width/$domain["dif"])){
- $red = "128";
- $green = "128";
- $blue = "128";
- }elseif($CurPx <= $domain["neg"]/$accuracy){
- $blue = $clr;
- $red = $clr;
- $green = "000";
- }else{
- $green = $clr;
- $blue = "000";
- $red = "000";
- }
- }elseif($CurPy < $maxheight+1){ //if Y < 0
- $clr = str_pad(abs(floor(255*($CurPy/$maxheight))), 3, "0", STR_PAD_LEFT);
- if(0 == $CurPx%($width/$domain["dif"])){
- $red = "128";
- $green = "128";
- $blue = "128";
- }elseif($CurPx <= $domain["neg"]/$accuracy){
- $blue = $clr;
- $red = "000";
- $green = "000";
- }else{
- $red = $clr;
- $blue = "000";
- $green = "000";
- }
- }
- $write = "$Nl$red $green $blue"; //the line that is to be outputted
- fwrite($fh, $write); //write the pixel to the file.
- $j++;
- }
- fclose($fh); //close the file, done!
- ?>
Advertisement
Add Comment
Please, Sign In to add comment