0

I am working on a graphing program that I am calling PyGraph. It allows you to create a graph of any size and draw on it, and later in development I will provide coordinates and things, but for now I have one question: How can I draw a intersecting lines through the center to represent the origin?

Here is what I have so far:

#pygraph
import pygame
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((640,480))
x=0
y=0
size=16
screen.fill((255,255,255))
pygame.draw.line(screen, (0,0,0), (screen.get_width()/2,0),(screen.get_width()/2,screen.get_height()),5)
pygame.draw.line(screen, (0,0,0), (0,screen.get_height()/2),(screen.get_width(),screen.get_height()/2),5)
while True:
    while y<480:
        pygame.draw.rect(screen,(0,0,0),(x,y,size,size),1)
        if x>640:
            x=0
            y+=size
            pygame.draw.rect(screen,(0,0,0),(x,y,size,size),1)
        x+=size
    for e in pygame.event.get():
        if e.type==QUIT:
            exit()
        if e.type==KEYUP:
            if e.key==K_SPACE:
                x=0
                y=0
                screen.fill((255,255,255))
                pygame.draw.line(screen, (0,0,0), (screen.get_width()/2,0),(screen.get_width()/2,screen.get_height()),5)
                pygame.draw.line(screen, (0,0,0), (0,screen.get_height()/2),(screen.get_width(),screen.get_height()/2),5)
                size=input('Enter size: ') 
    pygame.display.flip()

The lines go though the center, but it doesn't work for every size graph. I'm not the best at math, but I hope this isn't obvious.. any advice?

1 Answer 1

1

The problem is that you draw the grid using the top left corner as your anchor. That is, all your grid rectangles have one corner in the top left. This becomes a problem when the distance between the center line and the screen edge is not divisible by the size - you can't divide a line of 640 units into even divisions of 15, for example.

A far better solution would be to use the center as the anchor. So basically, all the grid rectangles have one corner in the center of the graph, which means you will never get any "remainder" on the center line, and the "remainder" will instead be on the border of the graph, which looks much nicer.

Here is code for anchoring your rectangles at the center (should replace your original while y<480 loop):

while y<=480/2+size: 
    pygame.draw.rect(screen,(0,0,0),(640/2+x, 480/2+y,size,size),1)
    pygame.draw.rect(screen,(0,0,0),(640/2-x, 480/2+y,size,size),1)
    pygame.draw.rect(screen,(0,0,0),(640/2+x, 480/2-y,size,size),1)
    pygame.draw.rect(screen,(0,0,0),(640/2-x, 480/2-y,size,size),1)
    x+=size
    if x>=640/2+size:
        x=0
        y+=size

Brief explanation: I change the anchor of the rectangle (the point you pass into pygame.draw.rect) to the center of the graph, and instead of drawing one rectangle, I draw four - one in each quadrant of the graph.

I also fixed the code a bit to not need to call pygame.draw.rect() in the if statement.

A minor style tip: Replace 480 and 640 with "screen.width" and "screen.height", so you can adjust the width and height later without problems.

2
  • Wow, I would have never figured that out. Thanks :) But just a question, was my original code.. bad?
    – Sam Tubb
    Feb 7, 2014 at 1:06
  • 1
    "Good code" is a bit hard to define. Your code is "good" in the sense that it's a correct, working program. However, there are many other things to consider in code, such as "how easy it is to understand for other people" and "how easy it is to make an improvement to your code." One good thing about your code is that the variable names, x, y, and size make sense and are easy to remember, and a bad thing would be that your code isn't organised into blocks, which makes it hard to read/improve later.
    – icedtrees
    Feb 7, 2014 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.