So I got bored last night and decided to create a double helix! Here’s the code.

The first thing I did was imported all the modules I needed and start the connection to the server.:

import math, block, random
import minecraft as minecraft

mc = minecraft.Minecraft.create()
mc.postToChat("Welcome to the Double Helix Creator")

The Maths

Creating the helix is pretty simple, though we have to use a bit of maths – Cos and Sin to be exact!

I was never a big fan of maths at school but I realised very quickly when I started coding that:

  1. You aren’t going to get very far if you avoid maths when coding.
  2. You don’t always need to know exactly why or how it works, you just need to know enough to be able to use it.

The main bit of maths we need today is the maths that finds a point on a circle for a given angle at a given distance from the center.

xcoord = centre of circle X coord + distance from center * cos (angle * (math.pi /180))

ycoord = centre of circle Y coord + distance from center * sin (angle * (math.pi /180))

This will allow use to find any point on the circle.

In order to make life easier I put this calculation in a function:

def getCoords(radius,angle): #Finds the correct z,y coords depending on the angle and radius
    y = radius * math.cos(angle * math.pi / 180.0) + CENTERY;
    z = radius * math.sin(angle * math.pi / 180.0) + CENTERZ;
    return y,z

Hang on, Why aren’t we calculating X and Y like above?

Well we are, sort of, but in the MineCraft world I was my spiral to go along into the world, not up to the sky, so I’ve used ZY instead of XY.

I then set up some constants and set the start point (x) of the helix.

#The center point on a circle is known as its origin
ORIGINY = 20
ORIGINZ = 20

RADIUS = 15

I then set up a for loop to give me 4 twirls (enough for demo purposes) and then set up the loop for each angle in each revolution (0-360).

 

for repetitions in range(0,5):
    #Draw the twirls
    for angle in range(0,365): 

I then started creating the spiral.

for repetitions in range(0,5):
    #Draw the twirls
    for angle in range(0,365): 
        y,z =  getCoords(RADIUS,angle)
        mc.setBlocks(x-2,y,z,x+2,y,z,block.WOOL.id,1) #Draw the first twirl
        #Calculate the angle opposite angle to draw the other twirl.
        angle2 = angle + 180
        if angle2 >= 360: 
            angle2 -=360
        y,z =  getCoords(RADIUS,angle2)
        mc.setBlocks(x-2,y,z,x+2,y,z,block.WOOL.id,10) #Draw the first twirl
        x-=0.2
        
        if angle%36 == 0: #Draw the joining bits every 10th of a revolution
            mc.setBlocks(x-1,ORIGINY-1,ORIGINY-1,x+1,ORIGINY+1,ORIGINZ+1,block.WOOL.id,0)
            colour1 = random.randint(0,13)
            colour2 = random.randint(0,13) # Change this to glass maybe?
            for point in range(0,RADIUS):
                y,z  =  getCoords(point,angle)
                mc.setBlocks(x,y,z,x+1,y,z,block.WOOL.id,colour1)
                #Calculate the angle opposite angle to draw the line
                angle2 = angle + 180
                if angle2 >= 360: angle2 -=360
                y,z =getCoords(point,angle2)
                mc.setBlocks(x,y,z,x+1,y,z,block.WOOL.id,colour2)

Apparently there are approximately 10 cross pieces for each revolution and so I needed a way of generating a stick every 36 degrees or a revolution. For this I used modulus:

if angle%36 == 0: #Draw the joining bits every 10th of a revolution

Modulus will only equal zero ( and so trigger the condition of the if statement) when the remainder of the angle divided by 36 is zero – so it will happen 10 times in a revolution 36Deg, 72, etc…

I also needed a way to reverse the angle so that the opposite twirl and cross piece could be drawn. My brain had given up on me by this point so I just used this simple hacky calculation!

                angle2 = angle + 180
                if angle2 >= 360: angle2 -=360

And there you go!

Here is the full code complete:

#The Double Helix Creator
#www.ninjagecko.co.uk

import math, block, random
import minecraft as minecraft

mc = minecraft.Minecraft.create()
mc.postToChat("Welcome to the Double Helix Creator")

def getCoords(radius,angle): #Finds the correct z,y coords depending on the angle and radius
    y = radius * math.cos(angle * math.pi / 180.0) + ORIGINY;
    z = radius * math.sin(angle * math.pi / 180.0) + ORIGINZ;
    return y,z


#The center point on a circle is known as its origin
ORIGINY = 20
ORIGINZ = 20

RADIUS = 15

x = 0.0 #Where to start drawing the helix

#Clear some space
mc.setBlocks(-1000,0,0,100,60,1000,block.AIR)

for repetitions in range(0,5):
    #Draw the twirls
    for angle in range(0,365): 
        y,z =  getCoords(RADIUS,angle)
        mc.setBlocks(x-2,y,z,x+2,y,z,block.WOOL.id,1) #Draw the first twirl
        #Calculate the angle opposite angle to draw the other twirl.
        angle2 = angle + 180
        if angle2 >= 360: 
            angle2 -=360
        y,z =  getCoords(RADIUS,angle2)
        mc.setBlocks(x-2,y,z,x+2,y,z,block.WOOL.id,10) #Draw the first twirl
        x-=0.2
        
        if angle%36 == 0: #Draw the joining bits every 10th of a revolution
            mc.setBlocks(x-1,ORIGINY-1,ORIGINY-1,x+1,ORIGINY+1,ORIGINZ+1,block.WOOL.id,0)
            colour1 = random.randint(0,13)
            colour2 = random.randint(0,13) # Change this to glass maybe?
            for point in range(0,RADIUS):
                y,z  =  getCoords(point,angle)
                mc.setBlocks(x,y,z,x+1,y,z,block.WOOL.id,colour1)
                #Calculate the angle opposite angle to draw the line
                angle2 = angle + 180
                if angle2 >= 360: angle2 -=360
                y,z =getCoords(point,angle2)
                mc.setBlocks(x,y,z,x+1,y,z,block.WOOL.id,colour2)