Death Star: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 29: Line 29:
# We now trigger the raytracer to see our finished product
# We now trigger the raytracer to see our finished product
rt</lang>
rt</lang>
=={{header|C}}==
<lang C>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
char shades[] = ".:!*oe&#%@";
double light[3] = { -50, 30, 50 };
void normalize(double * v)
{
double len = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
v[0] /= len; v[1] /= len; v[2] /= len;
}
double dot(double *x, double *y)
{
double d = x[0]*y[0] + x[1]*y[1] + x[2]*y[2];
return d < 0 ? -d : 0;
}

typedef struct { double cx, cy, cz, r; } sphere_t;
sphere_t big = { 20, 20, 0, 20 }, small = { 7, 7, -10, 15 };

int hit_sphere(sphere_t *sph, double x, double y, double *z1, double *z2)
{
double zsq;
x -= sph->cx;
y -= sph->cy;
zsq = sph->r * sph->r - (x * x + y * y);
if (zsq < 0) return 0;
zsq = sqrt(zsq);
*z1 = sph->cz - zsq;
*z2 = sph->cz + zsq;
return 1;
}

void draw_sphere(double k, double ambient)
{
int i, j, intensity;
double b;
double vec[3], x, y, zb1, zb2, zs1, zs2;
for (i = floor(big.cy - big.r); i <= ceil(big.cy + big.r); i++) {
y = i + .5;
for (j = floor(big.cx - 2 * big.r); j <= ceil(big.cx + 2 * big.r); j++) {
x = (j - big.cx) / 2. + .5 + big.cx;

if (!hit_sphere(&big, x, y, &zb1, &zb2)) {
putchar(' ');
continue;
}
if (!hit_sphere(&small, x, y, &zs1, &zs2)) {
vec[0] = x - big.cx;
vec[1] = y - big.cy;
vec[2] = zb1 - big.cz;
} else {
if (zs1 < zb1) {
if (zs2 > zb2) {
putchar(' ');
continue;
}
if (zs2 > zb1) {
vec[0] = small.cx - x;
vec[1] = small.cy - y;
vec[2] = small.cz - zs2;
} else {
vec[0] = x - big.cx;
vec[1] = y - big.cy;
vec[2] = zb1 - big.cz;
}
} else {
vec[0] = x - big.cx;
vec[1] = y - big.cy;
vec[2] = zb1 - big.cz;
}
}

normalize(vec);
b = pow(dot(light, vec), k) + ambient;
intensity = (1 - b) * (sizeof(shades) - 1);
if (intensity < 0) intensity = 0;
if (intensity >= sizeof(shades) - 1)
intensity = sizeof(shades) - 2;
putchar(shades[intensity]);
}
putchar('\n');
}
}

int main()
{
normalize(light);
draw_sphere(2, .3);
return 0;
}</lang>Output:<lang> #######ooo*******
&&&&&###########!!!!!!!!:::!!!
&&&&&&&&&###########!:::::::......:::
eee&&&&&&&&&&###########:::...............:
oooeeeee&&&&&&&&&##########:....................:
**ooooeeeee&&&&&&&&&########.........................
!!***ooooeeeee&&&&&&&&########...........................
:!!!!***ooooeeeee&&&&&&&&######..............................
.::::!!!***ooooeeee&&&&&&&&######...............................
...:::!!!!***ooooeeee&&&&&&&&####..................................
......:::!!!****oooeeee&&&&&&&&##:::..................................
........:::!!!***ooooeeee&&&&&&&#::::...................................
.........:::!!!****oooeeee&&&&&&&!::::....................................
...........:::!!!***oooeeee&&&&&!!!:::::...................................
............:::!!!***oooeeee&&&**!!!!::::....................................
............:::!!!***oooeeee&&***!!!!!::::...................................
............:::!!!***ooooeeeooo****!!!!:::::...................................
............:::!!!***oooeeooooo*****!!!!:::::..................................
...........:::!!!***oooeeeeooooo*****!!!!:::::.................................
#........:::!!!!***o&&eeeeeeooooo*****!!!!:::::................................
##::::::::!!!!***&&&&&&eeeeeeooooo*****!!!!!:::::.............................:
######****##&&&&&&&&&&&&eeeeeeooooo*****!!!!!::::::...........................:
#############&&&&&&&&&&&&eeeeeeoooooo*****!!!!!::::::........................:!
##############&&&&&&&&&&&&eeeeeeoooooo*****!!!!!!::::::.....................::!
##############&&&&&&&&&&&&eeeeeeeoooooo*****!!!!!!:::::::................:::!
###############&&&&&&&&&&&&&eeeeeeoooooo******!!!!!!!::::::::::.....::::::!!*
###############&&&&&&&&&&&&&eeeeeeeoooooo*******!!!!!!!::::::::::::::::!!!*
################&&&&&&&&&&&&&&eeeeeeeooooooo*******!!!!!!!!!!!!!!!!!!!!!**o
#################&&&&&&&&&&&&&eeeeeeeeooooooo*********!!!!!!!!!!!!!!****o
#################&&&&&&&&&&&&&&eeeeeeeeoooooooo**********************oe
##################&&&&&&&&&&&&&&&eeeeeeeeoooooooooo*************ooooe
#################&&&&&&&&&&&&&&&&eeeeeeeeeeoooooooooooooooooooooe
##################&&&&&&&&&&&&&&&&&eeeeeeeeeeeeooooooooooooeeee
##################&&&&&&&&&&&&&&&&&&eeeeeeeeeeeeeeeeeeeeee&
###################&&&&&&&&&&&&&&&&&&&&&eeeeeeeeeeee&&&
####################&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
####################&&&&&&&&&&&&&&&&&&&&&&&&&
#####################&&&&&&&&&&&&&&&&&&
###############################
#################
</lang>


=={{header|Openscad}}==
=={{header|Openscad}}==

Revision as of 05:58, 24 June 2011

Task
Death Star
You are encouraged to solve this task according to the task description, using any language you may know.

Death Star is a task to display a region that consists of a large sphere with part of a smaller sphere removed from it as a result of geometric subtraction. (This will basically produce a shape like a "death star".)

Brlcad

<lang brlcad># We need a database to hold the objects opendb deathstar.g y

  1. We will be measuring in kilometers

units km

  1. Create a sphere of radius 60km centred at the origin

in sph1.s sph 0 0 0 60

  1. We will be subtracting an overlapping sphere with a radius of 40km
  2. The resultant hole will be smaller than this, because we only
  3. only catch the edge

in sph2.s sph 0 90 0 40

  1. Create a region named deathstar.r which consists of big minus small sphere

r deathstar.r u sph1.s - sph2.s

  1. We will use a plastic material texture with rgb colour 224,224,224
  2. with specular lighting value of 0.1 and no inheritance

mater deathstar.r "plastic sp=0.1" 224 224 224 0

  1. Clear the wireframe display and draw the deathstar

B deathstar.r

  1. We now trigger the raytracer to see our finished product

rt</lang>

C

<lang C>

  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <math.h>

char shades[] = ".:!*oe&#%@";

double light[3] = { -50, 30, 50 }; void normalize(double * v) {

       double len = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
       v[0] /= len; v[1] /= len; v[2] /= len;

}

double dot(double *x, double *y) {

       double d = x[0]*y[0] + x[1]*y[1] + x[2]*y[2];
       return d < 0 ? -d : 0;

}

typedef struct { double cx, cy, cz, r; } sphere_t; sphere_t big = { 20, 20, 0, 20 }, small = { 7, 7, -10, 15 };

int hit_sphere(sphere_t *sph, double x, double y, double *z1, double *z2) { double zsq; x -= sph->cx; y -= sph->cy; zsq = sph->r * sph->r - (x * x + y * y); if (zsq < 0) return 0; zsq = sqrt(zsq); *z1 = sph->cz - zsq; *z2 = sph->cz + zsq; return 1; }

void draw_sphere(double k, double ambient) {

       int i, j, intensity;
       double b;
       double vec[3], x, y, zb1, zb2, zs1, zs2;
       for (i = floor(big.cy - big.r); i <= ceil(big.cy + big.r); i++) {
               y = i + .5;
               for (j = floor(big.cx - 2 * big.r); j <= ceil(big.cx + 2 * big.r); j++) {
                       x = (j - big.cx) / 2. + .5 + big.cx;

if (!hit_sphere(&big, x, y, &zb1, &zb2)) { putchar(' '); continue; } if (!hit_sphere(&small, x, y, &zs1, &zs2)) { vec[0] = x - big.cx; vec[1] = y - big.cy; vec[2] = zb1 - big.cz; } else { if (zs1 < zb1) { if (zs2 > zb2) { putchar(' '); continue; } if (zs2 > zb1) { vec[0] = small.cx - x; vec[1] = small.cy - y; vec[2] = small.cz - zs2; } else { vec[0] = x - big.cx; vec[1] = y - big.cy; vec[2] = zb1 - big.cz; } } else { vec[0] = x - big.cx; vec[1] = y - big.cy; vec[2] = zb1 - big.cz; } }

                       normalize(vec);
                       b = pow(dot(light, vec), k) + ambient;
                       intensity = (1 - b) * (sizeof(shades) - 1);
                       if (intensity < 0) intensity = 0;
                       if (intensity >= sizeof(shades) - 1)
                               intensity = sizeof(shades) - 2;
                       putchar(shades[intensity]);
               }
               putchar('\n');
       }

}

int main() {

       normalize(light);

	draw_sphere(2, .3);
       return 0;

}</lang>Output:<lang> #######ooo*******

                        &&&&&###########!!!!!!!!:::!!!                          
                     &&&&&&&&&###########!:::::::......:::                      
                  eee&&&&&&&&&&###########:::...............:                   
               oooeeeee&&&&&&&&&##########:....................:                
             **ooooeeeee&&&&&&&&&########.........................              
           !!***ooooeeeee&&&&&&&&########...........................            
         :!!!!***ooooeeeee&&&&&&&&######..............................          
       .::::!!!***ooooeeee&&&&&&&&######...............................         
      ...:::!!!!***ooooeeee&&&&&&&&####..................................       
    ......:::!!!****oooeeee&&&&&&&&##:::..................................      
   ........:::!!!***ooooeeee&&&&&&&#::::...................................     
  .........:::!!!****oooeeee&&&&&&&!::::....................................    
 ...........:::!!!***oooeeee&&&&&!!!:::::...................................    
............:::!!!***oooeeee&&&**!!!!::::....................................   
............:::!!!***oooeeee&&***!!!!!::::...................................   

............:::!!!***ooooeeeooo****!!!!:::::................................... ............:::!!!***oooeeooooo*****!!!!:::::.................................. ...........:::!!!***oooeeeeooooo*****!!!!:::::.................................

  1. ........:::!!!!***o&&eeeeeeooooo*****!!!!:::::................................
    1. !!!!***&&&&&&eeeeeeooooo*****!!!!!:::::.............................:
                        1. &&&&&&&&&&&&eeeeeeooooo*****!!!!!::::::...........................:
                          1. &&&&&&&&&&&&eeeeeeoooooo*****!!!!!::::::........................:!
                            1. &&&&&&&&&&&&eeeeeeoooooo*****!!!!!!::::::.....................::!
##############&&&&&&&&&&&&eeeeeeeoooooo*****!!!!!!:::::::................:::!   
###############&&&&&&&&&&&&&eeeeeeoooooo******!!!!!!!::::::::::.....::::::!!*   
 ###############&&&&&&&&&&&&&eeeeeeeoooooo*******!!!!!!!::::::::::::::::!!!*    
 ################&&&&&&&&&&&&&&eeeeeeeooooooo*******!!!!!!!!!!!!!!!!!!!!!**o    
  #################&&&&&&&&&&&&&eeeeeeeeooooooo*********!!!!!!!!!!!!!!****o     
   #################&&&&&&&&&&&&&&eeeeeeeeoooooooo**********************oe      
    ##################&&&&&&&&&&&&&&&eeeeeeeeoooooooooo*************ooooe       
      #################&&&&&&&&&&&&&&&&eeeeeeeeeeoooooooooooooooooooooe         
       ##################&&&&&&&&&&&&&&&&&eeeeeeeeeeeeooooooooooooeeee          
         ##################&&&&&&&&&&&&&&&&&&eeeeeeeeeeeeeeeeeeeeee&            
           ###################&&&&&&&&&&&&&&&&&&&&&eeeeeeeeeeee&&&              
             ####################&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&                
                ####################&&&&&&&&&&&&&&&&&&&&&&&&&                   
                   #####################&&&&&&&&&&&&&&&&&&                      
                       ###############################                          
                              #################                                 

</lang>

Openscad

<lang openscad>// We are performing geometric subtraction

difference() {

 // Create the primary sphere of radius 60 centred at the origin
 translate(v = [0,0,0]) {
   sphere(60);
 }
 /*Subtract an overlapping sphere with a radius of 40
    The resultant hole will be smaller than this, because we only
    only catch the edge
 */
 translate(v = [0,90,0]) {
   sphere(40);
 }

}</lang>

POV-Ray

<lang POV-Ray>camera { perspective location <0.0 , .8 ,-3.0> look_at 0

        aperture .1 blur_samples 20 variance 1/100000 focal_point 0}
                           

light_source{< 3,3,-3> color rgb 1}

sky_sphere { pigment{ color rgb <0,.2,.5>}}

plane {y,-5 pigment {color rgb .54} normal {hexagon} }

difference {

sphere { 0,1 }
sphere { <-1,1,-1>,1 }
 texture { 
   pigment{ granite } 
   finish { phong 1 reflection {0.10 metallic 0.5} }
 } 

} </lang>