Calculate Surface Area and Volume of a Sphere

Actionscript:
  1. var rad:Number = 1;
  2.  
  3. var surfaceArea:Number = sphereSurfaceArea(rad);
  4. var volume:Number = sphereVolume(rad)
  5. trace("surface area: ", surfaceArea);
  6. trace("volume:", volume);
  7. trace("volume / surface area:", volume / surfaceArea);
  8.  
  9. function sphereSurfaceArea(r:Number):Number{
  10.     return 4 * Math.PI * r * r;
  11. }
  12.  
  13. function sphereVolume(r:Number):Number{
  14.     return r * r * r * (4.0/3.0) * Math.PI;
  15. }
  16.  
  17. /*outputs:
  18. surface area:  12.566370614359172
  19. volume: 4.1887902047863905
  20. volume / surface area: 0.3333333333333333
  21. */

Last night I was feeling curious about sphere volume and sphere surface area... I used WolframAlpha to find the neccessary equations and then wrote the above code snippet...

Check out how WolframAlpha does it:
surface area of a sphere
surface area of a sphere with radius 10
sphere volume
sphere volume with radius 10

This entry was posted in Math, misc and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted September 13, 2009 at 9:07 am | Permalink

    volume to surface area will always be r / 3.0

    after regularly picking up ninja scrolls on a daily basis , i’m keen to see what samurai action this is leading to…

  2. Posted September 14, 2009 at 8:23 am | Permalink

    That makes sense…. thanks…. not sure exactly where I was going with this… :)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*