Vous êtes sur la page 1sur 3

## Justin Chong 21508335

# short description of your logic and approach to solve the problem: Followed th
e skeleton
# reference of any resources you used: PROJECT 1 SOLUTION from 2015
from graphics import *
import math
Xmin, Xmax = 0, 1280
Ymin, Ymax = 0, 720
#This function is the main control of the flow of the program
#It should draw the main graphics window
#It should call other functions to perform various tasks
#It should control when to draw, undraw the curve and labels.
def main(): # 4 marks
win = GraphWin('Graph', Xmax-Xmin, Ymax-Ymin)
win.setCoords(Xmin, Ymin, Xmax, Ymax)
drawButton, ev, pl, fl, fbr = makeInterface(win)
Pt = win.getMouse()
x = 0
while x==0:
Pt = win.getMouse()
if isClicked(Pt,drawButton):
erase(win)
drawLabels(win)
heightVtime(getInputs(ev,pl,fl,fbr)[0],getInputs(ev,pl,fl,fbr)[1],ge
tInputs(ev,pl,fl,fbr)[2],getInputs(ev,pl,fl,fbr)[3],win)
Pt = (0.0)
def erase(win):
rect = Rectangle(Point(Xmin+5,Ymin+100), Point(Xmax,Ymax-5))
rect.setFill(color_rgb(139,69,19))
rect.draw(win)
#This function should make the Graphical User Interface i.e. the Entry boxes and
the Buttons.
#It should return the variable identifiers of the Buttons and Entry boxes to the
calling function.
def makeInterface(win): #4 marks
win.setBackground(color_rgb(238,0,238))
erase(win)
#rectangle button with text
drawButton = Rectangle(Point(5, 10),Point(190, 85))
drawButton.setFill('#568203')
drawButton.draw(win)
dr = Text(Point(95, 50), "Calculate")
dr.draw(win)
# Entry for Exhaust Velocity
Text(Point(270, 50),'Exhaust Velocity =').draw(win)
ev = Entry(Point(350, 50), 3)
ev.setFill("greenyellow")
ev.draw(win)
# Entry for Payload
Text(Point(420, 50),'Payload =').draw(win)
pl = Entry(Point(475, 50), 3)
pl.setFill("greenyellow")
pl.draw(win)
# Entry for Fuel Load
Text(Point(540, 50),'Fuel Load =').draw(win)

fl = Entry(Point(600, 50), 3)
fl.setFill("greenyellow")
fl.draw(win)
# Entry for Fuel Burn Rate
Text(Point(680, 50),'Fuel Burn Rate =').draw(win)
fbr = Entry(Point(760, 50), 3)
fbr.setFill("greenyellow")
fbr.draw(win)
#labels
drawLabels(win)
return drawButton, ev, pl, fl, fbr

#This function should read the four Entry boxes identified by the four input par
ameters and return their values
def getInputs(ev, pl, fl, fbr): # 2 marks
exhaustVel = float(ev.getText())
payload = float(pl.getText())
fuelLoad = float(fl.getText())
burnRate = float(fbr.getText())
return exhaustVel, payload, fuelLoad, burnRate

#This function should calculate the height of the rocket versus time until the r
ocket reaches back at height =0.
#ev is the exhaust velocity, p is the pay load, f is the fuel load and b is the
burn rate.
#This function should plot the curve as shown in the figure.
#Note that plotting is to be done after every new calculation as the input value
s (exhaust velocity, pay load, fuel load and burn rate) can be changed by the us
er.
#The function should return these calculations as list of Polygon (containing li
st of Points) and Text.
#It should also return the total time of flight and maximum height achieved by t
he rocket.
def heightVtime(e,p,f,b,win): # 4 marks
polygon = []
maxHeight = 0
h=0
t0 = f/b
vMax = vStage1(e,p,f,b,t0)
for i in range(0,5000):
t = i/100
if t<=t0:
h = h + vStage1(e,p,f,b,t)*0.01
if h>maxHeight:
maxHeight = h
polygon.append(Point(t*25+22,h*0.35+120))
elif t>=t0:
h = h + vStage2(vMax,t,t0)*0.01
if h>maxHeight:
maxHeight = h
if h>0:
polygon.append(Point(t*25+22,h*0.35+120))
polygon.append(Point(+22,120))
draw(polygon, win)
eraseMaxheight = Rectangle(Point(770, 10),Point(1500, 85))

eraseMaxheight.setFill('#ee00ee')
eraseMaxheight.setOutline('#ee00ee')
eraseMaxheight.draw(win)
Text(Point(920, 50), "The maximum height is %f" % maxHeight).draw(win)
def vStage1(e,p,f,b,t):
v = e*math.log((p+f)/(p+f-b*t))-9.81*t
return v
def vStage2(vMax,t,t0):
v = vMax-9.81*(t-t0)
return v
def draw(polygon,win):
graph = Polygon(polygon)
graph.setOutline('cyan')
graph.draw(win)
#This function should calculate the y-axis and x-axis labels based on the entrie
s in Polygon 'poly' (which contains the height vs time).
#t is the total time of flight of rocket, P is the loan amount and hmx is the ma
ximum height achieved by the rocket
#It should return a list of Text identifiers for the correct label text and loca
tion (x,y).
#There should be appropriate number of y-axis and x-axis labels to represent hei
ght and time properly. Both x and y labels should be returned in one list.
#Note that it will not draw the labels.
#def makeLabels(poly, t, win, hmx): # 2 marks

#This functions should take the list of labels and draw them.
def drawLabels(win): # 1 mark
for i in range(0,51):
Text(Point(22+(i*25), 120), "%d" %i).draw(win)
for i in range(0,1600,100):
Text(Point(22, 120+(i*0.35)), "%d" %i).draw(win)
#This function should undraw the list of labels.
#def undrawLabels(labels): # 1 mark

#This function should take the Point pClick where the mouse was clicked and the
button identifier and return True if the click was inside the button otherwise i
t should return False.
def isClicked(pClick, button): # 2 marks
if (pClick.getX() > button.getP1().getX()) and (pClick.getY() > button.getP1
().getY()) and (pClick.getX() < button.getP2().getX()) and (pClick.getY() < butt
on.getP2().getY()):
return True
# execute the main function
main()

Vous aimerez peut-être aussi