try to add function class

This commit is contained in:
megamichi 2025-03-31 22:18:19 +02:00
parent 57f04ef178
commit aecda058b5

125
main.py
View file

@ -18,6 +18,26 @@ def get_debug_text():
## 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)))
class Graph:
def __init__(self) -> None:
self.scale = 100
@ -25,69 +45,22 @@ class Graph:
self.on_resize()
self.dots = []
self.pos = [0, 0]
self.functions = []
def on_resize(self):
# self.pos = nulpunkt auf dem bildschirm
self.pos = [screensize[0] // 2, screensize[1] // 2]
def drawlines(self):
def draw_grid(self):
### lines
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):
### beschriftung
# 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)
@ -107,6 +80,46 @@ class Graph:
(self.pos[0] + 10, y + 10),
)
# 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 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:
if draw_mode:
for p in range(len(func.dots) - 1):
pygame.draw.line(
screen,
func.color,
get_point(func, p),
get_point(func, p+1)
, 6
)
else:
for p in range(len(func.dots)):
pygame.draw.circle(
screen,
func.color,
get_point(func,p),
6
)
def update(self):
self.draw_grid()
for func in self.functions:
func.calculate(self)
self.draw_functions()
## variablen
screen = pygame.display.set_mode((300, 300), pygame.RESIZABLE)
screensize = pygame.display.get_window_size()
@ -120,6 +133,8 @@ old_graph_pos = []
old_mouse_pos = []
graph = Graph()
func1 = Math_Function()
graph.functions.append(func1)
show_debug = False
running = True
@ -184,9 +199,7 @@ if __name__ == "__main__":
graph.pos[0] = old_graph_pos[0] + offset_x
graph.pos[1] = old_graph_pos[1] + offset_y
graph.drawgrid()
graph.draw()
graph.calculate()
graph.update()
print(graph.pos)