Skip to content

Instantly share code, notes, and snippets.

@barrysmyth
Last active April 18, 2022 09:51
Show Gist options
  • Save barrysmyth/3ee74c594c9e0ab78d48f123316d8c55 to your computer and use it in GitHub Desktop.
Save barrysmyth/3ee74c594c9e0ab78d48f123316d8c55 to your computer and use it in GitHub Desktop.
A modified Edmund Harriss type plot of a Collatz orbit.
import math
import numpy as np
import matplotlib.pylab as plt
def harriss_plot_adj(
ax, n,
angle=11.45, twist=1.8, # Controls the odd/even turn angles
decay=.999, # Rate of decay of segment length
lw=10, # Initial segment thickness
taper=0.99, # Rate of decay of segment thickness/alpha
*args, **kwargs
):
"""A modified Harris visualisation of the orbit of a given Collatz number, n.
Args:
ax: The plotting axis (matplotlib).
n: The Collatz number whose orbit we wish to visualise.
angle: The line segments for even numbers use -angle (clockwise)
twist: The odd numbers use an angle of angle*twist (anti-clockwise).
decay: The rate of decay of the length of the line segments.
lw: The thickness of the linsegents at the root (1).
taper: The rate of decay of the thickness (and alpha) of the line segments.
"""
# Convention is that 1 is the root, so reverse the orbit.
orbit = collatz_orbit(n)[::-1]
prev_x, prev_y, heading = 0, 0, 0
# For each element in the orbit ...
for i, o in enumerate(orbit):
# Update the current heading based on the even/odd turn angle.
heading += turn_angle(o, angle=angle, twist=twist)
# Calculate the new (x,y) ...
new_x = prev_x + ((decay**i)*math.cos(math.radians(heading)))
new_y = prev_y + ((decay**i)*math.sin(math.radians(heading)))
# Plot the current line segment after setting the width and alpha
width = lw*(taper**i) # Segments closer to the root (1) are thicker.
alpha = taper**i # Segments closer to the root are darker/less transparent.
ax.plot([prev_x, new_x], [prev_y, new_y], lw=width, alpha=alpha, *args, **kwargs)
# Update the prev (x, y).
prev_x, prev_y = new_x, new_y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment