Article 6EKWW Recursive triangle subdivision

Recursive triangle subdivision

by
John
from John D. Cook on (#6EKWW)

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:

bary1.svg

bary2.svg

bary3.svg

I set the alpha value of the lines to 0.1 so that lines that get drawn repeatedly would appear darker.

The size of the images grows quickly as the number of subdivisions increases. Here are links to the images after five steps: SVG, PNG

Update: Plots using incenter and circumcenter look very different than the plots in this post. See here.

Python code

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[0][0], ps[1][0]] ys = [ps[0][1], ps[1][1]] 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[0], pair[1], 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()
Related postsThe post Recursive triangle subdivision first appeared on John D. Cook.
External Content
Source RSS or Atom Feed
Feed Location http://feeds.feedburner.com/TheEndeavour?format=xml
Feed Title John D. Cook
Feed Link https://www.johndcook.com/blog
Reply 0 comments