Draw a sphere: Difference between revisions
Content added Content deleted
(→{{header|C}}: In 3D! mhahaha) |
|||
Line 8: | Line 8: | ||
units cm # Set the unit of measure |
units cm # Set the unit of measure |
||
in ball.s sph 0 0 0 3 # Create a sphere of radius 3 cm named ball.s with its centre at 0,0,0 </lang> |
in ball.s sph 0 0 0 3 # Create a sphere of radius 3 cm named ball.s with its centre at 0,0,0 </lang> |
||
=={{header|C}}== |
|||
<lang C>#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <ctype.h> |
|||
#include <math.h> |
|||
char shades[] = ".:!*oe&#%@"; |
|||
double light[3] = { 30, 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; |
|||
} |
|||
void draw_sphere(double R, double k, double ambient) |
|||
{ |
|||
int i, j, intensity; |
|||
double b; |
|||
double vec[3], x, y; |
|||
for (i = floor(-R); i <= ceil(R); i++) { |
|||
x = i + .5; |
|||
for (j = floor(-2 * R); j <= ceil(2 * R); j++) { |
|||
y = j / 2 + .5; |
|||
if (x * x + y * y <= R * R) { |
|||
vec[0] = x; |
|||
vec[1] = y; |
|||
vec[2] = sqrt(R * R - x * x - y * y); |
|||
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]); |
|||
} else |
|||
putchar(' '); |
|||
} |
|||
putchar('\n'); |
|||
} |
|||
} |
|||
int main() |
|||
{ |
|||
normalize(light); |
|||
draw_sphere(20, 4, .1); |
|||
draw_sphere(10, 2, .4); |
|||
return 0; |
|||
}</lang>Output:<lang> #############%%%% |
|||
##&&eeeeeeeeee&&&&&&&####%%%%%%%% |
|||
&&eeooooooooooooooeeeee&&&&######%%%%%%%% |
|||
eeoo**************oooooooeeee&&&&####%%%%%%%% |
|||
&&oo**!!!!!!::!!!!!!!!****oooooee&&&&######%%%%%%%%%% |
|||
eeoo!!!!::::::::::::::!!!!*****ooeeee&&&&####%%%%%%%%%%%% |
|||
ee**!!::::............::::!!!!***ooooeeee&&######%%%%%%%%%%%% |
|||
&&oo!!::..................::!!!!*****ooeeee&&&&####%%%%%%%%%%%%%% |
|||
oo!!::....................::::!!*****ooeeee&&&&####%%%%%%%%%%%%%% |
|||
ee**!!::....................::::!!*****ooeeee&&&&####%%%%%%%%%%%%%%%% |
|||
&&oo!!::......................::::!!*****ooeeee&&&&######%%%%%%%%%%%%%%%% |
|||
ee**!!::......................::::!!*****ooeeee&&&&######%%%%%%%%%%%%%%%% |
|||
##oo**!!::......................::!!!!*****ooeeee&&&&####%%%%%%%%%%%%%%%%%%%% |
|||
&&oo**::::....................::::!!!!***ooooeeee&&&&####%%%%%%%%%%%%%%%%%%%% |
|||
eeoo**!!::..................::::!!!!*****ooooee&&&&######%%%%%%%%%%%%%%%%%%%% |
|||
eeoo**!!::................::::!!!!****oooooeeee&&&&######%%%%%%%%%%%%%%%%%%%% |
|||
#eeoo**!!::::............::::!!!!!!****oooeeee&&&&&&######%%%%%%%%%%%%%%%%%%%%%% |
|||
#eeoo**!!!!::::::::::::::::!!!!!!****oooooeeee&&&&######%%%%%%%%%%%%%%%%%%%%%%%% |
|||
#eeoooo**!!!!!!::::::::!!!!!!******ooooeeeee&&&&&&######%%%%%%%%%%%%%%%%%%%%%%%% |
|||
#&&eeoo****!!!!!!!!!!!!!!!!******ooooeeeee&&&&&&######%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
#&&eeoooo********************ooooooeeee&&&&&&&######%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
#&&&&eeoooooo************ooooooeeeeee&&&&&&&########%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%##&&eeeeeeooooooooooooooooooeeeeee&&&&&&&########%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%####&&&&eeeeeeeeeeeeeeeeeeeeee&&&&&&&&#########%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%##&&&&&&eeeeeeeeeeeeeeee&&&&&&&&&&#########%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%######&&&&&&&&&&&&&&&&&&&&&&&&###########%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%########&&&&&&&&&&&&&&############%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%%%##############################%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%%%%%######################%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%%%%%%%%%%%%%####%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|||
%%%%%%%%%%%%%%%%% |
|||
::..:::::!!** |
|||
.............::!!**oo |
|||
...................::!!**ooee |
|||
.......................::!!ooeeee |
|||
.......................::!!**ooee |
|||
.........................::!!**ooeeee |
|||
.........................::!!**ooeeee |
|||
:.........................::!!**ooeeeeee |
|||
........................::!!**ooooeeeeee |
|||
:.......................::!!**ooeeeeeeee |
|||
:.....................::!!****ooeeeeeeee |
|||
!::................:::!!****ooeeeeeeeeee |
|||
*!!::..........::::!!!****ooooeeeeeeeeee |
|||
**!!::::::::::!!!!*****ooooeeeeeeeeee |
|||
oo**!!!!!!!!!!*******ooooeeeeeeeeeeee |
|||
oooo********oooooooeeeeeeeeeeeeee |
|||
eeeeooooooooooeeeeeeeeeeeeeeeeeee |
|||
eeeeeeeeeeeeeeeeeeeeeeeeeeeee |
|||
eeeeeeeeeeeeeeeeeeeee |
|||
eeeeeeeeeeeee </lang> |
|||
==Icon and {{header|Unicon}}== |
==Icon and {{header|Unicon}}== |