# Icosahedron PShape

**URL:** <https://discourse.processing.org/t/icosahedron-pshape/45587>\
**Category:** Gallery\
**Created:** [January 14, 2025, 1:54am UTC](https://discourse.processing.org/t/icosahedron-pshape/45587 "2025-01-14T01:54:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [January 14, 2025, 1:54am UTC](https://discourse.processing.org/t/icosahedron-pshape/45587/1 "2025-01-14T01:54:40Z")

</div>

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:**

```auto
# 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:**

```auto
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:**

 ![Screenshot 2025-01-13 at 7.13.45 PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/c/ac7c1fa308f69ac4500ff5e168af2a0aadb709ca.png)
