Elements of programming and 3D graphics
Overview
Teaching: 30 min
Exercises: 0 minQuestions
…
Objectives
…
The following is a list of points in 3D, that we wish to plot - they are actually the vertices of an icosahedron, $\tau$ is the golden ratio we had above $1/2(1+\sqrt{5})$
#TODO: question - could go to Hamiltonian cycles now?
tau = 1.619
icos_vertex_list = [[1,0,tau], [-1,0,tau], [1,0,-tau], [-1,0,-tau], [0, tau, 1], [0, tau, -1], [0, -tau, 1], [0, -tau, -1], [tau, 1, 0], [tau, -1, 0], [-tau, 1, 0], [-tau, -1, 0]]
point3d(icos_vertex_list, size=50)
For next step you need to use some programming language, as doing this interactively is not productive. First we need to introduce some notions, namely ranges, loops and conditional statements.
for n in range(1,6):
print(n)
1
2
3
4
5
We now do a double loop over two variables with a non-trivial ‘function’. This gives the ‘allowed’ triangulation numbers in Caspar-Klug theory. Note that we define this as a set, so that the order does not matter and duplicates do not occur.
l=set([])
for h in range(0,6):
for k in range(0,6):
l.add(h^2+h*k+k^2)
l
{0, 1, 3, 4, 7, 9, 12, 13, 16, 19, 21, 25, 27, 28, 31, 37, 39, 48, 49, 61, 75}
Now let us loop over a larger range, and combine this with an if statement. We are generating large numbers of ‘allowed’ triangulation numbers. But say, we are only interested in finding potential $(h,k)$ pairs that give rise to the largest known triangulation in a virus, which for now (2018) is $499$ for Cafeteria roenbergensis virus (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5511168/).
for h in range(0,20):
for k in range(h,20):
t = h^2+h*k+k^2
if t == 499:
print(h, k)
(7, 18)
So there is a unique pair (up to chirality).
In the following example, we check that there is only one unordered 5-tuple of nonegative integers such that one of these integers is 1, and the sum of their squares is 60 (those familiar with group theory will recognise that this gives the dimensions of irreducible representations of the icosahedral group). This also uses a couple of nested for loops combined with an if statement to show the uniquess of the solution ‘by brute force’/enumeration.
order = 60
for i1 in range(1,8):
for i2 in range(i1,8):
for i3 in range(i2,8):
for i4 in range(i3,8):
if (1^2+i1^2+i2^2+i3^2+i4^2==order):
print(1, i1, i2, i3, i4)
(1, 3, 3, 4, 5)
Find the adjacencies of vertices. Which distances occur?
for i in range(0,1):
for j in range(0,12):
print(round(norm(vector(icos_vertex_list[i])-vector(icos_vertex_list[j])),2))
0.0
2.0
3.24
3.81
2.0
3.24
2.0
3.24
2.0
2.0
3.24
3.24
Find all those with distance (squared) 2
adj=[]
for i in range(0,len(icos_vertex_list)):
adj.append([])
for j in range(0,12):
if round(norm(vector(icos_vertex_list[i])-vector(icos_vertex_list[j])),2)==2.00:
adj[i].append(j)
print(adj[i])
[1, 4, 6, 8, 9]
[0, 4, 6, 10, 11]
[3, 5, 7, 8, 9]
[2, 5, 7, 10, 11]
[0, 1, 5, 8, 10]
[2, 3, 4, 8, 10]
[0, 1, 7, 9, 11]
[2, 3, 6, 9, 11]
[0, 2, 4, 5, 9]
[0, 2, 6, 7, 8]
[1, 3, 4, 5, 11]
[1, 3, 6, 7, 10]
Getting the list of edges
icos_edges = []
for i in range(0,len(adj)):
for j in range(0,len(adj[i])):
icos_edges.append( line3d( [icos_vertex_list[i], icos_vertex_list[adj[i][j]]], thickness=10) )
vertices = point3d(icos_vertex_list, size=100)
icosahedron = vertices
for edge in icos_edges:
icosahedron = icosahedron + edge
show(icosahedron)
Key Points