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

2024-03-28 08:44:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了这个函数来画三角形网格:

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

正如你所看到的,有些三角形没有对齐,但我不明白为什么。你知道吗


Tags: inforifrangeresultwidthlengthelse
1条回答
网友
1楼 · 发布于 2024-03-28 08:44:31

如注释中所述,浮点会给出错误的结果;您需要确保表示两个相邻三角形顶点的共享点是并发的。一个简单的方法是将点坐标减少到整数,并组织计算,这样误差就不会增加。你知道吗

在下面的示例中,校正了未对齐,画布上的每个三角形都由多边形表示,并单独绘制;因此,当鼠标移到上面时,可以引用每个三角形,或者通过索引或映射(未实现)来寻址。你知道吗

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 hereenter image description here

相关问题 更多 >