0

I want to create a hexagonal grid using XYZ coordinates that is constructed in a spiraling pattern. This is my current code, which produces a grid depicted by the red arrows below. My problem area is circled. Rather than going from [-1,0,1] to [0,-2,2] I need to move from [-1,0,1] to [-1,-1,2] (following the blue line).

spiral

The complete code appears below the hash line- I am creating the visualization in Blender 2.65a

radius = 11 # determines size of field
deltas = [[1,0,-1],[0,1,-1],[-1,1,0],[-1,0,1],[0,-1,1],[1,-1,0]]
hex_coords = []
for r in range(radius):
    x = 0
    y = -r
    z = +r
    points = x,y,z
    hex_coords.append(points)
    for j in range(6):
        if j==5:
            num_of_hexas_in_edge = r-1
        else:
            num_of_hexas_in_edge = r
        for i in range(num_of_hexas_in_edge):
            x = x+deltas[j][0]
            y = y+deltas[j][1]
            z = z+deltas[j][2] 
            plot = x,y,z           
            hex_coords.append(plot)

-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-

import bpy


FOXP2 = '''
CTTGAACCTTTGTCACCCCTCACGTTGCACACCAAAGACATACCCTAGTGATTAAATGCTGATTTTGTGT
ACGATTGTCCACGGACGCCAAAACAATCACAGAGCTGCTTGATTTGTTTTAATTACCAGCACAAAATGCC
CAATTCCTCCTCGACTACCTCCTCCAACACTTCCAAAGCATCACCACCAATAACTCATCATTCCATAGTG
AATGGACAGTCTTCAGTTCTAAGTGCAAGACGAGACAGCTCGTCACATGAGGAGACTGGGGCCTCTCACA
CTCTCTATGGCCATGGAGTTTGCAAATGGCCAGGCTGTGAAAGCATTTGTGAAGATTTTGGACAGTTTTT
AAAGCACCTTAACAATGAACACGCATTGGATGACCGAAGCACTGCTCAGTGTCGAGTGCAAATGCAGGTG
GTGCAACAGTTAGAAATACAGCTTTCTAAAGAACGCGAACGTCTTCAAGCAATGATGACCCACTTGCACA

'''

set_size = len(FOXP2)

def makeMaterial(name, diffuse, specular, alpha):
    mat = bpy.data.materials.new(name)
    mat.diffuse_color = diffuse
    mat.diffuse_shader = 'LAMBERT' 
    mat.diffuse_intensity = 1.0 
    mat.specular_color = specular
    mat.specular_shader = 'COOKTORR'
    mat.specular_intensity = 0.5
    mat.alpha = alpha
    mat.ambient = 1
    return mat

def setMaterial(ob, mat):
    me = ob.data
    me.materials.append(mat)

    # Create four materials
red = makeMaterial('Red', (1,0,0), (0,0,0), .5)
blue = makeMaterial('BlueSemi', (0,0,1), (0,0,0), 0.5)
green = makeMaterial('Green',(0,1,0), (0,0,0), 0.5)
yellow = makeMaterial('Yellow',(1,1,0), (0,0,0), 0.5)
black = makeMaterial('Black',(0,0,0), (0,0,0), 0.5)
white = makeMaterial('White',(1,1,1), (0,0,0), 0.5)    

def make_sphere(volume, position):
    create = bpy.ops.mesh.primitive_uv_sphere_add
    create(size=volume, location=position)


    # Builds a list of coordinate points
radius = 11
deltas = [[1,0,-1],[0,1,-1],[-1,1,0],[-1,0,1],[0,-1,1],[1,-1,0]]
hex_coords = []
for r in range(radius):
    x = 0
    y = -r
    z = +r
    points = x,y,z
    hex_coords.append(points)
    for j in range(6):
        if j==5:
            num_of_hexas_in_edge = r-1
        else:
            num_of_hexas_in_edge = r
        for i in range(num_of_hexas_in_edge):
            x = x+deltas[j][0]
            y = y+deltas[j][1]
            z = z+deltas[j][2] 
            plot = x,y,z           
            hex_coords.append(plot)   

    # Color-codes sequence and appends to color_array

color_array = []

for x in FOXP2:
    if x == 'A':
        color_array.append(1)
    elif x == 'T':
        color_array.append(1)
    elif x == 'C':
        color_array.append(0)
    elif x == 'G':
        color_array.append(0)
    else:
        pass

    # Pulls from sequence data and applies color to sphere
    # Positions sphere to coordinates     
    # Pulls from color_code and applies color scheme to sphere object 

for x in color_array: 
    if x =='RED':
        coord_tuple = hex_coords.pop(0) 
        make_sphere(1, coord_tuple)
        setMaterial(bpy.context.object, red)

    elif x =='GREEN':
        coord_tuple = hex_coords.pop(0)
        make_sphere(1, coord_tuple)
        setMaterial(bpy.context.object, green)

    elif x =='BLUE':
        coord_tuple = hex_coords.pop(0)
        make_sphere(1, coord_tuple)
        setMaterial(bpy.context.object, blue)

    elif x =='YELLOW':
        coord_tuple = hex_coords.pop(0)
        make_sphere(1, coord_tuple)
        setMaterial(bpy.context.object, yellow)

    else:
        pass
Benjamin James
  • 941
  • 1
  • 9
  • 24
  • Any reason why you are using that particular way of representing coordinates? It seems to me that choosing the axis lines go through the vertexes instead of the center of the sides of the (0,0,0) hex complicates a little these operations. Check the **cube coordinates** section on http://www.redblobgames.com/grids/hexagons/ – Chubas Aug 22 '14 at 16:16
  • I simply went with what I knew to get the spiraling effect, also i failed at coding up the implementation you describe. I would be interested to see your version. At this point what I have works in terms of symmetry in Blender. – Benjamin James Aug 22 '14 at 18:14
  • You might also be interested in http://blender.stackexchange.com/questions – stacker Sep 04 '14 at 11:33

0 Answers0