def end_cutter(self): if self.start and self.old: color = self.frame.colors['cutter'] self.frame.cutter.append((self.old, self.start)) self.draw_line(self.old.x, self.old.y, self.start.x, self.start.y, color, 'cutter') self.start = cfg.Point(exist=False) self.old = cfg.Point(exist=False)
def draw_cutter(self, event): color = self.frame.colors['cutter'] event_x, event_y = cfg.int_n(event.x), cfg.int_n(event.y) if not self.start: self.delete('cutter') self.delete('result') self.frame.cutter = [] self.draw_line(event_x, event_y, event_x + 1, event_y, self.color, 'cutter') self.start = cfg.Point(event_x, event_y, color) self.old = cfg.Point(event_x, event_y, color) else: try: if event.state in cfg.Ctrl: if event_x == self.old.x: m = 0 else: m = abs(event_y - self.old.y) / abs(event_x - self.old.x) if m < math.tan(math.pi / 4): new_point = cfg.Point(event_x, self.old.y, color) else: new_point = cfg.Point(self.old.x, event_y, color) else: raise AttributeError except AttributeError: new_point = cfg.Point(event_x, event_y, color) self.draw_line(self.old.x, self.old.y, new_point.x, new_point.y, color, 'cutter') self.frame.cutter.append([self.old, new_point]) self.old = new_point
def inverse_pixel(self, x, y, point_color): # s = time.time() # print(f'color {x}, {y} from {point_color} to inverse') color = alg.get_color(self, x, y) # print(x, y, color) if color == cfg.WHITE_COLOR: self.set_pixel(cfg.Point(x, y, point_color)) else: self.set_pixel(cfg.Point(x, y, 'white'))
def __init__(self, frame): self.frame = frame self.width = cfg.CANVAS_WIDTH self.height = cfg.CANVAS_HEIGHT self.first = cfg.Point(exist=False) self.old = cfg.Point(exist=False) self.color = 'black' self.edges = [] super().__init__(self.frame, width=self.width, height=self.height, bg='white', highlightbackground='black') self.bind('<ButtonPress-1>', self.draw_line)
def __init__(self, frame): self.frame = frame self.width = cfg.CANVAS_WIDTH self.height = cfg.CANVAS_HEIGHT self.start = cfg.Point(exist=False) self.old = cfg.Point(exist=False) self.section_1 = cfg.Point(exist=False) self.section_2 = cfg.Point(exist=False) self.color = 'black' super().__init__(self.frame, width=self.width, height=self.height, bg='white', highlightbackground='black') self.bind('<ButtonPress-1>', self.draw_section) self.bind('<ButtonPress-3>', self.draw_cutter)
def bresenham_int(canvas, start, finish): x1, y1, x2, y2 = start.x, start.y, finish.x, finish.y dx, dy = x2 - x1, y2 - y1 sx, sy = cfg.sign(dx), cfg.sign(dy) dx, dy = abs(dx), abs(dy) if dx > dy: exchange = 0 else: dx, dy = dy, dx exchange = 1 e = dy + dy - dx x, y = cfg.int_n(x1), cfg.int_n(y1) color = start.color for _ in range(int(dx)): canvas.set_pixel(cfg.Point(x, y, color)) if e >= 0: if exchange == 1: x += sx else: y += sy e -= (dx + dx) if e < 0: if exchange == 1: y += sy else: x += sx e += (dy + dy)
def create_section(self): coors = self.section_coors.get().split() self.section_coors.delete(0, 'end') try: x1, y1 = cfg.int_n(float(coors[0])), cfg.int_n(float(coors[1])) x2, y2 = cfg.int_n(float(coors[2])), cfg.int_n(float(coors[3])) self.canvas.draw_line(x1, y1, x2, y2, self.colors['section']) self.section.append([ cfg.Point(x1, y1, self.colors['section']), cfg.Point(x2, y2, self.colors['section']) ]) except IndexError: mb.showerror('Ошибка', 'Должно быть введено 4 координаты') except ValueError: mb.showerror( 'Ошибка', 'Каждая координата должна быть числом, введенным через пробел')
def create_cutter(self): coors = self.cutter_coors.get().split() self.cutter_coors.delete(0, 'end') try: x1, y1 = cfg.int_n(float(coors[0])), cfg.int_n(float(coors[1])) x2, y2 = cfg.int_n(float(coors[2])), cfg.int_n(float(coors[3])) self.canvas.delete('cutter') self.canvas.delete('result') self.canvas.draw_rectangle(cfg.Point(x1, y1), cfg.Point(x2, y2), self.colors['cutter'], 'cutter') # self.canvas.create_rectangle(x1, y1, x2, y2, fill='', outline=self.colors['cutter'], tag='cutter') # self.cutter = (cfg.Point(x1, y1, self.colors['cutter']), cfg.Point(x2, y2, self.colors['cutter'])) except IndexError: mb.showerror('Ошибка', 'Должно быть введено 4 координаты') except ValueError: mb.showerror( 'Ошибка', 'Каждая координата должна быть числом, введенным через пробел')
def draw_line(self, event): event_x, event_y = cfg.int_n(event.x), cfg.int_n(event.y) if not self.first: # self.create_line(event_x, event_y, event_x + 1, event_y, fill=self.color) self.set_pixel(cfg.Point(event_x, event_y, self.color)) self.first = cfg.Point(event_x, event_y, self.color) self.old = cfg.Point(event_x, event_y, self.color) else: try: if event.state in cfg.Ctrl: if event_x == self.old.x: m = 0 else: m = abs(event_y - self.old.y) / abs(event_x - self.old.x) if m < math.tan(math.pi / 4): new_point = cfg.Point(event_x, self.old.y, self.color) else: new_point = cfg.Point(self.old.x, event_y, self.color) else: raise AttributeError except AttributeError: new_point = cfg.Point(event_x, event_y, self.color) self.create_line(self.old.x, self.old.y, new_point.x, new_point.y, fill=self.color) # alg.bresenham_int(self, self.old, new_point) self.edges.append((self.old, new_point)) self.old = new_point
def draw_section(self, event): event_x, event_y = cfg.int_n(event.x), cfg.int_n(event.y) color = self.frame.colors['section'] if not self.section_1: self.section_1.set(event_x, event_y, color) else: if event.state not in cfg.Ctrl: self.section_2.set(event_x, event_y, color) else: if event_x == self.section_1.x: m = 0 else: m = abs(event_y - self.section_1.y) / abs(event_x - self.section_1.x) if m < math.tan(math.pi / 4): self.section_2.set(event_x, self.section_1.y, color) else: self.section_2.set(self.section_1.x, event_y, color) self.draw_line(self.section_1.x, self.section_1.y, self.section_2.x, self.section_2.y, color) self.frame.section.append([self.section_1, self.section_2]) self.section_1 = cfg.Point(exist=False) self.section_2 = cfg.Point(exist=False)
def draw_point(self): coors = self.coors.get().split() try: for i in range(0, len(coors), 2): color = self.color.get() self.canvas.color = list(cfg.colors.values())[color] point = cfg.Point(float(coors[i]), float(coors[i + 1]), color) self.canvas.draw_line(point) self.coors.delete(0, 'end') except ValueError: mb.showerror('Ошибка', 'Каждая координата должна быть числом') except IndexError: mb.showerror( 'Ошибка', 'Количество координат должно быть больше нуля и четным числом')
def create_cutter(self): coors = self.cutter_coors.get().split() self.cutter_coors.delete(0, 'end') try: for i in range(0, len(coors) - 1, 2): x, y = float(coors[i]), float(coors[i + 1]) self.canvas.draw_cutter(cfg.Point(x, y)) except IndexError: mb.showerror( 'Ошибка', 'Должно быть введено больше двух координат, и их количество должно быть четным' ) except ValueError: mb.showerror( 'Ошибка', 'Каждая координата должна быть числом, введенным через пробел')
def draw_line(self, event): event_x, event_y = cfg.int_n(event.x), cfg.int_n(event.y) if isinstance(event, tk.Event) and event.state == cfg.Shift: self.delete('seed') self.seed = cfg.Point(event_x, event_y, self.color) self.create_oval(event_x - 3, event_y - 3, event_x + 3, event_y + 3, fill='red', tag='seed') elif not self.first: # self.create_line(event_x, event_y, event_x + 1, event_y, fill=self.color) self.set_pixel(cfg.Point(event_x, event_y, self.color)) self.first = cfg.Point(event_x, event_y, self.color) self.old = cfg.Point(event_x, event_y, self.color) else: try: if event.state in cfg.Ctrl: if event_x == self.old.x: m = 0 else: m = abs(event_y - self.old.y) / abs(event_x - self.old.x) if m < math.tan(math.pi / 4): new_point = cfg.Point(event_x, self.old.y, self.color) else: new_point = cfg.Point(self.old.x, event_y, self.color) else: raise AttributeError except AttributeError: new_point = cfg.Point(event_x, event_y, self.color) # self.create_line(self.old.x, self.old.y, new_point.x, new_point.y, fill=self.color) alg.bresenham_int(self, self.old, new_point) self.edges.append((self.old, new_point)) self.old = new_point
def algorithm_seed(canvas, seed_point, delay=False): if not canvas.edges: return mb.showerror( 'Ошибка!', 'Невозможно выполнить действие, так как отсутствует область для закрашивания' ) if not canvas.seed and not seed_point: return mb.showerror( 'Ошибка!', 'Невозможно выполнить действие, так как отсутствует затравочный пиксель' ) else: if canvas.seed: x, y = canvas.seed.x, canvas.seed.y else: seed = list(map(int, seed_point.split())) x, y = seed[0], seed[1] if not affiliation(cfg.Point(x, y), canvas.edges): return mb.showerror( 'Ошибка!', 'Невозможно выполнить действие, так как затравочный пиксель находится ' 'вне области для закрашивания') start = time.time() if canvas.seed: seed_stack = [f'{canvas.seed.x} {canvas.seed.y}'] canvas.delete('seed') canvas.seed = cfg.Point(exist=False) else: seed_stack = [seed_point] while seed_stack: cur_seed = seed_stack.pop() cur_seed = list(map(int, cur_seed.split())) canvas.set_pixel( cfg.Point(int(cur_seed[0]), int(cur_seed[1]), canvas.color)) x_seed = cur_seed[0] x = x_seed + 1 y = cur_seed[1] while get_color(canvas, x, y) == cfg.WHITE_COLOR: canvas.set_pixel(cfg.Point(x, y, canvas.color)) x += 1 # canvas.update() xr = x - 1 x = x_seed - 1 while get_color(canvas, x, y) == cfg.WHITE_COLOR: canvas.set_pixel(cfg.Point(x, y, canvas.color)) x -= 1 # canvas.update() xl = x + 1 x, y = xl, cur_seed[1] + 1 while x <= xr: flag = 0 while get_color(canvas, x, y) == cfg.WHITE_COLOR and x <= xr: if not flag: flag = 1 x += 1 if flag: if x == xr and get_color(canvas, x, y) == cfg.WHITE_COLOR: seed_stack.append(f'{x} {y}') else: seed_stack.append(f'{x - 1} {y}') # flag = 0 x_in = x while get_color(canvas, x, y) != cfg.WHITE_COLOR and x <= xr: x += 1 if x == x_in: x += 1 x, y = xl, cur_seed[1] - 1 while x <= xr: flag = 0 while get_color(canvas, x, y) == cfg.WHITE_COLOR and x <= xr: if not flag: flag = 1 x += 1 if flag: if x == xr and get_color(canvas, x, y) == cfg.WHITE_COLOR: seed_stack.append(f'{x} {y}') else: seed_stack.append(f'{x - 1} {y}') # flag = 0 x_in = x while get_color(canvas, x, y) != cfg.WHITE_COLOR and x <= xr: x += 1 if x == x_in: x += 1 if delay: canvas.update() finish = time.time() - start print(finish) return finish
def restart(self): self.first = cfg.Point(exist=False) self.old = cfg.Point(exist=False)