Plotting Quadratic Functions, pygame

Senad Ibraimoski -student-Mathematical Institute Belgrade senad.ibraimoski at gmail.com
Wed Aug 12 23:27:54 CEST 2009


Hello, I'm a new guy to this group, my professor recommend this group
to me, because I was boring him with questions.I'm new to python, and
I have problem plotting Quadratic Functions. Using module pygame.
Here is the code:

#!/usr/bin/env python
import pygame
def main():
	import sys
	import math
	from math import sqrt
	#Input equation coefficients
	screen=pygame.display.set_mode((400,400))
	pygame.display.set_caption( ' Ploting ' )
	screen.fill((255,255,255))
	pen=((0,0,255))
	dark=(0,0,0)
	ox=200
	oy=200
	a= int(raw_input('Input coefficient a ' ))
	b= int(raw_input('Input coefficient b ' ))
	c= int(raw_input('Input coefficient c ' ))
	pygame.draw.aaline(screen,((255,0,0)),((200,0)) ,((200,400)))
	pygame.draw.aaline(screen,((255,0,0)),((0,200)), ((400,200)))
	if a==0:
		if b==0:
			print 'No solutions'
		else:
			x= -c/b
			print x
	else:
		d=b*b-4*a*c
		if d<0:
			num=complex(-b/(2*a),sqrt(-d)/(2*a))
			print num
		else:
			x1=(-b+sqrt(d))/(2*a)
			x2=(-b-sqrt(d))/(2*a)
			print 'x1= ' ,x1
			print 'x2= ' ,x2
			while 1:
				for event in pygame.event.get():
					if event ==pygame.QUIT:
						print ' Quitting'
						pygame.quit()
						sys.exit(1)

				x=-50;
				while x<=50:
					y=(a*(x**2) + b*x + c) *(-1)
					screen.set_at(((x+ox),(y+oy)),dark)
					pygame.display.flip()
					x=x+1;

	return 0

if __name__ == '__main__': main()

For now I'm plotting function only when Determinant >0. Or in other
words, when equation has two solutions,
x1,x2 e R... Now if you start my program you will see where problem
is. It's with function, It so bad drawn.
When I try to increment x in loop by 0.1 for every pass, I get
problems because method set_at() Which sets pixel requires integer...

Also I see I have problems with event handling here:
while 1:
				for event in pygame.event.get():
					if event ==pygame.QUIT:
						print ' Quitting'
						pygame.quit()
						sys.exit(1)

When I click X, or exit on windows it doesn't exit.
What do you suggest I do...?
I don't have in this year Computer Graphics on my faculty. So this are
my first steps in this area of CS.

Regards,
S.I



More information about the Python-list mailing list