# Is it sphere

• 11-23-2003
planet_abhi
Is it sphere
I make the following code for sphere in borland c++ ver 3.0
is it right sphere?

Code:

``` # include<graphics.h> # include<conio.h> int main() { int i,cx=220,cy=300; int gd=0,gm=0; initgraph(gd,&gm,""); cleardevice(); circle(cx,cy,80); for(i=10;i<=80;i++) { ellipse(cx,cy,90,270,i,80); ellipse(cx,cy,270,90,i,80); ellipse(cx,cy,90,270,80,i); ellipse(cx,cy,270,90,80,i); } getch(); closegraph(); restorecrtmode(); }```
does it represent a 3rd dimension ?
if not then which is the right way to do that in above code.
• 11-23-2003
VirtualAce
No.

I tried the same way as you at first before I did it the mathematical way.

where:

-90<alpha<90
0<beta<360
• 11-25-2003
planet_abhi
But what's that newx,newy,newz
i can't visualize the z point
a little idea i have that

Code:

```void CreateUnitSphere(int dtheta,int dphi) {   int n;   int theta,phi;   XYZ p[4];   for (theta=-90;theta<=90-dtheta;theta+=dtheta) {       for (phi=0;phi<=360-dphi;phi+=dphi) {         n = 0;         p[n].x = cos(theta*DTOR) * cos(phi*DTOR);         p[n].y = cos(theta*DTOR) * sin(phi*DTOR);         p[n].z = sin(theta*DTOR);         n++;         p[n].x = cos((theta+dtheta)*DTOR) * cos(phi*DTOR);         p[n].y = cos((theta+dtheta)*DTOR) * sin(phi*DTOR);         p[n].z = sin((theta+dtheta)*DTOR);         n++;         p[n].x = cos((theta+dtheta)*DTOR) * cos((phi+dphi)*DTOR);         p[n].y = cos((theta+dtheta)*DTOR) * sin((phi+dphi)*DTOR);         p[n].z = sin((theta+dtheta)*DTOR);         n++;         if (theta > -90 && theta < 90) {             p[n].x = cos(theta*DTOR) * cos((phi+dphi)*DTOR);             p[n].y = cos(theta*DTOR) * sin((phi+dphi)*DTOR);             p[n].z = sin(theta*DTOR);             n++;         }         /* Do something with the n vertex facet p */       }   } } 0<alpha<360 -90<beta<90 newx=centerx+((cos(alpha)*cos(beta))*radius) newy=centery+((cos(alpha)*sin(beta))*radius) newz=centerz+((sin(alpha))*radius```

but how can i draw the actual sphere its just the base logic
• 11-25-2003
VirtualAce
No no no. I'm not sure why you are multiplying three times. You only need the one.

The new variables are the new coordinates. If you want to create a sphere around something other than 0,0,0 then you must displace the points by a set amount to create a sphere around that point. The radius is simple - it displaces the point from centerx,centery,centerz.

This is for a 3D sphere. Now in order to create an actual sphere and draw it...u must think a bit. Let's say we want 16 facets in our sphere. To achieve this in 360 degrees of arc we do 360/16 to get the amount of angle we must increment on each iteration through alpha. As well you will want to increment beta by this too to get perfect quads in your sphere. But how do we draw lines when all we have is set amount of vertexes out in memory?

Simple. We know we have 16 facets right? After 16 facets we move to the next 'level' of the sphere.

So if we are the start we do this:

Draw a line between vertex 0 and vertex 1
Draw a line between vertex 0 and vertex 16
Draw a line between vertex 1 and vertex 17
Draw a line between vertex 16 and vertex 17

0........1
. .
. .
. .
16......17

Thus you have your quad. Send these vertexes to a polygon function or to DirectX - along with a texture or portion of a texture and you can texture your sphere - either a diff texture on each quad or like a planet with a large texture across the whole thing - each quad being only a section of the larger texture.

If you have implemented this formula you know that alpha is the rotation around the sphere on the x axis (lattitude) and beta is the rotation on the y axis (longitude). If you only go from -90 to 0 you will have half a sphere - like a dome. If you only go from 0 to 180 you will have half a sphere (like a dome turned on its side - half of an egg or something)
• 11-26-2003
planet_abhi
Could you please send me the code if not written in OpenGl
plain c
i'will be thankful to you
• 11-26-2003
VirtualAce
Yeah....now I just gotta remember where I put it.

:D