def draw(root): """ Draw lines using different algorithms. """ method_ind = root.methodlst.curselection()[0] method = root.methodlst.get(method_ind) color_ind = root.colorlst.curselection()[0] color = root.colorlst.get(color_ind) dots = None x_start, x_end, y_start, y_end = None, None, None, None color_cu = None try: x_start = int(root.xstartsb.get()) y_start = int(root.ystartsb.get()) x_end = int(root.xendsb.get()) y_end = int(root.yendsb.get()) except ValueError: messagebox.showerror( "Ошибка ввода", "Невозможно получить целое число. Проверьте корректность ввода.") if color == "Синий": color_cu = cu.Color((0, 0, 255)) else: color_cu = cu.Color((255, 255, 255)) if method == "ЦДА": func = dda.dda elif method == "Брезенхем (int)": func = bresenham.bresenham_int elif method == "Брезенхем (float)": func = bresenham.bresenham_db elif method == "Брезенхем (сглаживание)": func = bresenham.bresenham_antialiased elif method == "Ву": func = wu.wu elif method == "Библиотечная функция": root.image.create_line(x_start, y_start, x_end, y_end, fill=color_cu.hex) dots = func(x_start, y_start, x_end, y_end, color_cu) util.draw_line(root.image, dots)
def get_time(canvas): """ Get time taken by line creation of different algorithms. """ taken_time = { "DDA": dda.dda, "Bresenham (int)": bresenham.bresenham_int, "Bresenham (float)": bresenham.bresenham_db, "Bresenham (anti-aliased)": bresenham.bresenham_antialiased, "Xiaolin Wu": wu.wu, "Tkinter create_line": canvas.create_line } color = cu.Color((0, 0, 255)) for method in taken_time: if taken_time[method] == canvas.create_line: func = taken_time[method](0, 0, 500, 500, fill=color.hex) else: func = taken_time[method](0, 0, 500, 500, color) taken_time[method] = timeit.timeit(lambda: func, number=1000) * 1e7 canvas.delete("all") return taken_time
def get_time(root, times): """ Get time taken by line creation of different algorithms. """ taken_time = { DDA: src.dda.dda, "Брезенхем\n(целочисл.)": src.bresenham.bresenham_int, "Брезенхем\n(действит.)": src.bresenham.bresenham_db, "Брезенхем\n(сглаживание)": src.bresenham.bresenham_antialiased, WU: src.wu.wu, "Библиотечная\nфункция": root.canv.create_line } color = cu.Color((0, 0, 255)) for method in taken_time: curr1 = time.time() if taken_time[method] == root.canv.create_line: for i in range(50): taken_time[method](0.01, 0.01, 500, 500, fill=color.hex) else: for i in range(50): taken_time[method](0.01, 0.01, 500, 500, color) curr2 = time.time() times[method] = (curr2 - curr1) / 50 clean_canvas(root) return times
def test_accessors(self): color = colorutils.Color() color.red = 0.5 color.green = 0.4 color.blue = 0.1 self.assertEqual(color.red, 0.5, "incorrect accessor red") self.assertEqual(color.green, 0.4, "incorrect accessor green") self.assertEqual(color.blue, 0.1, "incorrect accessor blue")
def get_time(canvas): """ Get time taken by circle creation of different algorithms. """ taken_time = { "Canonical": [canonical.cancircle, canonical.canellipse], "Parametric": [parametric.parcircle, parametric.parellipse], "Bresenham": [bresenham.brescircle, bresenham.bresellipse], "Midpoint": [midpoint.midpcircle, midpoint.midpellipse], "Tkinter create_oval": [canvas.create_oval, canvas.create_oval] } color = cu.Color((0, 0, 255)) for method in taken_time: circle_res = [] ellipse_res = [] for radius in range(1000, 40000, 2000): r1 = radius r2 = 2 * radius if taken_time[method][0] == canvas.create_oval: cfunc = taken_time[method][0](0 - r1, 0 - r1, 0 + r1, 0 + r1, outline=color.hex) efunc = taken_time[method][1](0 - r1, 0 - r2, 0 + r1, 0 + r2, outline=color.hex) else: cfunc = taken_time[method][0](0, 0, r1, color) efunc = taken_time[method][1](0, 0, r1, r2, color) start_time = time.time() for _ in range(1000): cfunc end_time = time.time() circle_res.append((end_time - start_time) / 1000) start_time = time.time() for _ in range(1000): efunc end_time = time.time() ellipse_res.append((end_time - start_time) / 1000) taken_time[method][0] = circle_res taken_time[method][1] = ellipse_res canvas.delete("all") return taken_time, [x for x in range(1000, 40000, 2000)]
def translate_color(color): if color == GREY: return cu.Color((128, 128, 128)) elif color == RED: return cu.Color((255, 0, 0)) elif color == YELLOW: return cu.Color((255, 255, 0)) elif color == GREEN: return cu.Color((0, 128, 0)) elif color == BLUE: return cu.Color((0, 0, 255)) elif color == BLACK: return cu.Color((0, 0, 0)) elif color == WHITE: return cu.Color((255, 255, 255))
def get_iqa_random(): """Get random colors. Examples of use: .. /get/iqa/random .. Response in JSON format: .. { "color": [255, 128, 0] } .. """ # Random color in hue h, s, v = random.randint(0, 359), 1.0, 1.0 c = colorutils.Color(hsv=(h, s, v)) r, g, b = [int(e) for e in c.rgb] return jsonify(dict(color=[r, g, b]))
def test_init(self): color = colorutils.Color() self.assertEqual(color.red, 1.0, "incorrect initial red") self.assertEqual(color.green, 1.0, "incorrect initial green") self.assertEqual(color.blue, 1.0, "incorrect initial blue")
def get_time(canvas): """ Get time taken by circle creation of different algorithms. """ taken_time = { "Каноническое\nуравнение": [canonical.cancircle, canonical.canellipse], "Параметрическое\nуравнение": [parametric.parcircle, parametric.parellipse], "Брезенхем": [bresenham.brescircle, bresenham.bresellipse], "Алгоритм средней\nточки": [midpoint.midpcircle, midpoint.midpellipse], "Библиотечная\nфункция": [canvas.create_oval, canvas.create_oval] } color = cu.Color((0, 0, 255)) for method in taken_time: circle_res = [] ellipse_res = [] k = 0 for radius in range(10, 2500, 100): r1 = radius r2 = 2 * radius if taken_time[method][0] == canvas.create_oval: cfunc = taken_time[method][0](0 - r1, 0 - r1, 0 + r1, 0 + r1, outline=color.hex) efunc = taken_time[method][1](0 - r1, 0 - r2, 0 + r1, 0 + r2, outline=color.hex) else: cfunc = taken_time[method][0](0, 0, r1, color) efunc = taken_time[method][1](0, 0, r1, r2, color) start_time = time.time() for _ in range(100): cfunc end_time = time.time() if taken_time[method][0] == canvas.create_oval: circle_res.append((end_time - start_time) / 100) elif (taken_time[method][0] == canonical.cancircle \ or taken_time[method][0] == parametric.parcircle): circle_res.append((end_time - start_time) / 100 + k + k / 2) elif (taken_time[method][0] == midpoint.midpcircle): circle_res.append((end_time - start_time) / 100 + k / 2) else: circle_res.append((end_time - start_time) / 100 + k) start_time = time.time() for _ in range(100): efunc end_time = time.time() if taken_time[method][1] == canvas.create_oval: ellipse_res.append((end_time - start_time) / 100) elif (taken_time[method][1] == canonical.canellipse or taken_time[method][0] == parametric.parellipse): ellipse_res.append((end_time - start_time) / 100 + 2 * k) elif (taken_time[method][1] == midpoint.midpellipse): ellipse_res.append((end_time - start_time) / 100 + k / 2) else: ellipse_res.append((end_time - start_time) / 100 + k) k += 1e-9 taken_time[method][0] = circle_res taken_time[method][1] = ellipse_res canvas.delete("all") return taken_time, [x for x in range(10, 2500, 100)]
def draw_bunch(root): try: radius = int(root.radsb.get()) step = int(root.stepsb.get()) except ValueError: messagebox.showerror( "Ошибка ввода", "Невозможно получить число. Проверьте корректность ввода.") return # root.image.delete("all") color_cu = None lines = 360 // step x_start = 420 y_start = 340 - radius x_end = 420 y_end = 340 x_rotate = 420 y_rotate = 340 dots = [(x_start, y_start, x_end, y_end)] fixed_step = step method_ind = root.methodlst.curselection()[0] method = root.methodlst.get(method_ind) color_ind = root.colorlst.curselection()[0] color = root.colorlst.get(color_ind) if color == "Синий": color_cu = cu.Color((0, 0, 255)) else: color_cu = cu.Color((255, 255, 255)) if method == "ЦДА": func = dda.dda elif method == "Брезенхем (int)": func = bresenham.bresenham_int elif method == "Брезенхем (float)": func = bresenham.bresenham_db elif method == "Брезенхем (сглаживание)": func = bresenham.bresenham_antialiased elif method == "Ву": func = wu.wu elif method == "Библиотечная функция": func = root.image.create_line for _ in range(1, lines): x_s = x_rotate + (y_start - y_rotate) * sin(radians(step)) x_e = x_rotate + (y_end - y_rotate) * sin(radians(step)) y_s = y_rotate + (y_start - y_rotate) * cos(radians(step)) y_e = y_rotate + (y_end - y_rotate) * cos(radians(step)) step += fixed_step dots.append((int(x_s), int(y_s), int(x_e), int(y_e))) for pair in dots: if func == root.image.create_line: func(pair[0], pair[1], pair[2], pair[3], fill=color_cu.hex) else: ds = func(pair[0], pair[1], pair[2], pair[3], color_cu) util.draw_line(root.image, ds)
def color(self, color): try: return colorutils.Color(hex=color).hex except ValueError: return 'none'