Tuesday, April 22, 2008

Spherical Coordinates

Spherical coordinates provide another system for describing locations in 3-D. It uses the coordinate r, q (theta) and f (phi). Like polar coordinates it makes use of a radial coordinate. The length of the line joining the point to origin is the coordinate r. This same line subtends an angle q (theta) with the y (up/down) axis. As in the cylindrical system f (phi) determines the angular coordinate in the x-z plane. This is often referred to as the azimuthal coordinate.
Here is a patch that lets you play with 3-D spherical coordinates. The code is in the comments section. Since it uses cartesian coordinates we translate from spherical to cartesian as we send messages to OpenGL.

Here is how spherical and cartesian coordinates are related:
x = r cos(q) cos(f)
z = r cos(q) sin(f)
y = r sin(q)

1 comment:

Tony said...

max v2;
#N vpatcher 25 489 351 727;
#P origin -19 51;
#P window setfont "Sans Serif" 9.;
#P flonum 206 65 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#N vpatcher 291 50 1283 587;
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P newex 113 114 29 196617 s r_;
#P newex 280 117 47 196617 s theta_;
#P window linecount 0;
#P newex 677 101 37 196617 s phi_;
#P outlet 391 444 15 0;
#P outlet 358 444 15 0;
#P outlet 325 444 15 0;
#P newex 325 420 76 196617 unpack 0. 0. 0.;
#P hidden message 15 73 17 196617 0.;
#P hidden newex 10 51 48 196617 loadbang;
#P window setfont Symbol 24.;
#P window linecount 1;
#P comment 800 339 100 1507352 q;
#P comment 869 336 100 1507352 f;
#P comment 605 336 100 1507352 f;
#P comment 544 337 100 1507352 q;
#P comment 239 338 100 1507352 q;
#P window setfont "Sans Serif" 9.;
#P window linecount 0;
#P newex 658 47 29 196617 t b f;
#P newex 250 44 40 196617 t b b f;
#P newex 65 50 40 196617 t b b f;
#P window setfont "Sans Serif" 20.;
#P comment 717 334 249 196628 z=r*cos( )*cos( );
#P comment 461 334 230 196628 x=r*cos( )*sin( );
#P comment 161 334 152 196628 y=r*sin( );
#P window setfont Symbol 24.;
#P comment 293 42 100 1507352 q;
#P comment 690 43 100 1507352 f;
#P window setfont "Sans Serif" 9.;
#P newex 227 162 29 196617 * 1.;
#P newex 796 232 29 196617 * 1.;
#P newex 562 234 29 196617 * 1.;
#P flonum 280 70 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 325 119 23 196617 cos;
#P newex 227 124 21 196617 sin;
#P flonum 95 76 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 796 162 29 196617 * 1.;
#P newex 562 162 29 196617 * 1.;
#P flonum 677 75 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 796 122 23 196617 cos;
#P newex 562 123 21 196617 sin;
#P flonum 796 291 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 227 289 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 325 398 61 196617 pak 0. 4. 2.;
#P flonum 562 291 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P window setfont "Sans Serif" 20.;
#P comment 110 42 100 196628 r;
#P inlet 65 28 15 0;
#P inlet 658 24 15 0;
#P inlet 250 23 15 0;
#P hidden connect 33 0 34 0;
#P connect 2 0 25 0;
#P hidden connect 34 0 13 0;
#P connect 25 2 13 0;
#P connect 13 0 41 0;
#P connect 16 0 14 0;
#P fasten 25 1 19 0 85 156 232 156;
#P lcolor 1;
#P connect 14 0 19 0;
#P connect 19 0 6 0;
#P fasten 13 0 19 1 100 151 251 151;
#P lcolor 7;
#P connect 0 0 26 0;
#P hidden connect 34 0 16 0;
#P connect 26 2 16 0;
#P connect 16 0 40 0;
#P connect 16 0 15 0;
#P fasten 4 0 5 0 567 318 330 318;
#P connect 5 0 35 0;
#P connect 35 0 36 0;
#P fasten 6 0 5 1 232 323 355 323;
#P connect 35 1 37 0;
#P fasten 7 0 5 2 801 326 380 326;
#P connect 35 2 38 0;
#P connect 10 0 8 0;
#P fasten 25 1 11 0 85 157 567 157;
#P lcolor 1;
#P connect 8 0 11 0;
#P fasten 26 1 17 0 270 227 567 227;
#P connect 11 0 17 0;
#P connect 17 0 4 0;
#P fasten 13 0 11 1 100 151 586 151;
#P lcolor 7;
#P fasten 15 0 17 1 330 209 586 209;
#P lcolor 13;
#P connect 1 0 27 0;
#P connect 27 1 10 0;
#P hidden connect 34 0 10 0;
#P connect 10 0 39 0;
#P connect 10 0 9 0;
#P connect 9 0 12 0;
#P fasten 25 1 12 0 85 156 801 156;
#P lcolor 1;
#P connect 12 0 18 0;
#P fasten 26 1 18 0 270 227 801 227;
#P lcolor 1;
#P connect 18 0 7 0;
#P fasten 13 0 12 1 100 151 820 151;
#P lcolor 7;
#P fasten 15 0 18 1 330 209 820 209;
#P lcolor 13;
#P pop;
#P newobj 128 94 88 196617 p sphere_to_cart;
#P window setfont "Sans Serif" 18.;
#P comment 130 38 34 196626 r;
#P window setfont Symbol 20.;
#P comment 208 41 34 1507348 f;
#P window setfont "Sans Serif" 9.;
#P flonum 167 65 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 128 65 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P window setfont Symbol 20.;
#P comment 168 41 34 1507348 q;
#P window setfont "Sans Serif" 9.;
#P flonum 206 149 35 9 -2.5 2.5 3 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 167 149 35 9 -2.5 2.5 3 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 128 149 35 9 -2.5 2.5 3 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P window setfont "Sans Serif" 18.;
#P comment 212 118 34 196626 z;
#P comment 177 119 34 196626 y;
#P comment 140 119 34 196626 x;
#P window setfont "Sans Serif" 9.;
#N vpatcher 447 237 1248 775;
#P origin 8 326;
#P window setfont "Sans Serif" 9.;
#P message 15 290 23 196617 10.;
#P newex 723 401 40 196617 + 180.;
#P newex 720 374 34 196617 * -1.;
#P newex 707 305 29 196617 t b f;
#P newex 720 354 40 196617 * 180.;
#P newex 720 333 63 196617 / 3.141593;
#P newex 719 282 47 196617 r theta_;
#P window linecount 0;
#P newex 634 378 34 196617 + 90.;
#P window linecount 1;
#P newex 621 309 29 196617 t b f;
#P window linecount 0;
#P newex 555 358 29 196617 r r_;
#P window linecount 1;
#P newex 634 358 40 196617 * 180.;
#P newex 634 337 63 196617 / 3.141593;
#P window linecount 0;
#P newex 633 286 37 196617 r phi_;
#P newex 636 398 66 196617 pack 1. 2. 3.;
#P window linecount 1;
#P message 360 448 227 196617 blend_enable 1 \, depth_enable 0 \, blend_mode 6 1;
#P message 350 430 363 196617 reset \, glcolor 0.3 0. 0. \, moveto 0. 0. 0. \, shapeorient 0 \$2 0 \, circle \$1 \$3 90.;
#P newex 350 468 86 196617 jit.gl.sketch food;
#P window linecount 0;
#P message 53 313 72 196617 line_width \$1;
#P flonum 51 279 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 113 381 227 196617 blend_enable 1 \, depth_enable 1 \, blend_mode 6 1;
#P window linecount 2;
#P message 101 344 222 196617 reset \, glcolor 0. 0. 0.7 \, moveto 0. 0. 0. \, lineto \$1 \$2 \$3 \, glcolor 0.7 0. 0. \, lineto \$1 \$2 \$3;
#P window linecount 1;
#P newex 103 401 86 196617 jit.gl.sketch food;
#P newex 398 203 40 196617 * 180.;
#P newex 398 182 63 196617 / 3.141593;
#P newex 354 227 54 196617 pack 0. 0.;
#P newex 376 135 29 196617 t b f;
#P newex 354 159 54 196617 cartopol;
#P message 364 272 227 196617 blend_enable 1 \, depth_enable 0 \, blend_mode 6 1;
#P message 354 254 357 196617 reset \, glcolor 0. 0.3 0. \, moveto 0. 0. 0. \, shapeorient 90 0 0 \, circle \$1 0. \$2;
#P newex 354 292 86 196617 jit.gl.sketch food;
#P window linecount 0;
#P hidden newex 0 62 48 196617 loadbang;
#P message 365 340 227 196617 blend_enable 1 \, depth_enable 1 \, blend_mode 6 1;
#P message 102 80 310 196617 blend_enable 0 \, depth_enable 1 \, blend_mode 6 1 \, lighting_enable 1;
#P window linecount 1;
#P newex 264 220 66 196617 pak 0. 0. 0.;
#P message 355 322 266 196617 reset \, glcolor 0.5 0.5 0.5 \, moveto \$1 \$2 \$3 \, sphere 0.1;
#P newex 355 360 86 196617 jit.gl.sketch food;
#P window linecount 2;
#P message 85 48 399 196617 reset \, glcolor 0.5 0.5 0.5 \, shapeorient 0 90 0 \, cylinder 0.025 0.025 2.5 \, shapeorient 0 0 0 \, cylinder 0.025 0.025 2.5 \, shapeorient 90 0 0 \, cylinder 0.025 0.025 2.5;
#P window linecount 1;
#P newex 85 109 86 196617 jit.gl.sketch food;
#P inlet 320 104 15 0;
#P inlet 292 103 15 0;
#P inlet 264 103 15 0;
#P connect 10 0 40 0;
#P connect 40 0 23 0;
#P connect 22 0 23 0;
#P hidden connect 10 0 4 0;
#P connect 8 0 3 0;
#P connect 4 0 3 0;
#P connect 10 0 20 0;
#P connect 7 0 20 0;
#P hidden connect 10 0 8 0;
#P connect 21 0 19 0;
#P connect 20 0 19 0;
#P connect 23 0 19 0;
#P connect 10 0 21 0;
#P hidden connect 10 0 7 0;
#P connect 0 0 7 0;
#P connect 1 0 7 1;
#P connect 2 0 7 2;
#P connect 10 0 25 0;
#P connect 27 0 25 0;
#P connect 26 0 24 0;
#P connect 25 0 24 0;
#P fasten 15 0 14 0 381 155 359 155;
#P connect 0 0 14 0;
#P connect 14 0 16 0;
#P connect 10 0 12 0;
#P connect 16 0 12 0;
#P connect 12 0 11 0;
#P connect 13 0 11 0;
#P hidden connect 10 0 6 0;
#P connect 7 0 6 0;
#P connect 9 0 5 0;
#P connect 6 0 5 0;
#P connect 10 0 26 0;
#P connect 10 0 13 0;
#P hidden connect 10 0 9 0;
#P connect 2 0 15 0;
#P fasten 15 1 14 1 400 155 403 155;
#P connect 14 1 17 0;
#P connect 17 0 18 0;
#P connect 18 0 16 1;
#P connect 28 0 32 0;
#P connect 32 1 29 0;
#P connect 29 0 30 0;
#P connect 30 0 33 0;
#P connect 32 0 27 0;
#P connect 31 0 27 0;
#P connect 37 0 27 0;
#P connect 33 0 27 1;
#P connect 39 0 27 2;
#P connect 34 0 37 0;
#P connect 37 1 35 0;
#P connect 35 0 36 0;
#P connect 36 0 38 0;
#P connect 38 0 39 0;
#P pop;
#P newobj 128 169 88 196617 p sketch;
#P hidden message 4 86 70 196617 size 320 240;
#P hidden newex 17 32 50 196617 loadbang;
#P hidden message 6 54 70 196617 camera 2 4 4;
#P newex 18 204 152 196617 jit.window food @depthbuffer 1;
#P newex 18 146 50 196617 t b erase;
#P newex 18 184 87 196617 jit.gl.render food;
#P hidden connect 4 0 5 0;
#P hidden connect 4 0 3 0;
#P connect 19 0 1 0;
#P connect 15 0 1 0;
#P connect 14 0 1 0;
#P hidden fasten 3 0 0 0 11 161 23 161;
#P connect 1 0 0 0;
#P fasten 1 1 0 0 41 173 23 173;
#P hidden connect 5 0 2 0;
#P connect 14 0 18 0;
#P connect 18 0 10 0;
#P connect 10 0 6 0;
#P connect 15 0 18 1;
#P connect 18 1 11 0;
#P connect 11 0 6 1;
#P connect 19 0 18 2;
#P connect 18 2 12 0;
#P connect 12 0 6 2;
#P pop;