在较大的等边三角形内创建等边三角形网格/网格

Creating an equilateral triangle grid / mesh inside a larger equilateral triangle

我有一个多边形的顶点列表,我正在尝试在以输入多边形的当前顶点为中心的较大三角形内创建一个等边三角形网格。

内部三角形边的大小由 L 决定,它将容器边分成 L 等份。最后,我想将所有这些三角形(包括原来的较大三角形)的顶点坐标存储在 Python.

的列表中

我想出的一种方法如下:

不过,我对想法和其他可能更有效的方法持开放态度。

到目前为止,这是我的代码:

import math
import sys


# Constructs the larger, container triangle given the centroid (a vertex from the input polygon)
def construct_eq_triangle(centroid, radius):
    side_length = radius * math.sqrt(3)
    # Calculate three vertices of the container triangle
    a = [centroid[0], centroid[1] + (math.sqrt(3) / 3) * side_length]  # Top vertex
    b = [centroid[0] - (side_length / 2), centroid[1] - (math.sqrt(3) / 6) * side_length]  # Bottom left vertex
    c = [centroid[0] + (side_length / 2), centroid[1] - (math.sqrt(3) / 6) * side_length]  # Bottom right vertex

    return a, b, c


def draw_triangular_grid(vertex, radius, L):

    grid_x = []
    grid_y = []
    
    # contruct the container equilateral triangler around this vertex
    a, b, c = construct_eq_triangle(vertex, radius)
    # Draw the grid here inside a,b,c and fill 'grid_x' and 'grid_y'
   
    # but for now just print the mother triangle
    print("\n Equilateral triangle for vertex " + str(vertex) + ":")
    print((a, b, c))

    return grid_x, grid_y


def main(args):
    # demo data, 4 vertices of a simple square with a length of 8
    vertices = [(2.0, 10.0), (10.0, 10.10), (10.0, 2.0), (2.0, 2.0)]
    radius = 2
    L = 7
    i = 0
    while i <= len(vertices) - 1:
        grid_x, grid_y = draw_triangular_grid(vertices[i], radius, L)
        # process the grid coordinates
        i += 1


# Main entry point
if __name__ == "__main__":
    main(sys.argv[1:])


如果需要计算三角形的顶点:

import math

ax = 0
ay = 100
L = 4
tri = []
Size = 100
dx = 0.5 * Size / L
dy = - math.sqrt(3) * dx

for i in range(L):
    basex = ax - dx * i
    basey = ay + dy * i
    tri.append([(basex, basey), (basex - dx, basey + dy), (basex + dx, basey + dy)])
    for j in range(i):
        tri[-1].extend([(basex + j * 2 * dx, basey),
                        (basex + j * 2 * dx + dx, basey +   dy),
                        (basex + (j + 1) * 2 * dx, basey)])
        tri[-1].extend([(basex + (j + 1) * 2 * dx, basey),
                        (basex + (j + 1) * 2 * dx - dx, basey + dy),
                        (basex + (j + 1) * 2 * dx + dx, basey + dy)])

for i in range(L):
    print(tri[i])
    print()

结果包含三角形条纹作为顶点元组的三元组:

[(0.0, 100.0), (-12.5, 78.34936490538904), (12.5, 78.34936490538904)]

[(-12.5, 78.34936490538904), (-25.0, 56.698729810778076), (0.0, 56.698729810778076),
 (-12.5, 78.34936490538904), (0.0, 56.698729810778076), (12.5, 78.34936490538904), 
 (12.5, 78.34936490538904), (0.0, 56.698729810778076), (25.0, 56.698729810778076)]

[(-25.0, 56.69872981077807), (-37.5, 35.0480947161671), (-12.5, 35.0480947161671), 
 (-25.0, 56.69872981077807), (-12.5, 35.0480947161671), (0.0, 56.69872981077807), 
 (0.0, 56.69872981077807), (-12.5, 35.0480947161671), (12.5, 35.0480947161671), 
 (0.0, 56.69872981077807), (12.5, 35.0480947161671), (25.0, 56.69872981077807), 
 (25.0, 56.69872981077807), (12.5, 35.0480947161671), (37.5, 35.0480947161671)]

[(-37.5, 35.0480947161671), (-50.0, 13.397459621556134), (-25.0, 13.397459621556134), 
 (-37.5, 35.0480947161671), (-25.0, 13.397459621556134), (-12.5, 35.0480947161671), 
 (-12.5, 35.0480947161671), (-25.0, 13.397459621556134), (0.0, 13.397459621556134), 
 (-12.5, 35.0480947161671), (0.0, 13.397459621556134), (12.5, 35.0480947161671), 
 (12.5, 35.0480947161671), (0.0, 13.397459621556134), (25.0, 13.397459621556134), 
 (12.5, 35.0480947161671), (25.0, 13.397459621556134), (37.5, 35.0480947161671), 
 (37.5, 35.0480947161671), (25.0, 13.397459621556134), (50.0, 13.397459621556134)]