/
VikBEKH
/
PrimitiveSymba
Обзор
Документация
Войти
/
VikBEKH
/
PrimitiveSymba
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
plotter.py
96 строк
3 KB
MikhailBekhovskiy
Initial commit
11 май 2026, 21:20
11 май 2026, 21:20
5b3c82e
Код
Авторство
О чём код?
import expression # this module allows drawing in text file FIELD_SIZE:tuple[int, int]=(50,50) WHITEBOARD:str='res.txt' class Scene(): field:list[list[int]] def __init__(self, dims:tuple[int,int]=FIELD_SIZE, white:bool=True): self.field = [[]] * dims[0] for i in range(dims[0]): self.field[i] = [0] * dims[1] for j in range(dims[1]): if not white: self.field[i][j] = 1 # 0 is blank # 1 is * (general point) # 2 is - (x axis) # 3 is | (y axis) def render(self, outfname:str=WHITEBOARD): res = '' with open(outfname, 'w') as f: for line in self.field: for el in line: if el == 1: res += '* ' elif el == 2: res += '--' elif el == 3: res += '|' else: res += ' ' res += '\n' f.write(res) # since 2d array differs from classical algebraic coordinates # transformations are required # switch x and y axes def transpose(self): new_field:list[list[int]] = [[]] * len(self.field[0]) for i in range(len(new_field)): new_field[i] = [0] * len(self.field) for j in range(len(new_field[0])): new_field[i][j] = self.field[j][i] self.field = new_field # redirect vertical axis # (x pretranspose) # (y posttranspose) def reflect(self): for i in range(len(self.field)//2): self.field[i], self.field[len(self.field)-1-i] = self.field[len(self.field)-1-i], self.field[i] # parallel shift of expression so that center is at the whiteboard center def center(self, f_x:expression.Operator): thex = expression.Operator(name='x') horizontal = expression.Operator(val=len(self.field)/2) vertical = expression.Operator(val=len(self.field[0])/2) f_x += vertical f_x = f_x.substitute(thex, thex - horizontal) return f_x # draw an open circle def add_circle(self, radius:int, center:tuple[int, int]=(FIELD_SIZE[0]//2, FIELD_SIZE[1]//2)): for x in range(center[0] - radius, center[0] + 1 + radius): for y in range(center[1] - radius, center[1] + 1 + radius): if 0 <= x < len(self.field) and 0 <= y < len(self.field[0]) and (x - center[0])**2 + (y-center[1])**2 < radius*radius: self.field[x][y] = 1 # draw rectangle by two points def add_rectangle(self, low_left:tuple[int,int], high_right:tuple[int, int]): for x in range(low_left[0], high_right[0] + 1): for y in range(low_left[1], high_right[1]): if 0 <= x < len(self.field) and 0<=y<len(self.field[0]): self.field[x][y] = 1 # draw x and y axes def add_axes(self): cy = len(self.field[0])//2 cx = len(self.field)//2 for i in range(len(self.field)): self.field[i][cy] = 2 for j in range(len(self.field[0])): self.field[cx][j] = 3 # plot an expression def plot_function(self, f_x:expression.Operator): tmp_f = self.center(f_x) for x in range(len(self.field)): try: y = int(tmp_f.eval({'x':x})) except ValueError: continue if 0 <= y < len(self.field[0]): self.field[x][y] = 1