My Profile Photo

Baggerspion


Paul Adams' Wanderings Around Engineering, Free Software and Berlin


Starfields And Coding Challenges

Recently I have become addicted to the coding challenges of Daniel Shiffman on YouTube. The challenges are fun and his presenting style is something to behold: just-under-control chaos. I highly recomend them. Daniel is part of the Processing Foundation and a huge advocate for the convergence of art and software.

The Starfield Challenge

Daniel’s first challenge was to create a starfield simulator within 10 minutes, using Processing. Daniel uses Java for the purposes of the challenge and the video is inlcuded below:

Simple enough: create a simulation of a first-person perspective of travelling through stars. I decided to give this challenge a go since my programming is more than a little rusty.

Just in Python, rather than Java.

The problem with Daniel’s videos is that (for good or for bad) he sees them through to the bitter end. So, whilst very entertaining, you are presented the solution on a platter. IMO it would be better if there was a video to introduce the problem (and perhaps provide some hints for the approach) and a separate video with the solution.

So I followed Daniel’s video and Java programming and came up with my own Python solution. Needless to say, it should be very familiar to anyone who watched the complete video:

stars = []

def setup():
    global stars
    
    size(800, 800)
    stars = [Star() for x in range(800)]
    
def draw():
    background(0)
    translate(width / 2, height / 2)
    
    for star in stars:
        star.update()
        star.show()
        
class Star(object):
    def __init__(self):
        self.x = random(-width, width)
        self.y = random(-height, height)
        self.z = random(width)
        self.pz = self.z
        
    def update(self):
        self.z -= 10
        if self.z < 1:
            self.__init__()
                
    def show(self):
        fill(255)
        noStroke()
        
        sx = map(self.x / self.z, 0, 1, 0, width)
        sy = map(self.y / self.z, 0, 1, 0, height)

        r = map(self.z, 0, width, 16, 0)        
        ellipse(sx, sy, r, r)
        
        px = map(self.x / self.pz, 0, 1, 0, width);
        py = map(self.y / self.pz, 0, 1, 0, height);
        
        self.pz = self.z
        
        stroke(255)
        line(px, py, sx, sy)

On Processing

Processing is fun to use and simple enough to learn. Used carefully, it could be a decent tool for helping to learn a new language altogether. It is certainly a great tool for producing cool visualisations.

If you are taking your first steps into data visualisation, you should really look no further. If you are taking your first steps in programming, it is a fun place to start but perhaps not the best.

Either way, I highly recommend downloading it and playing along with Daniel’s awesomely hyperactive videos!



Comments