Created
October 28, 2019 02:13
-
-
Save mgritter/83dad667d803681dd7f468296aa8d25b to your computer and use it in GitHub Desktop.
A sequence of determinants of points along a Hilbert curve
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| import matplotlib.pyplot as plt | |
| # Code adapted from https://en.wikipedia.org/wiki/Hilbert_curve | |
| def rot( n, x, y, rx, ry ): | |
| if ry == 0: | |
| if rx == 1: | |
| x = (n-1) - x | |
| y = (n-1) - y | |
| # Swap x and y | |
| return (y,x) | |
| else: | |
| return (x,y) | |
| def d2xy( n, d ): | |
| t = d | |
| x = y = 0 | |
| s = 1 | |
| while s < n: | |
| rx = 1 & (t//2) | |
| ry = 1 & (t ^ rx) | |
| (x,y) = rot(s, x, y, rx, ry) | |
| x += s * rx | |
| y += s * ry | |
| s *= 2 | |
| t //= 4 | |
| return (x,y) | |
| def det( v1, v2 ): | |
| (a,b) = v1 | |
| (c,d) = v2 | |
| return a*d - b*c | |
| n = 32 | |
| d = 0 | |
| v1 = d2xy( n, 0 ) | |
| print( v1 ) | |
| points = [] | |
| labels = [] | |
| points.append( v1 ) | |
| for d in range( 1, n**2+1 ): | |
| v2 = d2xy( n, d ) | |
| points.append( v2 ) | |
| t = det( v1, v2 ) | |
| labels.append( (v2, t) ) | |
| print( v2, t ) | |
| v1 = v2 | |
| if n <= 32: | |
| plt.plot( [x for (x,y) in points[:-1]], [y for (x,y) in points[:-1]] ) | |
| for p, l in labels: | |
| plt.annotate( str(l), xy=p, xytext=(1,1), textcoords="offset points" ) | |
| plt.show() | |
| plt.plot( [d for (p,d) in labels], "r." ) | |
| plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment