Principal | Gráficos 3D | Gráficos 2D | Fractales | Math | Códigos | Tutoriales | Links
El sistema de coordenadas Esfericas se puede definir utilizando las
siguientes variables r, rho, theta, donde rho
se mide desde el eje Z y theta desde el eje X.
Las relaciones entre r, rho, theta y x, y,
z son las siguientes:
x=r*sin(rho)*cos(theta)
y=r*sin(rho)*sin(theta)
z=r*cos(rho)
donde r=(x2+y2+z2)1/2.
Ejemplo en OpenGL del uso de las coordenadas esfericas para dibujar los puntos de una esfera de radio 1.
#include <GL/glut.h> #include <stdlib.h> #include <stdio.h> #include <math.h> #define PI 3.141592654 void DibujarEjes(void) { glBegin(GL_LINES); //eje X glVertex3f(-1.5, 0.0, 0.0); glVertex3f(1.5, 0.0, 0.0); //eje Y glVertex3f(0.0, -1.5, 0.0); glVertex3f(0.0, 1.5, 0.0); //eje Z glVertex3f(0.0, 0.0, -1.5); glVertex3f(0.0, 0.0, 1.5); glEnd(); } void DibujarEsfera(void) { float x, y, z; float r=1.0, rho, theta; int i, j; glBegin(GL_POINTS); rho=0.0; x=(float)(r*sin(rho)*cos(theta)); y=(float)(r*sin(rho)*sin(theta)); z=(float)(r*cos(rho)); glVertex3f(x, y, z); for (i=1; i<23; i++) { rho=PI*i/24; for (j=0; j<16; j++) { theta=2*PI*j/16; x=(float)(r*sin(rho)*cos(theta)); y=(float)(r*sin(rho)*sin(theta)); z=(float)(r*cos(rho)); glVertex3f(x, y, z); } } rho=PI; x=(float)(r*sin(rho)*cos(theta)); y=(float)(r*sin(rho)*sin(theta)); z=(float)(r*cos(rho)); glVertex3f(x, y, z); glEnd(); } void init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); gluLookAt(2.0, 2.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //ejes en verde glColor3f (0.0, 1.0, 0.0); DibujarEjes(); //ejes en amarillo glColor3f (1.0, 1.0, 0.0); DibujarEsfera(); glPopMatrix(); glFlush(); } void reshape(int w, int h) { if (!h) return; glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (GLfloat) w/(GLfloat) h, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; } } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(400, 400); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutCreateWindow("Coordenadas Esfericas"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; } |
valcoey@hotmail.com
Ramiro Alcocer, 2001
Principal | Gráficos 3D | Gráficos 2D | Fractales | Math | Códigos | Tutoriales | Links