Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function minAngle($line, $p){
- //$l1 and $l2 are the start and end points of a line segment, formatted as (x, y, z). $p is the points that is being tested.
- /*I'm thinking screw it. Instead of finding the minimum distance between the ray and the point. I think that I should focus on whether or not the angle between the ray and the point is satisfactory. For starters, I'll make the angular limit 1 degree.*/
- $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
- $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.
- if($angle < 2){
- return true;
- }else{
- return false;
- }
- }
- $screen_dimensions = array('height' => 700, 'width' => 700);
- $screen_position = array('x' => 0, 'y' => 0, 'z' => 0);
- $screen_viewingAngle = 150;
- /* Do not use these yet.
- $screen_yaw = 0;
- $screen_pitch = 0;
- $screen_roll = 0;
- */
- /* BEGIN MAIN PROGRAM */
- //Build screen rays
- $offset['y'] = (0.5*$screen_dimensions['width'])/tan(deg2rad(0.5*$screen_viewingAngle));
- $offset['x'] = 0.5*$screen_dimensions['width'];
- $offset['z'] = 0.5*$screen_dimensions['height'];
- $current['z'] = 0.5*$screen_dimensions['height'];
- $current['x'] = -0.5*$screen_dimensions['width'];
- for($i = 0; $i < 36; $i++){
- if($i > 0){
- $current['x'] += $screen_dimensions['width']/5;
- }
- if($current['x'] > $offset['x']){
- $current['x'] = -$offset['x'];
- $current['z'] -= $screen_dimensions['height']/5;
- }
- print "Z = ".$current['z'] ." X = ".$current['x'] ."</br>\n";
- $points[$i]['x'] = $current['x'];
- $points[$i]['y'] = $offset['y']+10;
- $points[$i]['z'] = $current['z'];
- }
- $current['z'] = 0.5*$screen_dimensions['height'];
- $current['x'] = -0.5*$screen_dimensions['width'];
- 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
- $print_height = 1+$screen_dimensions['height'];
- $print_width = 1+$screen_dimensions['width'];
- $startWrite = "P3\n# image.ppm\n$print_width $print_height\n255"; //fomatting the ppm file
- fwrite($fh, $startWrite);
- 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'];
- // \Origin points
- //To points
- if($i > 0){
- $current['x']++;
- }
- if($current['x'] > $offset['x']){
- $current['x'] = -$offset['x'];
- $current['z']--;
- }
- $lines['x'][2] = $current['x'];
- $lines['y'][2] = $offset['y'];
- $lines['z'][2] = $current['z'];
- echo $lines['x'][2].", ".$lines['z'][2].", ".$lines['y'][2]."</br> \n";
- if($i%5 == 0){ //basic formatting decides when a new line must be placed
- $Nl = " \n";
- }else{
- $Nl = " ";
- }
- foreach($points as $value){
- if(minAngle($lines, $value)){
- $colour = "255";
- echo "white </br>\n";
- break 1;
- }else{
- $colour = "000";
- }
- }
- $write = "$Nl$colour $colour $colour"; //the line that is to be outputted
- fwrite($fh, $write); //write the pixel to the file.
- }
- fclose($fh);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment