python - 使用 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
当前的输出是这样的:
如您所见,有些三角形未对齐,但我不明白为什么。
解决方案
正如评论中提到的,浮点会给你不正确的结果;您要确保表示两个相邻三角形顶点的共享点是并发的。一种简单的方法是将点坐标减少为整数,并组织计算,以免错误累加。
在下面的例子中,错位被纠正,画布上的每个三角形都由一个多边形表示,并单独绘制;因此,每个三角形都可以在鼠标悬停时被引用,或者通过索引或映射(未实现)寻址。
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()
我添加了一个活动填充属性,当您将鼠标悬停在该属性上时,该属性将更改每个三角形的颜色。
推荐阅读
- dataframe - 如何仅在循环中选择逗号分隔值?
- linux - 虚拟接口的名称解析
- python - TCP 套接字代理转发器
- node.js - 投掷者;//管道中未处理的流错误:错误:ENOENT:没有这样的文件或目录
- reactjs - 打字稿如何在父组件中传递道具
- javascript - 从另一个函数调用 this.setState
- r - 如何获得特定 lambda 的交叉验证套索的系数(不是“1se”或“min”lambda)
- reactjs - 在文本组件中查找字符位置(React Native)
- javascript - 当您必须等待第二个按钮出现时,您如何重复单击两个按钮?
- node.js - 带有nestjs的Typeorm出现错误:运行迁移时在“NOT”处或附近出现语法错误