Back

Function Graph

Sript description

Result

!>

This is my attempt on creating a script that draws a function. It doesnt work properly. You can zoom in on the x axis by the left and right arrow keys and on the y axis with the up and down arous.

Source Code

import pygame
import math
import keyboard

pygame.init()

WIDTH, HEIGHT = 1800, 1000
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Function Graph")

font = pygame.font.Font('freesansbold.ttf', 10)

scaley = 100
scalex = 1
spacing = WIDTH/20
maximum = 0

domain = [-180, 180]



def f(x):
    return math.sin(math.radians(x))
    return x

def drawballat(x, y, size):
    pygame.draw.circle(WIN, (255, 255, 255), (x+WIDTH/2, y+HEIGHT/2), size)

def renderGrid():
    print(scalex, spacing)
    pygame.draw.line(WIN, (200, 200, 200), (0, (HEIGHT / 2)), (WIDTH, (HEIGHT / 2)))
    pygame.draw.line(WIN, (200, 200, 200), (WIDTH/2, 0), (WIDTH/2, HEIGHT))
    for i in range(30):
        print(i)
        pygame.draw.line(WIN, (200, 200, 200), (i*spacing*scalex+WIDTH/2, HEIGHT/2 + 5), (i*spacing*scalex+WIDTH/2, HEIGHT/2 - 5))
        value = round(i*spacing, 1)
        text = font.render(str(value), True, (255, 0, 0))
        textRect = text.get_rect()
        textRect.center = (i*spacing*scalex+WIDTH/2, (HEIGHT // 2)+10)
        WIN.blit(text, textRect)

    for i in range(30):
        print(i)
        pygame.draw.line(WIN, (200, 200, 200), (WIDTH/2-i*spacing*scalex, HEIGHT/2 + 5), (WIDTH/2-i*spacing*scalex, HEIGHT/2 - 5))
        value = round(-1*i*spacing, 1)
        text = font.render(str(value), True, (255, 0, 0))
        textRect = text.get_rect()
        textRect.center = (WIDTH/2-i*spacing*scalex, (HEIGHT // 2)+10)
        WIN.blit(text, textRect)

    pygame.draw.line(WIN, (200, 200, 200), (WIDTH/2-5, HEIGHT/2-scaley), (WIDTH/2+5, HEIGHT/2-scaley))
    text = font.render(str(round(maximum)), True, (255, 0, 0))
    textRect = text.get_rect()
    textRect.center = (WIDTH/2+15, HEIGHT/2-scaley)
    WIN.blit(text, textRect)

    pygame.draw.line(WIN, (200, 200, 200), (WIDTH/2-5, HEIGHT/2-scaley), (WIDTH/2+5, HEIGHT/2-scaley))
    text = font.render(str(round(-1*maximum)), True, (255, 0, 0))
    textRect = text.get_rect()
    textRect.center = (WIDTH/2+15, HEIGHT/2+scaley)
    WIN.blit(text, textRect)


for x in range(domain[0], domain[1]):
    if f(x) > maximum:
        maximum = f(x)
        print(maximum)


running = True
renderGrid()
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    if keyboard.is_pressed("down"):
        print(scaley)
        scaley-=0.3
        WIN.fill((0, 0, 0))
        renderGrid()
    if keyboard.is_pressed("up"):
        print(scaley)
        scaley+=0.3
        WIN.fill((0, 0, 0))
        renderGrid()
    if keyboard.is_pressed("right"):
        print(scalex)
        scalex+=0.01
        WIN.fill((0, 0, 0))
        renderGrid()
    if keyboard.is_pressed("left"):
        print(scalex)
        scalex-=0.01
        WIN.fill((0, 0, 0))
        renderGrid()

    for x in range(domain[0], domain[1]):
        #print(x, f(x))
        drawballat(scalex*x, scaley*f(x), 2)
        if f(x) > maximum:
            maximum = f(x)
            print(maximum)



    pygame.display.update()


pygame.quit()