Icosahedron PShape

The following source code may be used to generate an icosahedron composed of 12 vertices and 20 equilateral triangle faces. Each face consists of 3 points taken from the 12 vertices. The vertices are based upon the golden ratio: (1 + sqrt(5))/2. Vertices and faces may be found on the internet with a search for ‘python get_icosahedron_faces’ (AI generated). The python version was created first in a Thonny editor with Imported mode for py5 and secondarily ported to Processing Java. Py5 has two unique functions which simplify the code and are not found in Processing: shape.set_fills() and shape.vertices().

Python Py5 version:

# Uses Imported mode for py5

import numpy as np

def get_icosahedron_vertices():

    mu = (1 + np.sqrt(5)) / 2

    my_vertices = np.array([
        (-1,  mu,  0),
        ( 1,  mu,  0),
        (-1, -mu,  0),
        ( 1, -mu,  0),
        ( 0, -1,  mu),
        ( 0,  1,  mu),
        ( 0, -1, -mu),
        ( 0,  1, -mu),
        ( mu,  0, -1),
        ( mu,  0,  1),
        (-mu,  0, -1),
        (-mu,  0,  1),
    ])

    return np.array(my_vertices)

def get_icosahedron_faces():
    faces = np.array([
        0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11, 1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8,
        3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9, 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1 ])

    return np.array(faces)

def draw_icosahedron():
    global icosa

    my_vertices = get_icosahedron_vertices()
    faces = get_icosahedron_faces() 
    icosa = create_shape()
    icosa.begin_shape(TRIANGLES)
    icosa.vertices(my_vertices[faces])
    icosa.end_shape()
    icosa.set_fills(color(random(255),random(255),random(255)) for _ in range(60))
      
    return icosa
   
def setup():
  size(600, 600, P3D)
  window_title("Rotate with mouse and click to change colors.")
  stroke_weight(3)
  draw_icosahedron()

def draw():
  background(209)
  translate(width/2, height/2, 0)
  scale(100)
  rotate_x(remap(mouse_y, 0, height, PI, -PI))
  rotate_y(remap(mouse_x, 0, width, -PI, PI))
  shape(icosa)
  
def mouse_pressed():
  draw_icosahedron();

Processing Java version:

PShape icosa;

void icosahedron() {
  float mu = (1 + sqrt(5))/2;

  float vertices[][] = {
    {-1.0, mu, 0.0},
    {+1.0, mu, 0.0},
    {-1.0, -mu, 0.0},
    {1.0, -mu, 0.0},
    {0.0, -1.0, mu},
    {0.0, +1.0, mu},
    {0.0, -1.0, -mu},
    {0.0, +1.0, -mu},
    {mu, 0.0, -1.0},
    { mu, 0.0, 1.0},
    { -mu, 0.0, -1.0},
    { -mu, 0.0, 1.0}
  };

  int faces[] = { 0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11, 1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7,
    1, 8, 3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9, 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1 };

  icosa = createShape();
  icosa.beginShape(TRIANGLES);
  for (int i = 0; i < faces.length; i++) {
    icosa.vertex(vertices[faces[i]][0], vertices[faces[i]][1], vertices[faces[i]][2]);
  }
  icosa.endShape();
  icosa.setStrokeWeight(3.0);
  for (int i = 0; i < icosa.getVertexCount(); i++) {
    icosa.setFill(i, color(random(255), random(255), random(255)));
  }
}

void setup() {
  size(600, 600, P3D);
  surface.setTitle("Rotate with mouse and click to change colors.");
  icosahedron();
}

void draw() {
  background(209);
  translate(width/2, height/2);
  rotateX(map(mouseY, 0, height, PI, -PI));
  rotateY(map(mouseX, 0, width, -PI, PI));
  scale(100);
  shape(icosa);
}

void mousePressed() {
  for (int i = 0; i < icosa.getVertexCount(); i++) {
    icosa.beginTessellation();
    icosa.setFill(i, color(random(255), random(255), random(255)));
    icosa.endTessellation();
  }
}

Output:

4 Likes