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

Circle is a program for students and tutors in higher education. Circle program provide the user with the standard form ( x –x0)^2 + (y-y0)^2 = r^2, radius and centre(x0,y0), once the user input the Constants' values of the circle Equation of the form Ax^2 + Ay^2 + Dx + Ey + F = 0. if r^2 = 0, Circle program point and display that the graph is the single point (x0,y0), and if r^2 < 0 , Circle program point that the equation has no real solutions, thus no graph. However Circle program also does compute and display the reverse method if the user input the radius and centre values, it display both forms of the circle equation

Python, 501 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501``` ```#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 : 08/10/08 #Version : 2.4 """ Class of an equation of a circle of the form Ax^2 + Ay^2 + Dx + Ey + F = 0 (A !=0) it represents a circle or a point or has no graph , depending of the radius value. And a class of an equation for the circle of radius r and centred at point (x0,y0). """ import math class Circle(object): """ Class that represent an equation of a circle with A,D,E,F constants properties """ def __init__(self, Avalue,Dvalue,Evalue,Fvalue): """ Circle construction takes A,D,E,F Constants """ self.__A = float(Avalue) self.__D = float(Dvalue) self.__E = float(Evalue) self.__F = float(Fvalue) self._b = chr(253) self._a = self._checkSign(self.__A) self._d= self._checkSign(self.__D) self._e = self._checkSign(self.__E) self._f = self._checkSign(self.__F) self._g = ((self.__D/self.__A)/2) self._g1= self.__D/2 self._h =((self.__E/self.__A)/2) self._h1 = self.__E/2 self._i = self._checkSign(self._g) self._j = self._checkSign(self._h) self._k = (-self.__F/self.__A + self._g**2 +self._h**2) self._k1= (-self.__F + self._g1**2 +self._h1**2) self._l = "%2.2f" % math.sqrt(abs(self._k)) self._l1 = "%2.2f" % math.sqrt(abs(self._k1)) self._m = "(x%s%s)%s+(y%s%s)%s = %s" % \ (self._i,self._g,self._b,self._j,self._h,self._b,self._k) self._m1 = "(x%s%s)%s+(y%s%s)%s = %s" % \ (self._i,self._g1,self._b,self._j,self._h1,self._b,self._k1) self._n = "(%s,%s)" % (-self._g,-self._h) self._n1 = "(%s,%s)" % (-self._g1,-self._h1) def __str__(self): """ String representation of the circle equation, standard form , centre and radius """ try: math.sqrt(self._k) #Circle raises zero degenerate case assert math.sqrt(self._k) != 0,"The graph is the single point %s" % \ Circle.centre(self) if self.__A == 0: return "\n= 0: return "+" else: return "" def radius(self): """ Compute radius of a circle """ if self.__A == 1: return self._l1 else: return self._l def centre(self): """ Compute centre(x0,y0) of a circle """ if self.__A == 1: return self._n1 else: return self._n class Equation(Circle): """Class that represent a radius and the centre of a circle """ def __init__(self,x,y,radius): """Equation construction takes centre(xValue,yValue and radius""" self.__x = float(x) self.__y = float(y) self.__radius = float(radius) self._o = chr(253) self._p = self.__radius**2 self._q = self._checkSign(-self.__x) self._r = self._checkSign(-self.__y) self._s = "(x%s%s)%s + (y%s%s)%s = %s " % \ (self._q,-self.__x,self._o,self._r,-self.__y,self._o,self._p) self._t = self.__x**2 + self.__y**2 -self._p self._u = self._checkSign(self._t) self._v = "x%s + y%s %s %sx %s %sy %s %s = 0 " % \ (self._o,self._o,self._q,-self.__x*2,self._r,-self.__y*2,self._u,self._t) def __str__(self): """ String representation of the circle equation, standard form ,centre and radius """ #Equation raises radius value < 0 assert self.__radius > 0, " %s" ) % \ (self.__radius,self.__x,self.__y,self._s,self._v) if __name__ == "__main__": circle1 = Circle(16,40,16,-7) print circle1 #Though students might use only values of radius and circle print radius.circle1() print centre.circle1() circle2 = Circle(2,24,0,-81) print circle2 del circle2.A circle2.A = 1 print circle2 equation = Equation(2,5,3) print equation for doc in (Circle.A,Circle.D,Circle.E,Circle.F): print doc.__doc__,doc.fget.func_name,doc.fset.func_name,doc.fdel.func_name ######################################################################################## #Version : Python 3.2 #import math #class Circle(object): # """ Class that represent an equation of a circle # with A,D,E,F constants properties""" # # def __init__(self,Avalue,Dvalue,Evalue,Fvalue): # """ Circle constructor takes A,D,F,E constants """ # # self.__A = float(Avalue) # self.__D = float(Dvalue) # self.__E = float(Evalue) # self.__F = float(Fvalue) # # self._b = chr(178) # self._a = self._checkSign(self.__A) # self._d = self._checkSign(self.__D) # self._e = self._checkSign(self.__E) # self._f = self._checkSign(self.__F) # self._g = ((self.__D/self.__A)/2) # self._g1 = self.D/2 # self._h = ((self.__E/self.__A)/2) # self._h1 = self.E/2 # self._i = self._checkSign(self._g) # self._j = self._checkSign(self._h) # self._k = (-self.__F/self.__A +self._g**2 + self._h**2) # self._k1= (-self.__F +self._g1**2 + self._h1**2) # self._l = "%2.2f" % math.sqrt(abs(self._k)) # self._l1= "%2.2f" % math.sqrt(abs(self._k1)) # self._m = "(x%s%s)%s+(y%s%s)%s = %s" % \ # (self._i,self._g,self._b,self._j,self._h,self._b,self._k) # self._m1 ="(x%s%s)%s+(y%s%s)%s = %s" % \ # (self._i,self._g1,self._b,self._j,self._h1,self._b,self._k1) # self._n = "(%s,%s)" % (-self._g,-self._h) # self._n1= "(%s,%s)" % (-self._g1,-self._h1) # # # def squared(self): # self._w =(-self.__F/self.__A +((self.__D/self.__A)/2)**2 + ((self.__E/self.__A)/2)**2) # return self._w # def standardForm(self): # return "(x%s%s)%s+(y%s%s)%s = %s" % \ # (self._checkSign(((self.__D/self.__A)/2)),((self.__D/self.__A)/2),chr(178),self._checkSign(((self.__E/self.__A)/2)),((self.__E/self.__A)/2),chr(178),(-self.__F/self.__A +((self.__D/self.__A)/2)**2 + ((self.__E/self.__A)/2)**2)) # # def __str__(self): # """ String representation of the circle equation, # standard form, centre and radius""" # # try: # math.sqrt(Circle.squared(self)) # # #Circle raises zero degenerate case # assert math.sqrt(Circle.squared(self)) != 0,"The graph is the single point %s" % \ # Circle.centre(self) # if self.__A == 1: # # return "\n= 0: # return "+" # else : # return "" # # def radius(self): # """ Computes radius of a circle """ # if self.__A ==1: # return self._l1 # else: # return "%2.2f" % math.sqrt(abs(Circle.squared(self))) # # def centre(self): # """ Computes centre(x0,y0) of a circle """ # if self.__A == 1: # return self._n1 # else: # return "(%s,%s)" % (-((self.__D/self.__A)/2),-((self.__E/self.__A)/2)) # # # #class Equation(Circle): # """ class that represent a radius and the centre of a circle """ # # def __init__(self,x,y,radius): # """ Equation construction takes centre(xValue,yValue) # and radius """ # # self.__x = float(x) # self.__y = float(y) # self.__radius = float(radius) # # self._o = chr(178) # self._p = self.__radius**2 # self._q = self._checkSign(-self.__x) # self._r = self._checkSign(-self.__y) # self._s = "(x%s%s)%s+(y%s%s)%s = %s" % \ # (self._q,-self.__x,self._o,self._r,-self.__y,self._o,self._p) # self._t = self.__x**2 + self.__y**2 - self._p # self._u = self._checkSign(self._t) # self._v = "x%s + y%s %s%sx %s%sy %s%s = 0" % \ # (self._o,self._o,self._q,-self.__x*2,self._r,-self.__y*2,self._u,self._t) # # def __str__(self): # """ String representation of the circle equation, standard form, # centre and radius""" # # #Equation raises radius value < 0 # assert self.__radius > 0, " %s") %\ # (self.__radius,self.__x,self.__y,self._s,self._v ) # # #if __name__ == "__main__": # circle1 = Circle(10,40,16,-7) # print(circle1) # # print(circle1.radius()) # print(circle1.centre()) # circle1.delA # circle1.A=1 # print(circle1) # circle3 = Circle(5,24,0,-81) # print(circle3) # # circle3.E =80 # print(circle3) # # equation = Equation(2,5,3) # print(equation) # # # for doc in (Circle.A,Circle.D,Circle.E,Circle.F): # print(doc.__doc__,"=",doc.fget.__name__,doc.fset.__name__,doc.fdel.__name__) ####################################################################################### # x² + y² -4.0x -10.0y +20.0 = 0 #A constant = getA setA delA #D constant = getD setD delD #E constant = getE setE delE #F constant = getF setF delF ```

sebastien.renard 15 years, 6 months ago

All the get/set stuff is not very usefull. Use direct access to class members and add a property only if you want to add specific code to handle the member access (get and/or set).

Fouad Teniou (author) 15 years, 6 months ago

Thank your comment. However I used the Properties just for that purpose you mentioned, to handle the member access for the Circle equations' constants. It is recommended to use such programming technique for Mathematics equations and scientific programs, for the benefits of the property's set

David Lambert 15 years, 6 months ago

(original comments were constricted to 3000 characters)

Point 6 is most important.

2) Too much code! Three abbreviated replacement functions (also with _checkSign) ---

``````def radius(self):
""" Compute radius of a circle """

return (self._l,self._l1)[self.__A==1]

def centre(self):
""" Compute centre(x0,y0) of a circle """

return (self._n,self._n1)[self.__A==1]

#syntax for modern python
#return self._n1 if self.__A == 1 else self._n
``````

5a) _checkSign is independent of the class, so don't make it a method of the class. Thus, another way to write the Circle constructor is with

``````def _checkSign(value):
return '+'[value<0:]

def float_and_sign(value):
return (float(value),_checkSign(value),)

class Circle(object):
""" Class that repre...
bad activestate, insert dedent tokens to align left"""

def __init__(self, Avalue,Dvalue,Evalue,Fvalue):
""" Circle construction takes A,D,E,F Constants """

(self.__A,self._a,) = float_and_sign(Avalue)
(self.__D,self._d,) = float_and_sign(Dvalue)
(self.__E,self._e,) = float_and_sign(Evalue)
(self.__F,self._f,) = float_and_sign(Fvalue)

5b) Inheriting Circle into Equation is a bad plan.  The only thing useful it gets appears to be _checkSign, but we have just moved it into the module namespace, so that's not important.  Worse, class Equations lets us define a circle by center and radius.  It's reasonable to ask for that radius.

>>> e = Equation(0,0,1)
(I'm guessing attribute error occurs, none of self._l, self._l1 or self.__A are known.)
``````

6) And finally, the property set methods are incorrect. In this example the equation of circle is the same but the standard forms differ. The "set" methods have to recompute all that stuff in the "__init__" method. You could move _g through _n1 computations into a separate method as a function of self, A,D,E and F, which you could then call from __init__, setA, setD, setE, and setF.

``````>>> circle1 = Circle(16,40,16,-7)
>>> circle1.D = 0
>>> print circle1
<Equation of a circle : 16xý + 16yý + 0x + 16y  -7 = 0

(x+1.25)ý+(y+0.5)ý = 2.25           (-1.25,-0.5)                     1.50

>>> print Circle(16,0,16,-7)
<Equation of a circle : 16xý + 16yý + 0x + 16y  -7 = 0

(x+0.0)ý+(y+0.5)ý = 0.6875            (-0.0,-0.5)                     0.83
``````

finally, read the python library code to get some ideas for how python can work.

Fouad Teniou (author) 15 years, 6 months ago

I realised that you did run Circle on windows python and this is not for professionals and that is why you thought they were faults in the program, while I can assure you it is running perfectly with no mistakes on DOS. However,I write all my programs for Students and Tutors at the universities and expecting them to be professionals and able to use DOS for windows to run my programs or any other computer language program hint ( The professional way). Regarding the _checkSign Utility method I think your comment does not make sense completely since I learned from a Python professional book how to use Utility methods and the fact that it was the only method that the class inherit, is for others to use the program for other purposes and add methods to suite their needs as may be you are not aware of mathematics of universities level. and python library again is for a non professional people as I realised on your last comment and you could find out yourself my point. 1- start using professional tools ( DOS ) to run the Programs I do write 2- Use professionas resources ( Books ) to learn python 3- Circle is highly superior program only for professional

 Created by Fouad Teniou on Wed, 8 Oct 2008 (MIT)

### Required Modules

• (none specified)