Please Fix the follwing python program: ** The problem is from 71 to 74 line, th
ID: 3818297 • Letter: P
Question
Please Fix the follwing python program:
** The problem is from 71 to 74 line, the for statement for calculating the moment of inertia **
** note that I'm using 2.7.13 version **
import turtle
class MyRectangle:
x = 0.0
y = 0.0
b = 0.0
b = 0.0
# Get user inputs
def input (self):
self.x = float(raw_input("Lower left corner,x coordinate?"))
self.y= float(raw_input("Lower left corner,y coordinate?"))
self.b= float(raw_input("width of your rectangle?"))
self.h= float(raw_input("Height of your rectangle?"))
# Turtule function to draw the rectangle
def draw(self):
scaler = max(self.x+self.b,self.y+self.h)
turtle.setworldcoordinates(-0.1*scaler,-0.1*scaler,1.1*scaler,1.1*scaler)
# another way to do it.
#scaler= max(compound_shape[i].x + compound_shape[i].b, compound_shape[i].y + compound_shape[i].y)
#turtle.setworldcoordinates(-10,-10,10+scaler,10+scaler)
turtle.penup()
turtle.goto(self.x,self.y)
turtle.pendown()
turtle.goto(self.x+self.b,self.y)
turtle.goto(self.x+self.b,self.y+self.h)
turtle.goto(self.x,self.y+self.h)
turtle.goto(self.x,self.y)
turtle.penup()
turtle.goto(self.x+self.b/2,self.y+self.h/2)
#Function to calculater the area
def area(self):
return self.h*self.b
#Function to calculater the centroid
def x_cent(self):
return self.x+self.b/2
def y_cent(self):
return self.y+self.h/2
#Function to calculater the moment of inertia
def Ix(self):
return self.b*self.h^3/12
def Iy(self):
return self.b^3*self.h/12
compound_shape =[]
num_rect = input("How many rectangles in your compound shape")
scaler=0
for i in range(num_rect):
compound_shape.append(MyRectangle())
compound_shape[i].input()
compound_shape[i].draw()
sum_area = 0.0
sum_xarea = 0.0
sum_yarea = 0.0
for i in range(num_rect):
sum_area = sum_area+compound_shape[i].area()
sum_xarea = sum_xarea+(compound_shape[i].area()*compound_shape[i].x_cent())
sum_yarea = sum_yarea+(compound_shape[i].area()*compound_shape[i].y_cent())
tot_x_centroid = sum_xarea/sum_area
tot_y_centroid = sum_yarea/sum_area
Ix,tot=0
Iy,tot=0
for i in range(num_rect):
Ix,tot +=compound_shape[i],I
Iy,tot +=compound_shape[i],I
Ix,tot = sum[compound_shape[i].Ix()+ compound_shape[i].area()*(compound_shape.tot_x_cent-tot_y_cent)^2]
Iy,tot = sum[compound_shape[i].Iy()+ compound_shape[i].area()*(compound_shape.tot_x_cent-tot_y_cent)^2]
print"Centroid is at(",tot_x_centroid,",",tot_y_centroid,")"
turtle.goto(tot_x_centroid,tot_y_centroid)
Explanation / Answer
please find the code import turtle
class MyRectangle:
x = 0.0
y = 0.0
b = 0.0
b = 0.0
# Get user inputs
def input (self):
self.x = float(raw_input("Lower left corner,x coordinate?"))
self.y= float(raw_input("Lower left corner,y coordinate?"))
self.b= float(raw_input("width of your rectangle?"))
self.h= float(raw_input("Height of your rectangle?"))
# Turtule function to draw the rectangle
def draw(self):
scaler = max(self.x+self.b,self.y+self.h)
turtle.setworldcoordinates(-0.1*scaler,-0.1*scaler,1.1*scaler,1.1*scaler)
# another way to do it.
#scaler= max(compound_shape[i].x + compound_shape[i].b, compound_shape[i].y + compound_shape[i].y)
#turtle.setworldcoordinates(-10,-10,10+scaler,10+scaler)
turtle.penup()
turtle.goto(self.x,self.y)
turtle.pendown()
turtle.goto(self.x+self.b,self.y)
turtle.goto(self.x+self.b,self.y+self.h)
turtle.goto(self.x,self.y+self.h)
turtle.goto(self.x,self.y)
turtle.penup()
turtle.goto(self.x+self.b/2,self.y+self.h/2)
#Function to calculater the area
def area(self):
return self.h*self.b
#Function to calculater the centroid
def x_cent(self):
return self.x+self.b/2
def y_cent(self):
return self.y+self.h/2
#Function to calculater the moment of inertia
def Ix(self):
return self.b*self.h^3/12
def Iy(self):
return self.b^3*self.h/12
compound_shape =[]
num_rect = input("How many rectangles in your compound shape")
scaler=0
for i in range(num_rect):
compound_shape.append(MyRectangle())
compound_shape[i].input()
compound_shape[i].draw()
sum_area = 0.0
sum_xarea = 0.0
sum_yarea = 0.0
for i in range(num_rect):
sum_area = sum_area+compound_shape[i].area()
sum_xarea = sum_xarea+(compound_shape[i].area()*compound_shape[i].x_cent())
sum_yarea = sum_yarea+(compound_shape[i].area()*compound_shape[i].y_cent())
tot_x_centroid = sum_xarea/sum_area
tot_y_centroid = sum_yarea/sum_area
Ix,tot=0
Iy,tot=0
for i in range(num_rect):
Ix,tot =Ix,tot + compound_shape[i].Ix()
Iy,tot =Iy,tot + compound_shape[i].Iy()
Ix,tot = sum[compound_shape[i].Ix()+ compound_shape[i].area()*(compound_shape.tot_x_cent-tot_y_cent)^2]
Iy,tot = sum[compound_shape[i].Iy()+ compound_shape[i].area()*(compound_shape.tot_x_cent-tot_y_cent)^2]
print"Centroid is at(",tot_x_centroid,",",tot_y_centroid,")"
turtle.goto(tot_x_centroid,tot_y_centroid)