Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function collatz($originalNumber){ //the collatz conjecture function
- $number = $originalNumber;
- do{
- $i++;
- if(1&$number){
- $number = (3*$number)+1;
- }else{
- $number = $number/2;
- }
- }while($number != 1);
- return floor($i);
- }
- //Your function
- function yourFunction($x){
- return floor(pow(2, $x)-512);
- }
- echo "Name of Your Function\n"; // :D
- //Your parameters
- $domain = 10; //between 0 and 'x', doesn't plot negative values of X yet. :\
- $lineThickness = 2;
- $accuracy = 0.01; //lower the number, higher the accuracy
- //---Begin main program
- for($n = 0; $n <= $domain/$accuracy; $n++){
- $i = yourFunction($n*$accuracy); //testing how many steps that it will take for the number $n to reach 1.
- if($i > $maxheight || $n == 0){ //if the result is larger than all the rest, the new result will become the largest result.
- $maxheight = $i;
- }
- if($i < $minheight || $n == 0){
- $minheight = $i;
- }
- echo $n*$accuracy." = $i\n";
- $x[$n] = $i; //$n is the position along the x-axis, $i is the position along the y-axis.
- }
- //---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
- $width = $n; //width of the image is the largest number that was tested
- $height = 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);
- for($k = 0; $k < $width*$height; $k++){
- $CurPx = $k%$n; //basic modulus to decide when a new line must be made
- if($k%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);
- if($dist > -$lineThickness && $dist < $lineThickness){ //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){
- $red = "000";
- $green = "255";
- $blue = "000";
- }elseif($CurPy < $maxheight+1){
- $clr = str_pad(floor(255*($CurPy/$maxheight)), 3, "0", STR_PAD_LEFT);
- $red = $clr;
- $green = "000";
- $blue = "000";
- }else{
- $clr = str_pad(255-floor(255*(($CurPy-$maxheight)/$maxheight)), 3, "0", STR_PAD_LEFT);
- $red = "000"; //else, it will be black
- $green = $clr;
- $blue = "000";
- }
- $write = $Nl."$red $green $blue"; //the line that is to be outputted
- fwrite($fh, $write); //write the pixel to the file.
- }
- fclose($fh); //close the file, done!
- ?>
Advertisement
Add Comment
Please, Sign In to add comment