# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
# define f 0.3
# define projection_angle 45
void show_screen( );
void Sphere(constint,constint,constint,constint);
void get_projected_point(double&,double&,double&);
void multiply_matrices(constdouble[4],constdouble[4][4],double[4]);
int main( )
{
int driver=VGA;
int mode=VGAHI;
int x=0;
int y=0;
int z=0;
int r=0;
do
{
show_screen( );
gotoxy(8,10);
cout<<"Coordinates of Central Point - (x,y,z) :";
gotoxy(8,11);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(12,13);
cout<<"Enter the value of x = ";
cin>>x;
gotoxy(12,15);
cout<<"Enter the value of y = ";
cin>>y;
gotoxy(12,17);
cout<<"Enter the value of z = ";
cin>>z;
gotoxy(8,21);
cout<<"Radius of the Sphere - r :";
gotoxy(8,22);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(12,24);
cout<<"Enter the value of r = ";
cin>>r;
initgraph(&driver,&mode,"..\\Bgi");
setcolor(15);
Sphere(x,y,z,r);
setcolor(15);
outtextxy(110,460,"Press <Enter> to continue or any other key to exit.");
int key=int(getch( ));
if(key!=13)
break;
}
while(1);
return 0;
}
/*************************************************************************///----------------------------- Sphere( ) -----------------------------///*************************************************************************/void Sphere(constint _x,constint _y,constint _z,constint r)
{
double x;
double y;
double z;
for(float u=0;u<=1;u+=0.0005)
{
for(float v=0;v<=1;v+=0.05)
{
x=((r*cosl((u*M_PI))*cosl((v*2*M_PI)))+_x);
y=((r*cosl((u*M_PI))*sinl((v*2*M_PI)))+_y);
z=((r*sinl((u*M_PI)))+_z);
get_projected_point(x,y,z);
putpixel(int(x+0.5),int(y+0.5),15);
}
}
for(float v=0;v<=1;v+=0.0005)
{
for(float u=0;u<=1;u+=0.05)
{
x=((r*cosl((u*M_PI))*cosl((v*2*M_PI)))+_x);
y=((r*cosl((u*M_PI))*sinl((v*2*M_PI)))+_y);
z=((r*sinl((u*M_PI)))+_z);
get_projected_point(x,y,z);
putpixel(int(x+0.5),int(y+0.5),15);
}
}
}
/************************************************************************///--------------------- get_projected_point( ) -----------------------///************************************************************************/void get_projected_point(double& x,double& y,double& z)
{
double fcos0=(f*cos(projection_angle*(M_PI/180)));
double fsin0=(f*sin(projection_angle*(M_PI/180)));
double Par_v[4][4]={
{1,0,0,0},
{0,1,0,0},
{fcos0,fsin0,0,0},
{0,0,0,1}
};
double xy[4]={x,y,z,1};
double new_xy[4]={0};
multiply_matrices(xy,Par_v,new_xy);
x=new_xy[0];
y=new_xy[1];
z=new_xy[2];
}
/************************************************************************///---------------------- multiply_matrices( ) ------------------------///************************************************************************/void multiply_matrices(constdouble matrix_1[4],
constdouble matrix_2[4][4],double matrix_3[4])
{
for(int count_1=0;count_1<4;count_1++)
{
for(int count_2=0;count_2<4;count_2++)
matrix_3[count_1]+=
(matrix_1[count_2]*matrix_2[count_2][count_1]);
}
}
/*************************************************************************///-------------------------- show_screen( ) ---------------------------///*************************************************************************/void show_screen( )
{
restorecrtmode( );
clrscr( );
textmode(C4350);
cprintf("\n********************************************************************************");
cprintf("*-********************************- -********************************-*");
cprintf("*---------------------------------- ");
textbackground(1);
cprintf(" Sphere ");
textbackground(8);
cprintf(" ----------------------------------*");
cprintf("*-********************************- -********************************-*");
cprintf("*-****************************************************************************-*");
for(int count=0;count<42;count++)
cprintf("*-* *-*");
gotoxy(1,46);
cprintf("*-****************************************************************************-*");
cprintf("*------------------------------------------------------------------------------*");
cprintf("********************************************************************************");
gotoxy(1,2);
}