Welcome, guest | Sign In | My Account | Store | Cart

A sphere is a ball, similar to a circle in three dimension, and all its surface’s points are equidistant from the centre.

However, the distance from the centre point to the surface is called the radius, and the equation of the sphere is : (x-a)² + (y-b)² + (z-c)² = r² .

(a,b,c) are the coordinates of the centre and r is the radius.

Python, 111 lines
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 #On the name of ALLAH and may the blessing and peace of Allah #be upon the Messenger of Allah Mohamed Salla Allahu Aliahi Wassalam. #Author : Fouad Teniou #Date : 06/05/10 #version :2.6 """ Sphere class represents a geometric sphere and a completing_the_squares function is used for the purpose, while an utility _checksign function is used to check the sign of all the coefficients and return an empty string for a positive number and a minus character for a negative number. A string representation function for the three different outcome possibilities is used to print the solution of the sphere equation. """ from math import sqrt class Sphere(object): """ class that represents a geometric Sphere """ def __init__(self,coef_A = 0,coef_B = 0, coef_C = 0, coef_D= 0, coef_E = 0, coef_F = 0, coef_G = 0): """ Sphere Construction takes coef_A,coef_B,coef_C,coef_D,coef_E,coef_F,coef_G constants """ self._A = coef_A self._B = coef_B self._C = coef_C self._D = coef_D self._E = coef_E self._F = coef_F self._G = coef_G self._a = self._checkSign(self._D) self._b = self._checkSign(self._E) self._c = self._checkSign(self._F) self._d = pow((self._D/2.0)/self._A,2) self._e = pow((self._E/2.0)/self._B,2) self._f = pow((self._F/2.0)/self._C,2) self._g = chr(253) self._h = (-self._G/self._A + self._d + self._e + self._f) def _checkSign(self,value): """ Utility method to check the values' sign and return a sign string""" if value >= 0: return "+" else : return "" def completing_the_squares(self): """ completing the squares function """ c_squares = "(x%s %s%sx + %s) + (y%s %s%sy + %s) + (z%s %s%sz + %s) = %s" % \ (self._g,self._a,self._D/self._A,self._d, self._g,self._b,self._E/self._B,self._e, self._g,self._c,self._F/self._C,self._f,self._h) return c_squares def __str__(self): """ String representation of a sphere """ print ("\n(x%s%s)%s + (y%s%s)%s + (z%s%s)%s = %s") % \ (self._a,(self._D/2.0)/self._A,self._g,self._b,(self._E/2.0)/self._B, self._g,self._c,(self._F/2.0)/self._C,self._g,self._h) if self._h > 0: return "\n

1 comment Fouad Teniou (author) 11 years, 8 months ago

Though, a profile and a secret.

Good luck!

https://acrobat.com/#d=aEjxtq78QkGKUxa*UprkZQ Created by Fouad Teniou on Thu, 6 May 2010 (MIT)