• API
• FAQ
• Tools
• Trends
• Archive
SHARE
TWEET

# Untitled

a guest Sep 24th, 2013 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # This is my Quadratic Formula program and more
2.
3. import math, pygame
4. from pygame import *
5. pygame.init()
6. font = pygame.font.SysFont("Arial",18)
7.
8. def mymain():
9.     # global variables being used in other functions
10.     global a, b, c, Vx, Vy, d, x1, x2
11.
12.     # input the a, b, and c vaules by the user
13.     print'What is the coefficient of x^2, a?'
14.     a = raw_input('>>> ')
15.     print'What is the coefficient of x, b?'
16.     b = raw_input('>>> ')
17.     print'What is the constant?'
18.     c = raw_input('>>> ')
19.
20.     # change the input to real values
21.     a = float(a)
22.     b = float(b)
23.     c = float(c)
24.
25.     # discriminant
26.     d = b**2-4*a*c
27.
28.     # vertex of the parabola
29.     Vx = -b/(2*a)
30.     Vy = a*Vx**2+b*Vx+c
31.
32.     print 'The symmetry line is at x = ', Vx
33.     print 'The vertex is at (',Vx,',',Vy,')'
34.
35.
36.     # testing the discriminant
37.     if d < 0:
38.         print 'The solution is imaginary.'
39.         print 'The discriminant is ',d
40.     else:
41.         # evaluate the quadratic formula
42.         x1 = Vx + math.sqrt(d)/(2*a)
43.         x2 = Vx - math.sqrt(d)/(2*a)
44.         x1 = round(x1,2)
45.         x2 = round(x2,2)
46.
47.         # print solution
48.         print "The 'x' intercepts are", x1,"and", x2
49.     GraphEq()
50.
51. def GraphEq():
52.     # setup a window using the pygame library
53.     width,height = 500, 500
54.     screen = pygame.display.set_mode([width+400,height])
56.     screen.fill([255,255,255])
57.
58.     # graph paper scale in k pixels per grid
59.     k = 10
60.
61.     # draw graph paper
62.     for i in range(width/k):
63.         gridx = k*i
64.         gridy = k*i
65.         pygame.draw.line(screen, (100,50,240), (gridx, 0), (gridx, height), 1)
66.         pygame.draw.line(screen, (100,50,240), (0, gridy), (width, gridy), 1)
67.
68.     # draw the x and y axis on the screen
69.     midx = (width/k)/2
70.     midy = (height/k)/2
71.     pygame.draw.line(screen, (0,0,0), (midx*k,0), (midx*k,height), 3)
72.     pygame.draw.line(screen, (0,0,0), (0,midy*k), (width, midy*k), 3)
73.
74.     # graph the parabola across the width
75.     for i in range (width):
76.         x = (width/2 - i)/float(k)
77.         y = a*x**2+b*x+c
78.         pos1 =[width/2+x*k, height/2-y*k]
79. #        pygame.draw.circle(screen, (255,0,0), pos1, 3)
80.         nx = (width/2 - (i+1))/float(k)
81.         ny = a*nx**2+b*nx+c
82.         pos2 =[width/2+nx*k, height/2-ny*k]
83.         pygame.draw.line(screen, (255,0,0), pos1, pos2, 3)
84.
85. # displaying the results symmetry line, vertex, and zeros
86.     symline = font.render("The line of symmetry is at x = "+str(Vx),1,(255,0,0))
87.     screen.blit(symline, (width+10, 20))
88.     instruct = font.render("Select 's' to graph the symmetry line.", 1, (200,0,0))
89.     screen.blit(instruct, (width+20, 45))
90.
91.     vertex = font.render("The vertex is at ("+str(Vx)+","+str(Vy)+")",1,(0,255,0))
92.     screen.blit(vertex, (width+10, 75))
93.     instruct = font.render("Select 'v' to plot the vertex point.", 1, (0,200,0))
94.     screen.blit(instruct, (width+20, 100))
95.     if d<0:
96.         xint = font.render("The are no x-intercepts.",1,(0,0,255))
97.         screen.blit(xint, (width+10, 130))
98.         instruct = font.render("The discriminant is "+str(d), 1, (0,0,200))
99.         screen.blit(instruct, (width+20, 155))
100.     else:
101.         xint = font.render("The x-intercepts are at ("+str(x1)+",0) ("+str(x2)+", 0)",1,(0,0,255))
102.         screen.blit(xint, (width+10, 130))
103.         instruct = font.render("Select 'x' to plot the x-intercepts.", 1, (0,0,200))
104.         screen.blit(instruct, (width+20, 155))
105.     instruct = font.render("Select 'n' to show graph another parabola.", 1, (0,0,0))
106.     screen.blit(instruct, (width+10, height-30))
107.
108.     # run an infinite loop to control the window
109.     active = True
110.     while active:
111.         # update the screen
112.         pygame.display.update()
113.
114.         # keyboard and mouse actions
115.         for event in pygame.event.get():
116.             if event.type == pygame.QUIT:
117.                 active = False
118.             if event.type == KEYDOWN:
119.                 if event.key == K_s:
120.                     pygame.draw.line(screen, (200,0,0), (width/2+Vx*k,10),(width/2+Vx*k,height-10),3)
121.                 if event.key == K_v:
122.                     pygame.draw.circle(screen, (0,200,0), (int(width/2+Vx*k),int(height/2-Vy*k)),4)
123.                 if event.key == K_x:
124.                     if d<0:
125.                         instruct = font.render("The discriminant is "+str(d), 1, (250,0,0))
126.                         screen.blit(instruct, (width+20, 155))
127.                     else:
128.                         pygame.draw.circle(screen, (0,0,200), (int(width/2+x1*k),int(height/2)),4)
129.                         pygame.draw.circle(screen, (0,0,200), (int(width/2+x2*k),int(height/2)),4)
130.
131.                 if event.key == K_n:
132.                     mymain()
133.
134.     # if exited from loop, then just quit
135.     pygame.quit()
136.
137.
138.
139.
140.
141. if __name__=='__main__':
142.     mymain()
RAW Paste Data
Top