Our recent ring lessons have prepared us for creating a lot of fun structures. In this lesson, we'll produce the following helix structure with a very small modification to our original code.
For a refresher, let's start with the original ring code:
As position moves from 0
to 19
we calculate the x
and y
coordinates of each of the positions and place a cube there. But you'll notice
that we always leave the z
coordinate to 0
. What if we advanced the
z
coordinate as well? Perhaps we should see what happens if we set the z
coordinate of the cubes to position
, so it advances in a consistent
direction as we go around the ring.
After hitting Run, you'll have to zoom out a bit to see the structure you've
created. Note that the only change we've made to create this basic helix shape
is to replace place(x, y, 0)
with place(x, y, position)
.
The helix we've created does seem a bit short. To appreciate it more, we're
going to need to increase its length. What if we didn't stop the loop when it
reached SEGMENTS
, but kept it going for one more round? We could just
replace the loop to go on for twice as long:
There's another problem with this helix: it's not centered around the 0, 0, 0
coordinate. The reason for this is that the z
coordinate of the helix
starts at 0
and goes on till 2*SEGMENTS
. Instead, if it started at
-SEGMENTS
and went on till +SEGMENTS
, it would end up being centered
around 0, 0, 0
:
Notice that I pulled the z
coordinate into its own variable. As your code
begins to get more complex, it's reasonable to modify it a little bit to keep it
readable. The process of cleaning up code to keep it easy to understand is
called "refactoring" and it's recommended that you do it from time to time.
You may have read about DNA being a "double helix". What that means is that 2 helixes are running side-by-side as shown in the animated gif below. Play around with the code above to see if you could get a second helix running side-by-side with the first one. We'll look at the solution in the next lesson.
© 2024 Abhin Chhabra | Sitemap