Search:

Submit Article

# Program to draw a Sphere using Ellipses

Posted By: Easy Tutor     Category: C++ Programming     Views: 4632

## Code for Program to draw a Sphere using Ellipses in C++ Programming

# 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(int&,int&,int&);
void multiply_matrices(constfloat[4],constfloat[4][4],float[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)
{
int x=_x;
int y=_y;
int z=_z;

get_projected_point(x,y,z);

for(int angle=2;angle<=r;angle+=5)
{
ellipse(x,y,0,360,r,angle);
ellipse(x,y,0,360,angle,r);
}
}

/************************************************************************///---------------------  get_projected_point( )  -----------------------///************************************************************************/void get_projected_point(int& x,int& y,int& z)
{
float fcos0=(f*cos(projection_angle*(M_PI/180)));
float fsin0=(f*sin(projection_angle*(M_PI/180)));

float Par_v[4][4]={
{1,0,0,0},
{0,1,0,0},
{fcos0,fsin0,0,0},
{0,0,0,1}
};

float xy[4]={x,y,z,1};
float 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(constfloat matrix_1[4],
constfloat matrix_2[4][4],float 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);
}

Share:

Didn't find what you were looking for? Find more on Program to draw a Sphere using Ellipses Or get search suggestion and latest updates.

 Easy Tutor author of Program to draw a Sphere using Ellipses is from United States. Easy Tutor says Hello Friends,I am Free Lance Tutor, who helped student in completing their homework. I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] comI have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada. I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.Thanks,Happy Programming :) View All Articles

 Please enter your CommentComment should be atleast 30 Characters.Please put code inside [Code] your code [/Code]. No Comment Found, Be the First to post comment!