用tkinter画布绘制的三角形未对齐

时间:2019-04-20 21:27:06

标签: python python-3.x tkinter tkinter-canvas

我编写了此函数来绘制三角形网格:

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

当前输出是这样:

screenshot of current output

您可以看到一些三角形未对齐,但是我不明白为什么。

1 个答案:

答案 0 :(得分:2)

如评论中所述,浮点数会给您不正确的结果;您要确保代表两个相邻三角形的顶点的共享点是并发的。一种简单的方法是将点的坐标简化为整数,并组织计算,以免出现误差。

在以下示例中,校正了不对齐的情况,画布上的每个三角形都由一个多边形表示,并分别绘制;因此,当鼠标悬停在每个三角形上时可以引用每个三角形,也可以通过索引或映射(未实现)对每个三角形进行寻址。

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()

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

enter image description here enter image description here