Jonathan Gardner jgardner at jonathangardner.net
Thu Aug 13 00:34:34 CEST 2009

```Before we get into fixing your code, let's talk about style. A lot of
bugs are solved when you fix the style, and the remaining bugs are
much easier to find and fix.

On Aug 12, 2:27 pm, Senad Ibraimoski -student-Mathematical Institute
> 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

Vertical whitespace is very useful for separating different parts of
your code, just like you do when you write paragraphs.

> import pygame

vertical whitespeace.

> def main():

Throwing everything into one function is rarely a good idea. I'll
identify ways you can separate out the code below into functions. For
now, your code can share data between functions with global variables.
Down the road, you may want to encapsulate the shared data in objects.

>         import sys
>         import math
>         from math import sqrt

Put all the import statements at the top, as much as you can.

>         #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

There are really two parts here: Getting the screen setup and drawing.
Both should be separated into two functions.

>         a= int(raw_input('Input coefficient a ' ))
>         b= int(raw_input('Input coefficient b ' ))
>         c= int(raw_input('Input coefficient c ' ))

Another part deals with collecting user input.

Note that you shouldn't be collecting ints but floats.

(Ideally, you would collect real numbers, but representing that in a
computer is difficult.)

>         pygame.draw.aaline(screen,((255,0,0)),((200,0)) ,((200,400)))
>         pygame.draw.aaline(screen,((255,0,0)),((0,200)), ((400,200)))

This draws the coordinate axes. This belongs in another function.

>         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

The solver belongs in another function.

I would have the solver take a representation of the equation (simply
a tuple of (a, b, c)) and return a list of solutions.

If there are no solutions, then return an empty list.

>                         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;

The main loop belongs in another function.

The drawing bit belongs in another function.

>
>         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...
>

Your drawing function that you write should take a scaling factor in
addition to the equation it is trying to draw.

You'll have to round the floats to the nearest integer. This is pretty
easy to do:

number_as_int = int(number_as_float + 0.5)

Rather than step between arbitrary numbers, step through every point
on the graph. For this equation, y = a x^2 + b x + c, right? So walk
from left-to-right over all visible values of x (0 to 400, or rather
-200 to 200) and find y.

> I don't have in this year Computer Graphics on my faculty. So this are
> my first steps in this area of CS.
>

This is a very good first step. You have a bright future in
programming and perhaps in computer graphics.

```