Object-oriented 2

Polar points

A polar point on a plane can be defined a distance from the origin and an angle (in degree).

Step 1

Write a simple Polar class.

Skeleton:

  • polar1.py:
#!/usr/bin/python

# -*- coding: UTF-8 -*-

import math # to convert degrees in radians

class Polar(object):
    def __init__(self, r, degree):
    pass # ADD YOUR CODE HERE

    def putPolar(self, r, degree):
    pass # ADD YOUR CODE HERE

    def __repr__(self):
    pass # ADD YOUR CODE HERE

P1 = Polar(1.0, 180.0)
P2 = Polar(3.0, 90.0)

print "P1 = ", P1
print "P2 = ", P2

Step 2

  • polar2.py
#!/usr/bin/python

# -*- coding: UTF-8 -*-

import math

class Polar(object):
    def __init__(self, r, degree):
    """Polar constructor"""
    pass

    def putPolar(self, r, degree):
    """Polar accessor to modify the attributes of a Polar object (mutator)"""
    pass

    def __repr__(self):
    """Representation of a Polar object"""
    pass

    def getPolar(self):
    """Polar accessor that returns the attributes of a Polar object as a polar tuple (r, theta)"""
    pass

    def getCart(self):
    """Polar accessor that returns the attributes of a Polar object as a cartesian tuple (x, y)"""
    pass

P1 = Polar(1.0, 180.0)
P2 = Polar(2.0, 90.0)

print "P1 = ", P1
print "P2 = ", P2

R2, T2 = P2.getPolar()
X2, Y2 = P2.getCart()

print R2, T2
print X2, Y2
P1 =  r = 1.000000, theta = 3.141593
P2 =  r = 2.000000, theta = 1.570796
2.0 90.0
1.22464679915e-16 2.0

Step 3

  • polar3.py
#!/usr/bin/python

# -*- coding: UTF-8 -*-

import math

class Polar(object):
    def __init__(self, r, degree):
    """Polar constructor"""
    pass

    def putPolar(self, r, degree):
    """Polar accessor to modify the attributes of a Polar object (mutator)"""
    pass

    def __repr__(self):
    """Representation of a Polar object"""
    pass

    def getPolar(self):
    """Polar accessor that returns the attributes of a Polar object as a polar tuple (r, theta)"""
    pass

    def getCart(self):
    """Polar accessor that returns the attributes of a Polar object as a cartesian tuple (x, y)"""
    pass

    def putCart(self, x, y):
    """Polar accessor that modify the attributes of a Polar object by giving the corresponding cartesian coordinates"""
    pass

P1 = Polar(1.0, 180.0)
P2 = Polar(2.0, 90.0)

print "P1 = ", P1
print "P2 = ", P2

R2, T2 = P2.getPolar()
X2, Y2 = P2.getCart()

print R2, T2
print X2, Y2

P1.putCart(0.0, -3.0)
P2.putCart(4.0, 0.0)

print "P1 = ", P1
print "P2 = ", P2
P1 =  r = 1.000000, theta = 3.141593
P2 =  r = 2.000000, theta = 1.570796
2.0 90.0
1.22464679915e-16 2.0
P1 =  r = 3.000000, theta = -1.570796
P2 =  r = 4.000000, theta = 0.000000

Step 4

  • polar4.py
#!/usr/bin/python

# -*- coding: UTF-8 -*-

import math

class Polar(object):
    """The Polar Class that describes point2D object either in polar coordinates (r, theta), with theta in degrees, or in cartesian coordinates (x, y)"""

    def __init__(self, r, degree):
    """Polar constructor"""
    pass

    def __repr__(self):
    """Representation of a Polar object"""
    pass

    def __putPolar(self, (r, degree)):
    pass

    def __getPolar(self):
    pass

    def __getCart(self):
    pass

    def __putCart(self, (x, y) ):
    pass

    __cart_doc = """Cartesian attributes of a Polar object"""
    __polar_doc = """Polar attributes of a Polar object"""
    polar = property(__getPolar, __putPolar, doc=__polar_doc)
    cart = property(__getCart, __putCart, doc=__cart_doc)


P1 = Polar(1.0, 180.0)
P2 = Polar(2.0, 90.0)

print "P1 = ", P1
print "P2 = ", P2

R2, T2 = P2.polar
X2, Y2 = P2.cart

print R2, T2
print X2, Y2

P1.cart = 0.0, -3.0
P2.cart = 4.0,  0.0

print "P1 = ", P1
print "P2 = ", P2
P1 =  r = 1.000000, theta = 3.141593
P2 =  r = 2.000000, theta = 1.570796
2.0 90.0
1.22464679915e-16 2.0
P1 =  r = 3.000000, theta = -1.570796
P2 =  r = 4.000000, theta = 0.000000

Step 5

  • polar5.py:
#!/usr/bin/python

# -*- coding: UTF-8 -*-

import math

class Polar(object):
    """The Polar Class that describes point2D object either in polar coordinates (r, theta), with theta in degrees, or in cartesian coordinates (x, y)"""

    def __init__(self, r, degree):
    """Polar constructor"""
    pass

    def __repr__(self):
    """Representation of a Polar object"""
    pass

    def __putPolar(self, (r, degree)):
    pass

    def __getPolar(self):
    pass

    def __getCart(self):
    pass

    def __putCart(self, (x, y) ):
    pass

    __cart_doc = """Cartesian attributes of a Polar object"""
    __polar_doc = """Polar attributes of a Polar object"""
    polar = property(__getPolar, __putPolar, doc=__polar_doc)
    cart = property(__getCart, __putCart, doc=__cart_doc)

    def distance(p1, p2):
    """Static method to compute the distance between two Polar object"""
    pass
    distance = staticmethod(distance)


if __name__ == "__main__":
    P1 = Polar(1.0, 180.0)
    P2 = Polar(2.0, 90.0)

    print "P1 = ", P1
    print "P2 = ", P2

    R2, T2 = P2.polar
    X2, Y2 = P2.cart

    print R2, T2
    print X2, Y2

    P1.cart = 0.0, -3.0
    P2.cart = 4.0,  0.0

    print "P1 = ", P1
    print "P2 = ", P2

    print Polar.distance(P1, P2)
P1 =  r = 1.000000, theta = 3.141593
P2 =  r = 2.000000, theta = 1.570796
2.0 90.0
1.22464679915e-16 2.0
P1 =  r = 3.000000, theta = -1.570796
P2 =  r = 4.000000, theta = 0.000000
Distance between P1 and P2 =  5.0

Step 6

What about storing a Polar object using (internally) cartesian coordinates instead of polar coordinates. From the user point of view, it should change nothing. This is encapsulation 😃.

A complete new implementation of a Polar module could be (with documentation and tests):

  • Polar.py
#!/usr/bin/python

# -*- coding: UTF-8 -*-

import math

__doc__ = """
This is the Polar module
"""
__author__ = "Gerald Monard"
__date__ = "2013-04-18"
__version__ = "0.1"


class Polar(object):
    """The Polar Class that describes point2D object either in polar coordinates (r, theta), with theta in degrees, or in cartesian coordinates (x, y)

>>> P1 = Polar(1.0, 180.0)
>>> P2 = Polar(2.0, 90.0)
>>> print "P1 = ", P1
P1 =  r = 1.000000, theta = 180.000000
>>> print "P2 = ", P2
P2 =  r = 2.000000, theta = 90.000000
>>> R2, T2 = P2.polar
>>> X2, Y2 = P2.cart
>>> print R2, T2
2.0 90.0
>>> print X2, Y2
1.22464679915e-16 2.0
>>> P1.cart = 0.0, -3.0
>>> P2.cart = 4.0,  0.0
>>> print "P1 = ", P1
P1 =  r = 3.000000, theta = -90.000000
>>> print "P2 = ", P2
P2 =  r = 4.000000, theta = 0.000000
>>> print Polar.distance(P1, P2)
5.0
"""

    __slots__ = ["_x", "_y"]
    def __init__(self, r, degree):
    """Polar constructor"""
    self._x, self._y = Polar._polar2cart(r, degree)

    def __repr__(self):
    """Representation of a Polar object"""
    return "r = %f, theta = %f" % self.polar

    def _getPolar(self):
    return Polar._cart2polar(self._x, self._y)

    def _putPolar(self, r, degree):
    self._x, self._y = Polar._polar2cart(r, degree)

    def _getCart(self):
    return self._x, self._y

    def _putCart(self, t):
    self._x = t[0]
    self._y = t[1]

    _cart_doc = """Cartesian attributes of a Polar object"""
    _polar_doc = """Polar attributes of a Polar object"""
    polar = property(_getPolar, _putPolar, doc=_polar_doc)
    cart = property(_getCart, _putCart, doc=_cart_doc)

    def distance(p1, p2):
    """Static method to compute the distance between two Polar object"""
    x1, y1 = p1.cart
    x2, y2 = p2.cart
    d = (x1-x2)**2 + (y1-y2)**2
    return math.sqrt(d)
    distance = staticmethod(distance)

    # some functions to help conversions...
    def _deg2rad(degree):
    return degree/180.0*math.pi
    _deg2rad = staticmethod(_deg2rad)

    def _rad2deg(radian):
    return radian*180.0/math.pi
    _rad2deg = staticmethod(_rad2deg)

    def _cart2polar(x, y):
    r = math.sqrt(x*x + y*y)
    theta = math.atan2(y,x)
    return r, Polar._rad2deg(theta)
    _cart2polar = staticmethod(_cart2polar)

    def _polar2cart(r, degree):
    theta = Polar._deg2rad(degree)
    x = r * math.cos(theta)
    y = r * math.sin(theta)
    return x, y
    _polar2cart = staticmethod(_polar2cart)


if __name__ == "__main__":
    import doctest
    doctest.testmod()

Using the Polar module:

#!/usr/bin/env python

# -*- coding: UTF-8 -*-

from Polar import Polar


P1 = Polar(1.0, 180.0)
P2 = Polar(2.0, 90.0)

print "P1 = ", P1
print "P2 = ", P2

R2, T2 = P2.polar
X2, Y2 = P2.cart

print R2, T2
print X2, Y2

P1.cart = 0.0, -3.0
P2.cart = 4.0,  0.0

print "P1 = ", P1
print "P2 = ", P2

print "Distance between P1 and P2 = ", Polar.distance(P1, P2)

Pydoc output:

Help on module Polar:

NAME
    Polar - This is the Polar module

FILE
    /home/gmonard/Ecrit/Conferences/2013/Ecole-CORREL-Paris/Python-Objet/Polar.py

CLASSES
    __builtin__.object
        Polar

    class Polar(__builtin__.object)
 | The Polar Class that describes point2D object either in polar coordinates (r, theta), with theta in degrees, or in cartesian coordinates (x, y)
 | -----------------------------------------------------------------------------------------------------------------------------------------------
 |                                                                                                                                                
 | >>> P1 = Polar(1.0, 180.0)                                                                                                                     
 | >>> P2 = Polar(2.0, 90.0)                                                                                                                      
 | >>> print "P1 = ", P1                                                                                                                          
 | P1 =  r = 1.000000, theta = 180.000000                                                                                                         
 | >>> print "P2 = ", P2                                                                                                                          
 | P2 =  r = 2.000000, theta = 90.000000                                                                                                          
 | >>> R2, T2 = P2.polar                                                                                                                          
 | >>> X2, Y2 = P2.cart                                                                                                                           
 | >>> print R2, T2                                                                                                                               
 | 2.0 90.0                                                                                                                                       
 | >>> print X2, Y2                                                                                                                               
 | 1.22464679915e-16 2.0                                                                                                                          
 | >>> P1.cart = 0.0, -3.0                                                                                                                        
 | >>> P2.cart = 4.0,  0.0                                                                                                                        
 | >>> print "P1 = ", P1                                                                                                                          
 | P1 =  r = 3.000000, theta = -90.000000                                                                                                         
 | >>> print "P2 = ", P2                                                                                                                          
 | P2 =  r = 4.000000, theta = 0.000000                                                                                                           
 | >>> print Polar.distance(P1, P2)                                                                                                               
 | 5.0                                                                                                                                            
 |                                                                                                                                                
 | Methods defined here:                                                                                                                          
 |                                                                                                                                                
 | __init__(self, r, degree)                                                                                                                      
 | Polar constructor                                                                                                                              
 |                                                                                                                                                
 | __repr__(self)                                                                                                                                 
 | Representation of a Polar object                                                                                                               
 |                                                                                                                                                
 | ----------------------------------------------------------------------                                                                         
 | Static methods defined here:                                                                                                                   
 |                                                                                                                                                
 | distance(p1, p2)                                                                                                                               
 | Static method to compute the distance between two Polar object                                                                                 
 |                                                                                                                                                
 | ----------------------------------------------------------------------                                                                         
 | Data descriptors defined here:                                                                                                                 
 |                                                                                                                                                
 | cart                                                                                                                                           
 | Cartesian attributes of a Polar object                                                                                                         
 |                                                                                                                                                
 | polar                                                                                                                                          
 | Polar attributes of a Polar object                                                                                                             

DATA
    __author__ = 'Gerald Monard'
    __date__ = '2013-04-18'
    __version__ = '0.1'

VERSION
    0.1

DATE
    2013-04-18

AUTHOR
    Gerald Monard