This commit is contained in:
megamichi 2025-03-31 21:48:37 +02:00
commit 57f04ef178

198
main.py Normal file
View file

@ -0,0 +1,198 @@
#!/usr/bin/python3
import pygame
from math import sin, cos, radians, exp , degrees, log
pygame.init()
## funktionen
def get_debug_text():
def text(text, line_counter):
debug_text = default_font.render(str(text), True, (255,255,255))
screen.blit(debug_text, (0, line_counter * default_font.get_height()))
line_counter = 1
globalvars = globals()
for a in globalvars:
text(f"{str(a)} = {str(globalvars[a])}", line_counter)
line_counter += 1
## klassen
class Graph:
def __init__(self) -> None:
self.scale = 100
# scale = abstand von einer ganzahl in px
self.on_resize()
self.dots = []
self.pos = [0, 0]
def on_resize(self):
# self.pos = nulpunkt auf dem bildschirm
self.pos = [screensize[0] // 2, screensize[1] // 2]
def drawlines(self):
for x in range(self.pos[0] % self.scale, screensize[0], self.scale):
pygame.draw.line(screen, (200, 200, 200), (x, 0), (x, screensize[1]), 1)
for y in range(self.pos[1] % self.scale, screensize[1], self.scale):
pygame.draw.line(screen, (200, 200, 200), (0, y), (screensize[0], y), 1)
def drawgrid(self):
self.drawlines()
self.beschrifte()
# x linie
pygame.draw.line(
screen, (255, 255, 255), (0, self.pos[1]), (screensize[0], self.pos[1]), 2
)
# y linie
pygame.draw.line(
screen, (255, 255, 255), (self.pos[0], 0), (self.pos[0], screensize[1]), 2
)
def rule(self, x):
if not x:
return 0
else:
return log(x)
def calculate(self):
self.dots = []
for x in range(0, screensize[0] * resolution):
x -= self.pos[0]
x /= resolution
if not self.rule(x) < screensize[1]:
pass
else:
self.dots.append((x, self.rule(x)))
def draw(self):
def get_point(p):
return (self.pos[0] + self.dots[p][0] * self.scale,
self.pos[1] - self.dots[p][1] * self.scale)
if draw_mode:
for p in range(len(self.dots) - 1):
pygame.draw.line(
screen,
(255, 0, 0),
get_point(p),
get_point(p+1)
, 6
)
else:
for p in range(len(self.dots)):
pygame.draw.circle(
screen,
(255,0,0),
get_point(p),
6
)
def beschrifte(self):
# x beschriftung
for num, x in enumerate(range(self.pos[0] % self.scale, screensize[0], self.scale)):
pygame.draw.line(screen, (255, 255, 255), (x, self.pos[1] + 10), (x, self.pos[1] - 10), 5)
num = self.pos[0] // self.scale - num
num *= -1
screen.blit(
default_font.render(str(num), False, (255, 255, 255)),
(x + 10, self.pos[1] + 10),
)
# y beschriftung
for num, y in enumerate(range(self.pos[1] % self.scale, screensize[1], self.scale)):
pygame.draw.line(screen, (255, 255, 255), (self.pos[0] + 10, y), (self.pos[0] - 10, y), 5)
num = self.pos[1] // self.scale - num
screen.blit(
default_font.render(str(num), False, (255, 255, 255)),
(self.pos[0] + 10, y + 10),
)
## variablen
screen = pygame.display.set_mode((300, 300), pygame.RESIZABLE)
screensize = pygame.display.get_window_size()
clock = pygame.time.Clock()
default_font = pygame.font.SysFont("sans", 14)
resolution = 3
draw_mode = 0
# 0 = lines , 1 = dots
old_graph_pos = []
old_mouse_pos = []
graph = Graph()
show_debug = False
running = True
dragging = False
if __name__ == "__main__":
while running:
## key managment
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_ESCAPE]:
exit()
if pressed_keys[pygame.K_d] and not preview_pressed_keys[pygame.K_d]:
show_debug = not show_debug
if pressed_keys[pygame.K_SPACE] and not preview_pressed_keys[pygame.K_SPACE]:
draw_mode = not draw_mode
if pressed_keys[pygame.K_PLUS] and not preview_pressed_keys[pygame.K_PLUS]:
resolution += 1
if pressed_keys[pygame.K_MINUS] and not preview_pressed_keys[pygame.K_MINUS]:
resolution -= 1
if pressed_keys[pygame.K_UP]:
graph.pos[1] += 1
if pressed_keys[pygame.K_DOWN]:
graph.pos[1] -= 1
if pressed_keys[pygame.K_LEFT]:
graph.pos[0] += 1
if pressed_keys[pygame.K_RIGHT]:
graph.pos[0] -= 1
preview_pressed_keys = pressed_keys
## event managment
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEWHEEL:
if event.dict["y"] == -1 and not graph.scale == 20:
graph.scale -= 1
if event.dict["y"] == 1:
graph.scale += 1
if event.type == pygame.WINDOWRESIZED:
screensize = pygame.display.get_window_size()
graph.on_resize()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left mouse button
dragging = True
old_graph_pos = graph.pos[:]
old_mouse_pos = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1: # Left mouse button
dragging = False
if dragging:
mouse_x, mouse_y = pygame.mouse.get_pos()
offset_x = mouse_x - old_mouse_pos[0]
offset_y = mouse_y - old_mouse_pos[1]
graph.pos[0] = old_graph_pos[0] + offset_x
graph.pos[1] = old_graph_pos[1] + offset_y
graph.drawgrid()
graph.draw()
graph.calculate()
print(graph.pos)
if show_debug:
get_debug_text()
## bildschirm aktuallisierung
pygame.display.flip()
clock.tick(60)
screen.fill((0, 0, 0))