首页 > 解决方案 > 使用 tkinter 画布绘制的三角形未对齐

问题描述

我编写了这个绘制三角形网格的函数:

def create_triangles(side_length):
    result = []
    half_width = int(side_length / 2)
    # height = int(side_length * math.sqrt(3) / 2)
    height = side_length
    max_width = 15 * side_length
    max_height = 10 * height

    for i in range(0, max_height, height):
        if (i / height) % 2 == 0:
            for j in range(0, max_width-half_width, half_width):
                if j % side_length == 0:
                    triangle = (i-height/2, j-half_width, i+height/2, j, i-height/2, j+half_width)
                else:
                    triangle = (i-height/2, j, i+height/2, j+half_width, i+height/2, j-half_width)

                result.append(triangle)
        else:
            for j in range(half_width, max_width, half_width):
                if j % side_length == 0:
                    triangle = (i-height/2, j-2*half_width, i+height/2, j-half_width+2, i-height/2, j)
                else:
                    triangle = (i-height/2, j-half_width, i+height/2, j, i+height/2, j-2*half_width)

                result.append(triangle)

    return result

当前的输出是这样的:

当前输出截图

如您所见,有些三角形未对齐,但我不明白为什么。

标签: pythonpython-3.xtkintertkinter-canvas

解决方案


正如评论中提到的,浮点会给你不正确的结果;您要确保表示两个相邻三角形顶点的共享点是并发的。一种简单的方法是将点坐标减少为整数,并组织计算,以免错误累加。

在下面的例子中,错位被纠正,画布上的每个三角形都由一个多边形表示,并单独绘制;因此,每个三角形都可以在鼠标悬停时被引用,或者通过索引或映射(未实现)寻址。

import tkinter as tk
import math


WIDTH, HEIGHT = 500, 500


class Point:
    """convenience for point arithmetic
    """
    def __init__(self, x, y):
        self.x, self.y = x, y
    def __add__(self, other):
        return Point(self.x + other.x, self.y + other.y)
    def __iter__(self):
        yield self.x
        yield self.y


def tile_with_triangles(canvas, side_length=50):
    """tiles the entire surface of the canvas with triangular polygons
    """
    triangle_height = int(side_length * math.sqrt(3) / 2)
    half_side = side_length // 2
    p0 = Point(0, 0)
    p1 = Point(0, side_length)
    p2 = Point(triangle_height, half_side)

    for idx, x in enumerate(range(-triangle_height, WIDTH+1, triangle_height)):
        for y in range(-side_length, HEIGHT+1, side_length):
            y += half_side * (idx%2 + 1)
            offset = Point(x, y)
            pa, pb, pc = p0 + offset, p1 + offset,p2 + offset
            canvas.create_polygon(*pa, *pb, *pc, outline='black', fill='', activefill='red')

    p2 = Point(-triangle_height, half_side)  # flip the model triangle

    for idx, x in enumerate(range(-triangle_height, WIDTH+triangle_height+1, triangle_height)):
        for y in range(-side_length, HEIGHT+1, side_length):
            y += half_side * (idx%2 + 1)
            offset = Point(x, y)
            pa, pb, pc = p0 + offset, p1 + offset,p2 + offset
            canvas.create_polygon(*pa, *pb, *pc, outline='black', fill='', activefill='blue')


root = tk.Tk()
canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg='cyan')
canvas.pack()

tile_with_triangles(canvas) #, side_length=10)

root.mainloop()

我添加了一个活动填充属性,当您将鼠标悬停在该属性上时,该属性将更改每个三角形的颜色。

在此处输入图像描述 在此处输入图像描述


推荐阅读