Esempio n. 1
0
def generate_icosahedron(side_length) :
  ''' Generate an icosahedron from the given side length and 
      return an array of 20 triangles component from the icosahedron 
      and his construction base quad set. ''' 
  
  side_lt=side_length                   # Define the littler side of the rectangle.  
  side_gt=side_length*((1+sqrt(5))/2.)  # Compute the length of the greater side with the gold number ( (1+sqrt(5))/2. )
  
  # Define the base quad so that his center is Vertex(0.0, 0.0, 0.0) :
  quad1=[Vertex(-side_lt/2.,-side_gt/2.,0.0),Vertex(side_lt/2.,-side_gt/2.,0.0),Vertex(side_lt/2.,side_gt/2.,0.0),Vertex(-side_lt/2.,side_gt/2.,0.0)]
  
  # Rotate the base quad on the y and z axes from 90°, to obtain the second crossing quad.
  quad2=[rotate_z(rotate_y(quad1[0],90),90), rotate_z(rotate_y(quad1[1],90),90), rotate_z(rotate_y(quad1[2],90),90), rotate_z(rotate_y(quad1[3],90),90)]
  
  # Rotate the base quad on the y and x axes from 90°, to obtain the third crossing quad. 
  quad3=[rotate_x(rotate_y(quad1[0],90),90), rotate_x(rotate_y(quad1[1],90),90), rotate_x(rotate_y(quad1[2],90),90), rotate_x(rotate_y(quad1[3],90),90)]
  
  icosahedron_base_quads=[quad1,quad2,quad3] # We set the bases quads for our polyhedron: an icosahedron.
  
  # Define an array composed of triangles because an icosahedron is composed from 20 equilateral triangles:
  icosahedron_triangle_array=[(icosahedron_base_quads[0][0],icosahedron_base_quads[0][1],icosahedron_base_quads[2][1]),
                              (icosahedron_base_quads[0][1],icosahedron_base_quads[1][0],icosahedron_base_quads[1][1]),
                              (icosahedron_base_quads[0][0],icosahedron_base_quads[0][1],icosahedron_base_quads[2][2]),
                              (icosahedron_base_quads[0][0],icosahedron_base_quads[1][2],icosahedron_base_quads[1][3]),
                              (icosahedron_base_quads[0][0],icosahedron_base_quads[1][3],icosahedron_base_quads[2][1]),
                              (icosahedron_base_quads[0][1],icosahedron_base_quads[2][1],icosahedron_base_quads[1][0]),
                              (icosahedron_base_quads[0][1],icosahedron_base_quads[1][1],icosahedron_base_quads[2][2]),
                              (icosahedron_base_quads[0][0],icosahedron_base_quads[2][2],icosahedron_base_quads[1][2]),
                              (icosahedron_base_quads[0][2],icosahedron_base_quads[0][3],icosahedron_base_quads[2][3]),
                              (icosahedron_base_quads[0][3],icosahedron_base_quads[1][2],icosahedron_base_quads[1][3]),
                              (icosahedron_base_quads[0][2],icosahedron_base_quads[0][3],icosahedron_base_quads[2][0]),
                              (icosahedron_base_quads[0][2],icosahedron_base_quads[1][0],icosahedron_base_quads[1][1]),
                              (icosahedron_base_quads[0][2],icosahedron_base_quads[1][1],icosahedron_base_quads[2][3]),
                              (icosahedron_base_quads[0][3],icosahedron_base_quads[2][3],icosahedron_base_quads[1][2]),
                              (icosahedron_base_quads[0][3],icosahedron_base_quads[1][3],icosahedron_base_quads[2][0]),
                              (icosahedron_base_quads[0][2],icosahedron_base_quads[2][0],icosahedron_base_quads[1][0]),
                              (icosahedron_base_quads[2][0],icosahedron_base_quads[2][1],icosahedron_base_quads[1][3]),
                              (icosahedron_base_quads[2][0],icosahedron_base_quads[2][1],icosahedron_base_quads[1][0]),
                              (icosahedron_base_quads[2][2],icosahedron_base_quads[2][3],icosahedron_base_quads[1][1]),
                              (icosahedron_base_quads[2][2],icosahedron_base_quads[2][3],icosahedron_base_quads[1][2])]
  
  
  # We return the base quad set and the triangle array.
  return [quad1,quad2,quad3],icosahedron_triangle_array 
def generate_polygon_on_xy_side_length(edges,side_length,offset=0) :
  ''' Return an polygon on plan XY: with edges edges from length side_length, with offset offset.
    
            y 
            | 
        ____|____x
            |
            |              plan XY.
  '''

  angle=360.0/side_length
  
  polygon=[]         # Polygon vertice container.
  scale=360.0/edges  # Computing of the angle separating 2 points from the polygon.
  
  start_vertex1=Vertex(-side_length/2.,0.0,0.0)
  start_vertex2=Vertex(side_length/2.,0.0,0.0)
    
  point_to_rotate=start_vertex1
  rotate_point=start_vertex2
  
  polygon.append(point_to_rotate)
  polygon.append(rotate_point)
  
  i=2
  while i < edges :
  
    vertex=rotate_on_xy(rotate_point,abs(180-scale),point_to_rotate)
    
    point_to_rotate=rotate_point
    rotate_point=vertex
    
    polygon.append(vertex)
    
    i += 1
  
  
  center=get_center_from_polygon(polygon) # Compute polygon center.
  
  tmp=[]
  for v in polygon :
    # Translate polygon vertices so as his center is the display center.
    tmp.append(translate(v,-center.wx,-center.wy,-center.wz))
  
  if offset :
    offset_set=[]
    for v in tmp :
      offset_set.append(rotate_z(v,offset))
    
    tmp=offset_set  
  
  polygon=tmp
  return polygon     
Esempio n. 3
0
def generate_icosahedron(side_length):
    ''' Generate an icosahedron from the given side length and 
      return an array of 20 triangles component from the icosahedron 
      and his construction base quad set. '''

    side_lt = side_length  # Define the littler side of the rectangle.
    side_gt = side_length * (
        (1 + sqrt(5)) / 2.
    )  # Compute the length of the greater side with the gold number ( (1+sqrt(5))/2. )

    # Define the base quad so that his center is Vertex(0.0, 0.0, 0.0) :
    quad1 = [
        Vertex(-side_lt / 2., -side_gt / 2., 0.0),
        Vertex(side_lt / 2., -side_gt / 2., 0.0),
        Vertex(side_lt / 2., side_gt / 2., 0.0),
        Vertex(-side_lt / 2., side_gt / 2., 0.0)
    ]

    # Rotate the base quad on the y and z axes from 90°, to obtain the second crossing quad.
    quad2 = [
        rotate_z(rotate_y(quad1[0], 90), 90),
        rotate_z(rotate_y(quad1[1], 90), 90),
        rotate_z(rotate_y(quad1[2], 90), 90),
        rotate_z(rotate_y(quad1[3], 90), 90)
    ]

    # Rotate the base quad on the y and x axes from 90°, to obtain the third crossing quad.
    quad3 = [
        rotate_x(rotate_y(quad1[0], 90), 90),
        rotate_x(rotate_y(quad1[1], 90), 90),
        rotate_x(rotate_y(quad1[2], 90), 90),
        rotate_x(rotate_y(quad1[3], 90), 90)
    ]

    icosahedron_base_quads = [
        quad1, quad2, quad3
    ]  # We set the bases quads for our polyhedron: an icosahedron.

    # Define an array composed of triangles because an icosahedron is composed from 20 equilateral triangles:
    icosahedron_triangle_array = [
        (icosahedron_base_quads[0][0], icosahedron_base_quads[0][1],
         icosahedron_base_quads[2][1]),
        (icosahedron_base_quads[0][1], icosahedron_base_quads[1][0],
         icosahedron_base_quads[1][1]),
        (icosahedron_base_quads[0][0], icosahedron_base_quads[0][1],
         icosahedron_base_quads[2][2]),
        (icosahedron_base_quads[0][0], icosahedron_base_quads[1][2],
         icosahedron_base_quads[1][3]),
        (icosahedron_base_quads[0][0], icosahedron_base_quads[1][3],
         icosahedron_base_quads[2][1]),
        (icosahedron_base_quads[0][1], icosahedron_base_quads[2][1],
         icosahedron_base_quads[1][0]),
        (icosahedron_base_quads[0][1], icosahedron_base_quads[1][1],
         icosahedron_base_quads[2][2]),
        (icosahedron_base_quads[0][0], icosahedron_base_quads[2][2],
         icosahedron_base_quads[1][2]),
        (icosahedron_base_quads[0][2], icosahedron_base_quads[0][3],
         icosahedron_base_quads[2][3]),
        (icosahedron_base_quads[0][3], icosahedron_base_quads[1][2],
         icosahedron_base_quads[1][3]),
        (icosahedron_base_quads[0][2], icosahedron_base_quads[0][3],
         icosahedron_base_quads[2][0]),
        (icosahedron_base_quads[0][2], icosahedron_base_quads[1][0],
         icosahedron_base_quads[1][1]),
        (icosahedron_base_quads[0][2], icosahedron_base_quads[1][1],
         icosahedron_base_quads[2][3]),
        (icosahedron_base_quads[0][3], icosahedron_base_quads[2][3],
         icosahedron_base_quads[1][2]),
        (icosahedron_base_quads[0][3], icosahedron_base_quads[1][3],
         icosahedron_base_quads[2][0]),
        (icosahedron_base_quads[0][2], icosahedron_base_quads[2][0],
         icosahedron_base_quads[1][0]),
        (icosahedron_base_quads[2][0], icosahedron_base_quads[2][1],
         icosahedron_base_quads[1][3]),
        (icosahedron_base_quads[2][0], icosahedron_base_quads[2][1],
         icosahedron_base_quads[1][0]),
        (icosahedron_base_quads[2][2], icosahedron_base_quads[2][3],
         icosahedron_base_quads[1][1]),
        (icosahedron_base_quads[2][2], icosahedron_base_quads[2][3],
         icosahedron_base_quads[1][2])
    ]

    # We return the base quad set and the triangle array.
    return [quad1, quad2, quad3], icosahedron_triangle_array