working ;)

This commit is contained in:
megamichi 2025-04-03 21:25:06 +02:00
parent aecda058b5
commit 9614de5e76
2 changed files with 74 additions and 39 deletions

103
main.py
View file

@ -1,7 +1,7 @@
#!/usr/bin/python3
import pygame
from math import sin, cos, radians, exp , degrees, log
import math
import random
pygame.init()
## funktionen
@ -16,42 +16,67 @@ def get_debug_text():
text(f"{str(a)} = {str(globalvars[a])}", line_counter)
line_counter += 1
def random_color():
levels = range(32,256,32)
return tuple(random.choice(levels) for _ in range(3))
def frange(start, stop, step):
array = []
while start <= stop:
array.append(round(start, 1)) # Rundet auf 1 Nachkommastelle
start += step
return array
##return [round(start + step * i, 10) for i in range(int((stop - start) / step))]
## klassen
class Math_Function:
def __init__(self):
self.color = (0,255,0)
self.dots = []
def get_y(self,x):
if not x:
return 0
else:
return sin(x)
def calculate(self,graph):
for x in range(0, screensize[0] * resolution):
x -= graph.pos[0]
x /= resolution
if not self.get_y(x) < screensize[1]:
pass
else:
self.dots.append((x, self.get_y(x)))
print(frange(-1,1,0.1))
class Graph:
def __init__(self) -> None:
self.scale = 100
# scale = abstand von einer ganzahl in px
self.on_resize()
self.dots = []
self.fdots = []
self.pos = [0, 0]
self.functions = []
self.function_map = {
"sin": math.sin,
"cos": math.cos,
"tan": math.tan,
"exp": math.exp,
"log": math.log,
"sqrt": math.sqrt,
"abs": abs
}
def on_resize(self):
# self.pos = nulpunkt auf dem bildschirm
self.pos = [screensize[0] // 2, screensize[1] // 2]
def get_y(self, func, x):
if x == 0:
return 0
return eval(func[0], {"x": x, "math": math})
def calculate(self):
self.fdots = []
for _ in self.functions:
self.fdots.append([])
for i, func in enumerate(self.functions):
for x in frange(-50,50,1/resolution):#range(0, screensize[0] * resolution):
#x -= self.pos[0]
#x /= resolution
if not self.get_y(func,x) < screensize[1]:
pass
else:
self.fdots[i].append((x, self.get_y(func,x)))
def draw_grid(self):
### lines
@ -91,33 +116,32 @@ class Graph:
def draw_functions(self):
def get_point(f, p): #function, pointer
return (self.pos[0] + f.dots[p][0] * self.scale,
self.pos[1] - f.dots[p][1] * self.scale)
for func in self.functions:
def get_point(fi,f, p): #function, pointer
return (self.pos[0] + self.fdots[fi][p][0] * self.scale,
self.pos[1] - self.fdots[fi][p][1] * self.scale)
for i, func in enumerate(self.functions):
if draw_mode:
for p in range(len(func.dots) - 1):
for p in range(len(self.fdots[i]) - 1):
pygame.draw.line(
screen,
func.color,
get_point(func, p),
get_point(func, p+1)
self.functions[i][1],
get_point(i, func, p),
get_point(i, func, p+1)
, 6
)
else:
for p in range(len(func.dots)):
for p in range(len(self.fdots[i])):
pygame.draw.circle(
screen,
func.color,
get_point(func,p),
self.functions[i][1],
get_point(i, func,p),
6
)
def update(self):
self.draw_grid()
for func in self.functions:
func.calculate(self)
self.calculate()
self.draw_functions()
## variablen
@ -131,14 +155,16 @@ draw_mode = 0
# 0 = lines , 1 = dots
old_graph_pos = []
old_mouse_pos = []
dragging = False
graph = Graph()
func1 = Math_Function()
graph.functions.append(func1)
graph.functions.append(("2*x",(0,255,0)))
graph.functions.append(("math.sin(x)",(0,255,255)))
graph.functions.append(("5/x",(255,255,0)))
graph.functions.append(("x**2",(255,0,0)))
show_debug = False
running = True
dragging = False
if __name__ == "__main__":
while running:
@ -173,7 +199,7 @@ if __name__ == "__main__":
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEWHEEL:
if event.dict["y"] == -1 and not graph.scale == 20:
if event.dict["y"] == -1 and not graph.scale == 30:
graph.scale -= 1
if event.dict["y"] == 1:
graph.scale += 1
@ -200,9 +226,8 @@ if __name__ == "__main__":
graph.pos[1] = old_graph_pos[1] + offset_y
graph.update()
print(graph.pos)
if show_debug:
get_debug_text()
## bildschirm aktuallisierung

10
test.py Normal file
View file

@ -0,0 +1,10 @@
x = []
for _ in range(len([1,2,3,4])):
x.append([])
for i,a in enumerate([1,2,3,4]):
x[i].append(a)
test = [("a",(1)),("b",(2)),("c",(3))]
for i, a in enumerate(test):
print(a[i])