Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function cubeLatticeArray($a, $b){
- //$a is the radius of the larger sphere, $b is the radius of the smallers spheres that are fitting into the larger sphere.
- //This function will only solve for one eighth of the cube. I will later have this function copy these results over
- //to the other 7 parts of the sphere.
- $radius = pow($a, 2);
- $points[0] = array('x' => $b, 'y' => $b, 'z' => sqrt(pow($a, 2)-pow(2*$b, 2))-$b); //I solved this line for myself, stfu
- $pass = true;
- for($i = 1; $pass == true; $i++){
- $current = end($points);
- if($radius > (pow($current['x']+$b, 2)+pow($current['y']+3*$b, 2)+pow($current['z']+$b, 2))){
- //Move to the next Y cube relative to the last exact position
- $points[$i] = array('x' => $current['x'], 'y' => $current['y']+2*$b, 'z' => $current['z']);
- }elseif($radius > (pow($current['x']+3*$b, 2)+pow(2*$b, 2)+pow($current['z']+$b, 2))){
- //Move to the next X cube relative to the last Z and X position (not Y)
- $points[$i] = array('x' => $current['x']+2*$b, 'y' => $b, 'z' => $current['z']);
- }elseif($radius > (pow(2*$b, 2)+pow(2*$b, 2)+pow($current['z']-3*$b, 2)) && $current['z']-3*$b >= 0){
- //Move to the next Z cube relative to the last Z value (not x or y)
- $points[$i] = array('x' => $b, 'y' => $b, 'z' => $current['z']-2*$b);
- }else{
- $pass = false;
- $total = $i;
- }
- }
- foreach($points as $key => $value){
- $total++;
- $points[$total]['x'] = -$value['x'];
- $points[$total]['y'] = -$value['y'];
- $points[$total]['z'] = -$value['z'];
- $total++;
- $points[$total]['x'] = -$value['x'];
- $points[$total]['y'] = $value['y'];
- $points[$total]['z'] = -$value['z'];
- $total++;
- $points[$total]['x'] = -$value['x'];
- $points[$total]['y'] = -$value['y'];
- $points[$total]['z'] = $value['z'];
- $total++;
- $points[$total]['x'] = $value['x'];
- $points[$total]['y'] = -$value['y'];
- $points[$total]['z'] = -$value['z'];
- $total++;
- $points[$total]['x'] = -$value['x'];
- $points[$total]['y'] = $value['y'];
- $points[$total]['z'] = $value['z'];
- $total++;
- $points[$total]['x'] = $value['x'];
- $points[$total]['y'] = -$value['y'];
- $points[$total]['z'] = $value['z'];
- $total++;
- $points[$total]['x'] = $value['x'];
- $points[$total]['y'] = $value['y'];
- $points[$total]['z'] = -$value['z'];
- }
- echo "$total </br>";
- return $points;
- }
- function minAngle($line, $p, $threshold, $focus, $depth){
- //---Find the side lengths of the triangle created by the point and the line
- $b = pow($line['x'][1]-$line['x'][2], 2)+pow($line['y'][1]-$line['y'][2], 2)+pow($line['z'][1]-$line['z'][2], 2); //Difference between the two points of the line
- $c = pow($line['x'][1]-$p['x'], 2)+pow($line['y'][1]-$p['y'], 2)+pow($line['z'][1]-$p['z'], 2); //Difference between the start of the line and the point
- $a = pow($p['x']-$line['x'][2], 2)+pow($p['y']-$line['y'][2], 2)+pow($p['z']-$line['z'][2], 2); //difference between the end of the line and the point
- //---Find the angle formed between the ray and the point
- $angle = rad2deg(acos(($b+$c-$a)/(2*sqrt($b)*sqrt($c)))); //cos^(-1)((b^2 + c^2 - a^2) / 2bc), will give the angle between the point an the ray.
- //---Find out how out of focus the point is
- $dif = intval(abs($p['y']-$focus)/$depth);
- if($dif == 0){
- $dif = 1;
- }
- if($angle < $threshold*$dif){
- //---Depending on the angle formed and the level of focus, return the brightness of the ray/pixel.
- $return = intval((($threshold*$dif-$angle)/($threshold*$dif))*255);
- return $return;
- }else{
- return 0;
- }
- }
- function movePoint($current_point, $other_points, $mass, $time_interval){
- //Given the above information, this function will return the resulting point after it has been affected by the gravity of the other points.
- /* Basically the method is:
- -Find the distance between the given point, and another of the rest of the points
- -Find the net force experienced by the point
- -Separate net force into the X, Y and Z planes.
- -Repeat steps 1-3, summing up the separated forces.
- -Then, a = G*((m1*m2)/(r^2))/m1
- -I guess that the period is going to affect the velocity, and then displacement... how does it go again. :S
- -s = ut+1/2at^2
- -...I'm going to need more information from the $current_point, I believe. Going to need velocity aswell.
- -shit...
- */
- $net['x'] = 0;
- $net['y'] = 0;
- $net['z'] = 0;
- $dif['x'] = 0;
- $dif['y'] = 0;
- $dif['z'] = 0;
- foreach($other_points as $key => $other){
- //I suck at creating variable names
- //And I didn't really think this through
- if($current_point['x'] != $other['x'] && $current_point['y'] != $other['y'] && $current_point['z'] != $other['x']){
- $dif['x'] = $other['x']-$current_point['x'];
- $dif['y'] = $other['y']-$current_point['y'];
- $dif['z'] = $other['z']-$current_point['z'];
- $distance = sqrt(pow($dif['x'], 2)+pow($dif['y'], 2)+pow($dif['z'], 2)); //Pretty much the radius
- $acceleration = ((6.67300*pow(10, -11)*pow($mass, 2))/pow($distance, 2))/$mass; //I guess that's right
- echo "acc = $acceleration </br> \n";
- //now to divide this into the 3 planes, x, y and z
- if($dif['x'] < 0){
- $multiplier['x'] = -1;
- }elseif($dif['x'] == 0){
- $multiplier['x'] = 0;
- }else{
- $multiplier['x'] = 1;
- }
- if($dif['y'] < 0){
- $multiplier['y'] = -1;
- }elseif($dif['y'] == 0){
- $multiplier['y'] = 0;
- }else{
- $multiplier['y'] = 1;
- }
- if($dif['z'] < 0){
- $multiplier['z'] = -1;
- }elseif($dif['x'] == 0){
- $multiplier['z'] = 0;
- }else{
- $multiplier['z'] = 1;
- }
- $output_acceleration['x'] += $multiplier['x']*abs($acceleration*cos(acos($dif['x']/$distance)));
- $output_acceleration['y'] += $multiplier['y']*abs($acceleration*cos(acos($dif['y']/$distance)));
- $output_acceleration['z'] += $multiplier['z']*abs($acceleration*cos(acos($dif['z']/$distance)));
- echo "acc (" . $output_acceleration['x'] . ", " . $output_acceleration['y'] . ", " . $output_acceleration['z'] . ") </br> \n";
- //That was more simple that I thought.
- }else{
- $current_key = $key;
- }
- $displacement['x'] = $current_point['xv']*$time_interval+0.5*$output_acceleration['x']*pow($time_interval, 2); // s = ut+(1/2)at^2
- $displacement['y'] = $current_point['yv']*$time_interval+0.5*$output_acceleration['y']*pow($time_interval, 2); // s = ut+(1/2)at^2
- $displacement['z'] = $current_point['zv']*$time_interval+0.5*$output_acceleration['z']*pow($time_interval, 2); // s = ut+(1/2)at^2
- global $points;
- $points[$current_key]['xv'] = $displacement['x']/$time_interval;
- $points[$current_key]['yv'] = $displacement['y']/$time_interval;
- $points[$current_key]['zv'] = $displacement['z']/$time_interval;
- }
- $current_point['x'] += $displacement['x'];
- $current_point['y'] += $displacement['y'];
- $current_point['z'] += $displacement['z'];
- //Holy shit, I think that's done. This isn't written the most efficiently, but it will do.
- return $current_point;
- }
- //---Variables
- $screen_dimensions = array('height' => 100, 'width' => 100); //Do not make this very large as too much memory/time will be used and the program will fail.
- $screen_position = array('x' => 0, 'y' => -20, 'z' => 0); //I spose this could be changed, but I haven't bothered with it.
- $amount_of_frames = 2; //1 second = 1 frame
- $screen_viewingAngle = 65; //In degrees
- //$focus = (0.5*$screen_dimensions['width'])/tan(deg2rad(0.5*$screen_viewingAngle))+70; //The Y frame which will be in focus.
- $focus = 798;
- $depth_of_field = 150; //This is kinda like the F-stop on a lens.
- $angular_threshold = 1; //This is kinda different, but it's basically the accuracy of the imaging. 0 would be perfect, but would not work.
- /* Do not use these yet.
- $screen_yaw = 0;
- $screen_pitch = 0;
- $screen_roll = 0;
- */
- /* BEGIN MAIN PROGRAM */
- //---Pre-processing variables (inititators of a sort)
- $offset['y'] = (0.5*$screen_dimensions['width'])/tan(deg2rad(0.5*$screen_viewingAngle))+$screen_position['y'];
- //$offset['x'] = 0.5*$screen_dimensions['width']+$screen_position['x']; NOT USED
- $offset['z'] = 0.5*$screen_dimensions['height']-$screen_position['z'];
- $funny_variable[1] = 0.5*$screen_dimensions['width'];
- $funny_variable[2] = 0.5*$screen_dimensions['width']+$screen_position['x'];
- //$points = cubeLatticeArray(800, 77);
- $points[1] = array('x' => 0, 'y' => 0, 'z' => 0);
- $points[2] = array('x' => 50, 'y' => 10, 'z' => 50);
- foreach($points as $key => $value){
- //Make the velocity on each of the points zero (initiate variables)
- $points[$key]['xv'] = 0;
- $points[$key]['yv'] = 0;
- $points[$key]['zv'] = 0;
- }
- //---Reset '$current' variables
- for($h = 0; $h < $amount_of_frames; $h++){
- $current['z'] = $offset['z'];
- $current['x'] = $screen_position['x']-$funny_variable[1];
- //---Begin writing file
- unlink("image$h.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$h.ppm", 'a+'); //creates the image file that we will be editting and opens the file for appending
- $print_height = 1+$screen_dimensions['height'];
- $print_width = 1+$screen_dimensions['width'];
- $startWrite = "P3\n# image$h.ppm\n$print_width $print_height\n255"; //fomatting the ppm file
- fwrite($fh, $startWrite);
- //---Begin building rays and printing the file
- //These two steps are done at the same time for efficiency, and memory
- for($i = 0; $i < ($print_height*$print_width); $i++){
- //---Origin points
- $lines['x'][1] = $screen_position['x'];
- $lines['y'][1] = $screen_position['y'];
- $lines['z'][1] = $screen_position['z'];
- //---Resolve when to move to the next row of pixels
- if($i > 0){
- $current['x']++;
- }
- if($current['x'] > $funny_variable[2]){
- $current['x'] = -$funny_variable[1]+$screen_position['x'];
- $current['z']--;
- }
- //---To points
- $lines['x'][2] = $current['x'];
- $lines['y'][2] = $offset['y'];
- $lines['z'][2] = $current['z'];
- if($i%5 == 0){ //basic formatting decides when a new line must be placed
- $Nl = " \n";
- }else{
- $Nl = " ";
- }
- $out = 000;
- foreach($points as $value){
- $colour = minAngle($lines, $value, $angular_threshold, $focus, $depth_of_field);
- if($colour == 255){
- $out = $colour;
- break 1;
- //}elseif($colour >= 0 && $colour >= $out){
- }elseif($colour >= 0){
- $out = str_pad(intval($colour+$out), 3, "0", STR_PAD_LEFT);
- if($out >= 255){
- $out = 255;
- break 1;
- }
- }
- }
- printf("(%+'04.2d, %+'04.2d, %+'04.2d) = %'03d </br>", $lines['x'][2], $lines['y'][2], $lines['z'][2], $out);
- //echo "(".$lines['x'][2].", ".$lines['y'][2].", ".$lines['z'][2].")= $out </br>\n";
- $write = "$Nl$out $out $out"; //the line that is to be outputted
- fwrite($fh, $write); //write the pixel to the file.
- }
- fclose($fh);
- foreach($points as $key => $value){
- $points[$key] = movePoint($value, $points, 1000, 1);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment