# formula to draw a sphere

This topic is 4491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, I'm trying to make my own functions to create mathematical structures such as spheres, circles, etc... But I'm stuck now. What is the math behind drawing a sphere? I managed to draw a circle. Here is the code:
void Circle(float radius, float center_x, float center_y, float center_z)
{
float angle;
float current_x;
float current_y;
float next_x;
float next_y;

current_y = center_y;

glBegin(GL_LINES);
for (angle = 0;angle < 2*M_PI;angle+=0.001)
{

glVertex3f(current_x, current_y, center_z);
glVertex3f(next_x, next_y, center_z);

current_x = next_x;
current_y = next_y;
}
glEnd();
}


The main formula is: x = radius*cos(angle) + center_x; y = radius*sin(angle) + center_y; But for the sphere I don't manage to get such a formula. We learned at school a formula for a sphere: (x - center_x)² + (y - center_y)² + (z - center_z)² = radius² Can I use it in my program? I hope you understand my problem. Thanks, Yoran

##### Share on other sites
Well, if you can draw a circle then you can draw a sphere. A unit circle is r(t)=(cos(t),sin(t)) a t varies from 0 to 2pi. A unit sphere is r(s,t)=(cos(t)*sin(s),sin(t)*sin(s),cos(s)) as t varies from 0 to 2pi and s varies from 0 to pi. If you hold s constant then you get a latitude line. If you hold t constant you get a longitude line. So to build a sphere you generate triangle strips between two latitude lines as you circle the globe.

##### Share on other sites
This topic is also currently being discussed here.

##### Share on other sites
Mathworld has a pretty good description of the parametirc sphere equation. Your circle function uses the parametric equation for a circle with one param "angle". With a sphere you are generating a surface with two params. The link above gives equations to derive x, y, and z from the params.

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 15
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633004
• Total Posts
3009840
• ### Who's Online (See full list)

There are no registered users currently online

×