Reddit Question

In [7]:
%pylab inline
import pint  # Units.
import sympy # Symbolic math
from IPython.display import display,Latex,Image # Display latex from Python windows.
Populating the interactive namespace from numpy and matplotlib

Somewhere along the way I learned about 2D shapes and their properties.

In [8]:
class Shape2D(object):
    def __init__(self,area=None,perimeter=None):
        self.area=area
        self.perimeter=perimeter

Test, test, test. Lets make sure we're calculating things correctly.

In [12]:
class Rectangle(Shape2D):
    def __init__(self,width=None,height=None):
        self.width=width
        self.height=height
        self.sides=4
    
    def parallel_axis(self,area_moment,distance):
        return area_moment+self.area*distance**2
    
    @property
    def area(self):
        return self.height*self.width
    @property
    def perimeter(self):
        return 2*self.height+2*self.width   
    @property
    def Ix_c(self):
        # Ix about the centroid.
        return 1/12*self.width*self.height**3
    @property
    def Ix_0(self):
        # Ix about 0,0
        return 1/3*self.width*self.height**3
    @property
    def Iy_c(self):
        # Ix about the centroid.
        return 1/12*self.width**3*self.height
    @property
    def Iy_0(self):
        # Iy about 0,0
        return 1/3*self.width**3*self.height
    
    def __retr__(self):
        return "{}({}x{})".format(self.__class__.__name__,
                                      self.width,
                                      self.height)
    def __str__(self):
        return self.__retr__

# Square is just an special instance of rectangles.
class Square(Rectangle):
    def __init__(self,width=None,height=None,area=None,perimeter=None):
        if width is not None:
            super().__init__(width=width,height=width)
            return
        if height is not None:
            super().__init__(width=height,height=height)
            return
        if area is not None:
            side=math.sqrt(area)
        if perimeter is not None:
            side=perimeter/4
        super().__init__(width=side,height=side)
        return

width=5
height=10
my_rectangle=Rectangle(width, height)
assert my_rectangle.area==width*height
assert my_rectangle.perimeter==width+height+width+height

side=4
my_square=Square(side)
assert my_square.area==side*side
assert my_square.perimeter==side+side+side+side
In [13]:
display(Image("http://i.imgur.com/bFlgPLf.png"))
In [14]:
A=Rectangle(height=3.5,width=2.5)
In [15]:
A.Ix_0
Out[15]:
35.729166666666664
In [16]:
A.Ix_c
Out[16]:
8.932291666666666
In [17]:
A.Iy_0
Out[17]:
18.229166666666664
In [19]:
A.Iy_c
Out[19]:
4.557291666666666
In [20]:
# 
d=6.0-3.5