ClarkeRubber

Graphical Calculator

Sep 24th, 2011
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.84 KB | None | 0 0
  1. <?php
  2. function collatz($originalNumber){ //the collatz conjecture function
  3.   $number = $originalNumber;
  4.   do{
  5.   $i++;
  6.       if(1&$number){
  7.         $number = (3*$number)+1;
  8.       }else{
  9.         $number = $number/2;
  10.       }
  11.   }while($number != 1);
  12.   return floor($i);
  13. }
  14.  
  15. //Your function
  16. function yourFunction($x){
  17.   return floor(pow(2, $x)-512);
  18. }
  19.  
  20. echo "Name of Your Function\n"; // :D
  21.  
  22. //Your parameters
  23. $domain = 10; //between 0 and 'x', doesn't plot negative values of X yet. :\
  24. $lineThickness = 2;
  25. $accuracy = 0.01; //lower the number, higher the accuracy
  26.  
  27. //---Begin main program
  28. for($n = 0; $n <= $domain/$accuracy; $n++){
  29.   $i = yourFunction($n*$accuracy); //testing how many steps that it will take for the number $n to reach 1.
  30.   if($i > $maxheight || $n == 0){ //if the result is larger than all the  rest, the new result will become the largest result.
  31.     $maxheight = $i;
  32.   }
  33.   if($i < $minheight || $n == 0){
  34.     $minheight = $i;
  35.   }
  36.   echo $n*$accuracy." = $i\n";
  37.   $x[$n] = $i; //$n is the position along the x-axis, $i is the position along the y-axis.
  38. }
  39. //---begin writing file
  40. unlink("image.ppm"); //file that we will be editing, we are are deleting the file so we have a clean file to work on
  41. $fh = fopen("image.ppm", 'a+'); //creates the image file that we will be editting and opens the file for appending
  42.  
  43. $width = $n; //width of the image is the largest number that was tested
  44. $height = abs($minheight - $maxheight)+1; //height is the largest amount of steps that it took to reach zero
  45.  
  46. $startWrite = "P3\n# image.ppm\n".$width." ".$height."\n255"; //fomatting the ppm file
  47. fwrite($fh, $startWrite);
  48.  
  49. for($k = 0; $k < $width*$height; $k++){
  50.   $CurPx = $k%$n; //basic modulus to decide when a new line must be made
  51.   if($k%5 == 0){ //basic formatting decides when a new line must be placed
  52.     $Nl = " \n";
  53.   }else{
  54.     $Nl = " ";
  55.   }
  56.   if($CurPx == 0){
  57.     $CurPy++;
  58.   }
  59.   $dist = $x[$CurPx] - ($maxheight-$CurPy+1);
  60.   if($dist > -$lineThickness && $dist < $lineThickness){ //basic algebra, fuck I hate zero being counted as an integer
  61.     $red = "255"; //if the if statement above reflects the array which was made on line 23, the pixel will be white
  62.     $green = "255";
  63.     $blue = "255";
  64.   }elseif($CurPy == $maxheight+1){
  65.     $red = "000";
  66.     $green = "255";
  67.     $blue = "000";
  68.   }elseif($CurPy < $maxheight+1){
  69.     $clr = str_pad(floor(255*($CurPy/$maxheight)), 3, "0", STR_PAD_LEFT);
  70.     $red = $clr;
  71.     $green = "000";
  72.     $blue = "000";
  73.   }else{
  74.     $clr = str_pad(255-floor(255*(($CurPy-$maxheight)/$maxheight)), 3, "0", STR_PAD_LEFT);
  75.     $red = "000"; //else, it will be black
  76.     $green = $clr;
  77.     $blue = "000";
  78.  
  79.   }
  80.   $write = $Nl."$red $green $blue"; //the line that is to be outputted
  81.     fwrite($fh, $write); //write the pixel to the file.
  82. }
  83. fclose($fh); //close the file, done!
  84. ?>
Advertisement
Add Comment
Please, Sign In to add comment