Clean up and worked on main
This commit is contained in:
parent
d3af7327a7
commit
510ce201cd
45
compiler.py
45
compiler.py
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
import math
|
import math
|
||||||
from typing import Any, Callable, ClassVar, Iterable, Sequence
|
from typing import Callable, ClassVar, Iterable, Sequence
|
||||||
from textwrap import indent
|
from textwrap import indent
|
||||||
|
|
||||||
|
|
||||||
|
@ -1323,6 +1323,33 @@ class Screen:
|
||||||
@property
|
@property
|
||||||
def file_info(self) -> FileInfo: return self._file_info
|
def file_info(self) -> FileInfo: return self._file_info
|
||||||
|
|
||||||
|
@property
|
||||||
|
def top(self) -> None | int: return self._top
|
||||||
|
|
||||||
|
@property
|
||||||
|
def bottom(self) -> None | int: return self._bottom
|
||||||
|
|
||||||
|
@property
|
||||||
|
def right(self) -> None | int: return self._right
|
||||||
|
|
||||||
|
@property
|
||||||
|
def left(self) -> None | int: return self._left
|
||||||
|
|
||||||
|
@property
|
||||||
|
def width(self) -> None | int: return self._width
|
||||||
|
|
||||||
|
@property
|
||||||
|
def height(self) -> None | int: return self._height
|
||||||
|
|
||||||
|
@property
|
||||||
|
def width_scale(self) -> float: return self._width_scale
|
||||||
|
|
||||||
|
@property
|
||||||
|
def height_scale(self) -> float: return self._height_scale
|
||||||
|
|
||||||
|
@property
|
||||||
|
def fps(self) -> int: return self._fps
|
||||||
|
|
||||||
def tree_str(self, pre: str = "", pre_cont: str = "") -> str:
|
def tree_str(self, pre: str = "", pre_cont: str = "") -> str:
|
||||||
s: str = f"{pre} Screen\n"
|
s: str = f"{pre} Screen\n"
|
||||||
if self._top is not None:
|
if self._top is not None:
|
||||||
|
@ -2584,6 +2611,9 @@ class Context:
|
||||||
@property
|
@property
|
||||||
def file_info(self) -> FileInfo: return self._file_info
|
def file_info(self) -> FileInfo: return self._file_info
|
||||||
|
|
||||||
|
@property
|
||||||
|
def screen(self) -> Screen: return self._screen
|
||||||
|
|
||||||
def step(self):
|
def step(self):
|
||||||
for anim in self._animations.values():
|
for anim in self._animations.values():
|
||||||
anim.step(ContextNamespace(self))
|
anim.step(ContextNamespace(self))
|
||||||
|
@ -2997,6 +3027,19 @@ def semantics_analyzer(file: File) -> Context:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def compile(filename: str) -> Context | CompilerError:
|
||||||
|
try:
|
||||||
|
with open(filename, encoding='utf-8') as file:
|
||||||
|
code = file.read()
|
||||||
|
tokens = lexer(code, filename)
|
||||||
|
syntax_tree = syntactical_analyzer(tokens)
|
||||||
|
context = semantics_analyzer(syntax_tree)
|
||||||
|
return context
|
||||||
|
except CompilerError as err:
|
||||||
|
print(err.compiler_error())
|
||||||
|
return err
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
with open("example.graph", encoding='utf-8') as file:
|
with open("example.graph", encoding='utf-8') as file:
|
||||||
|
|
53
equation.py
53
equation.py
|
@ -1,53 +0,0 @@
|
||||||
|
|
||||||
|
|
||||||
class Equation:
|
|
||||||
|
|
||||||
def __init__(self, xf, yf, cf=(255,255,255), wf=1):
|
|
||||||
self.xf = xf
|
|
||||||
self.yf = yf
|
|
||||||
self.cf = cf
|
|
||||||
self.wf = wf
|
|
||||||
|
|
||||||
def _xf(self, t):
|
|
||||||
if callable(self.xf):
|
|
||||||
return self.xf(t)
|
|
||||||
else:
|
|
||||||
return t
|
|
||||||
|
|
||||||
def _yf(self, t):
|
|
||||||
if callable(self.yf):
|
|
||||||
return self.yf(t)
|
|
||||||
else:
|
|
||||||
return t
|
|
||||||
|
|
||||||
def _cf(self, t, r):
|
|
||||||
if callable(self.cf):
|
|
||||||
return self.cf(t, r)
|
|
||||||
else:
|
|
||||||
return self.cf
|
|
||||||
|
|
||||||
def _wf(self, t, r):
|
|
||||||
if callable(self.wf):
|
|
||||||
return self.wf(t, r)
|
|
||||||
else:
|
|
||||||
return self.wf
|
|
||||||
|
|
||||||
def plot(self, r=(0, 12, 0.1), scale=(1,1)):
|
|
||||||
t = r[0]
|
|
||||||
prev_x = self._xf(t)
|
|
||||||
prev_y = self._yf(t)
|
|
||||||
while t <= r[1]:
|
|
||||||
x = self._xf(t)
|
|
||||||
y = self._yf(t)
|
|
||||||
c = self._cf(t, r)
|
|
||||||
w = self._wf(t, r)
|
|
||||||
yield (
|
|
||||||
int(prev_x * scale[0]),
|
|
||||||
int(prev_y * scale[1]),
|
|
||||||
int(x * scale[0]),
|
|
||||||
int(y * scale[1]),
|
|
||||||
c,
|
|
||||||
w,
|
|
||||||
)
|
|
||||||
prev_x, prev_y = x, y
|
|
||||||
t += r[2]
|
|
61
main.py
61
main.py
|
@ -1,7 +1,7 @@
|
||||||
import pygame
|
import pygame
|
||||||
import sys
|
import sys
|
||||||
import math
|
import math
|
||||||
from equation import Equation
|
from compiler import compile, Context, CompilerError
|
||||||
|
|
||||||
# Initialize Pygame
|
# Initialize Pygame
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
@ -15,61 +15,37 @@ pygame.display.set_caption("Equation Plotter")
|
||||||
pygame.mouse.set_visible(False)
|
pygame.mouse.set_visible(False)
|
||||||
MOD_KEYS = pygame.key.get_mods()
|
MOD_KEYS = pygame.key.get_mods()
|
||||||
|
|
||||||
class Hypotrochoid(Equation):
|
def main(filename):
|
||||||
|
|
||||||
def __init__(self, R, r, d, c=(255,255,255)):
|
|
||||||
self.R = R
|
|
||||||
self.r = r
|
|
||||||
self.d = d
|
|
||||||
super().__init__(self._h_xf, self._h_yf, self._h_cf, 5)
|
|
||||||
|
|
||||||
def _h_xf(self, t):
|
|
||||||
return (self.R - self.r) * math.cos(t) + self.d * math.cos(((self.R - self.r) / self.r) * t)
|
|
||||||
|
|
||||||
def _h_yf(self, t):
|
|
||||||
return (self.R - self.r) * math.sin(t) - self.d * math.sin(((self.R - self.r) / self.r) * t)
|
|
||||||
|
|
||||||
def _h_cf(self, t, r):
|
|
||||||
c = int(((t - r[0]) / (r[1] - r[0])) * 255)
|
|
||||||
return c, c, c
|
|
||||||
|
|
||||||
# def _h_wf(self, t, r):
|
|
||||||
# return int(((t - r[0]) / (r[1] - r[0])) * 5)
|
|
||||||
|
|
||||||
def main(equ):
|
|
||||||
SCREEN_WIDTH = screen.get_width()
|
SCREEN_WIDTH = screen.get_width()
|
||||||
SCREEN_HEIGHT = screen.get_height()
|
SCREEN_HEIGHT = screen.get_height()
|
||||||
HALF_WIDTH = SCREEN_WIDTH // 2
|
HALF_WIDTH = SCREEN_WIDTH // 2
|
||||||
HALF_HEIGHT = SCREEN_HEIGHT // 2
|
HALF_HEIGHT = SCREEN_HEIGHT // 2
|
||||||
# Main loop
|
|
||||||
off = 0
|
context = compile(filename)
|
||||||
mouse_x_last, mouse_y_last = pygame.mouse.get_pos()
|
|
||||||
|
if isinstance(context, CompilerError):
|
||||||
|
running = False
|
||||||
|
else:
|
||||||
running = True
|
running = True
|
||||||
|
|
||||||
|
# Main loop
|
||||||
|
mouse_x_last, mouse_y_last = pygame.mouse.get_pos()
|
||||||
while running:
|
while running:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
running = False
|
running = False
|
||||||
|
|
||||||
|
if isinstance(context, Context):
|
||||||
# Clear the screen
|
# Clear the screen
|
||||||
screen.fill((0,0,0))
|
screen.fill((0,0,0))
|
||||||
|
|
||||||
for prev_x, prev_y, x, y, c, w in equ.plot((0 + off, (8 * math.pi) + off, math.pi/32), (100, 100)):
|
# for prev_x, prev_y, x, y, c, w in equ.plot((0 + off, (8 * math.pi) + off, math.pi/32), (100, 100)):
|
||||||
pygame.draw.line(screen, c, (prev_x + HALF_WIDTH, HALF_HEIGHT - prev_y), (x + HALF_WIDTH, HALF_HEIGHT - y), w)
|
# pygame.draw.line(screen, c, (prev_x + HALF_WIDTH, HALF_HEIGHT - prev_y), (x + HALF_WIDTH, HALF_HEIGHT - y), w)
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
pygame.time.Clock().tick(60)
|
pygame.time.Clock().tick(context.screen.fps)
|
||||||
off += math.pi/128
|
|
||||||
|
|
||||||
# if direction:
|
context.step()
|
||||||
# equ.R += 0.1
|
|
||||||
# equ.r += 0.05
|
|
||||||
# if equ.R >= 20:
|
|
||||||
# direction = False
|
|
||||||
# else:
|
|
||||||
# equ.R -= 0.1
|
|
||||||
# equ.r -= 0.05
|
|
||||||
# if equ.R <= 6:
|
|
||||||
# direction = True
|
|
||||||
|
|
||||||
mouse_x, mouse_y = pygame.mouse.get_pos()
|
mouse_x, mouse_y = pygame.mouse.get_pos()
|
||||||
if 5 < math.sqrt(pow(mouse_x_last - mouse_x, 2) + pow(mouse_y_last - mouse_y, 2)):
|
if 5 < math.sqrt(pow(mouse_x_last - mouse_x, 2) + pow(mouse_y_last - mouse_y, 2)):
|
||||||
|
@ -83,7 +59,4 @@ def main(equ):
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
# main(Equation(None, lambda t: math.sin(t)))
|
main("example.graph")
|
||||||
# main(Hypotrochoid(6, 4, 1))
|
|
||||||
main(Hypotrochoid(7, 4, 1))
|
|
||||||
# main(Hypotrochoid(15, 14, 1))
|
|
||||||
|
|
Loading…
Reference in New Issue