The other day I saw where Cliff Pickover tweeted some images of triangles recursively subdivided by adding a point to the barycenter of each triangle. The images were not what I expected, so I wanted to reproduce the images to look into this further.
Here are the first three steps:
I set the alpha value of the lines to 0.1 so that lines that get drawn repeatedly would appear darker.
Update: Plots using incenter and circumcenter look very different than the plots in this post. See here.
The code below can be used to subdivide any triangle, not just an equilateral triangle, to any desired depth.
I pulled the code to find the center of the triangle out into its own function because there are many ways to define the center of a triangle—more on that here—and I may want to come back and experiment with other centers.
import matplotlib.pyplot as plt import numpy as np from itertools import combinations def center(points): return sum(points)/len(points) def draw_triangle(points): for ps in combinations(points, 2): xs = [ps, ps] ys = [ps, ps] plt.plot(xs, ys, 'b-', alpha=0.1) def mesh(points, depth): if depth > 0: c = center(points) for pair in combinations(points, 2): pts = [pair, pair, c] draw_triangle(pts) mesh(pts, depth-1) points = [ np.array([0, 1]), np.array([-0.866, -0.5]), np.array([ 0.866, -0.5]) ] mesh(points, 3) plt.axis("off") plt.gca().set_aspect("equal") plt.show()