Skip to content

Instantly share code, notes, and snippets.

@goatslacker
Created October 7, 2011 08:01
Show Gist options
  • Save goatslacker/1269738 to your computer and use it in GitHub Desktop.
Save goatslacker/1269738 to your computer and use it in GitHub Desktop.
creates an svg path for a spaceship based on Dave Bollinger's algorithm
# Based on Dave Bollinger`s work
class Spaceship
constructor: (@size = 5) ->
@ship = ( solid: [], cockpit: [], body: [] )
# map the ship
{ empty, solid, cockpit, body } = @map this
# turn on all solids first
solid.forEach((block) =>
@ship.solid.push block
)
# dynamically build cockpit and body
cockpit.forEach @__create "cockpit"
body.forEach @__create "body"
# Exports SVG Path
toSVG: ->
{ solid, cockpit, body } = @ship
svg = ""
toSVG = (block) =>
[x, y] = block
sx = x * @size
sy = y * @size
mx = ((6 * 2) - 1 - x) * @size
# base
svg += "M#{sx}, #{sy}L#{sx + 1}, #{sy + 1}"
# mirror
svg += "M#{mx}, #{sy}L#{mx + 1}, #{sy + 1}"
solid.forEach toSVG
cockpit.forEach toSVG
body.forEach toSVG
svg
map: ->
# blocks that are always empty
empty = [
[0, 0], [1, 0], [2, 0], [3, 0],
[0, 1], [1, 1], [2, 1],
[0, 2], [1, 2], [2, 2],
[0, 3], [1, 3],
[0, 4], [1, 4],
[0, 5]
]
# blocks that are always solid
solid = [
[5, 2], [5, 3], [5, 4], [5, 9]
]
# blocks that form the cockpit, can be 1/0
cockpit = [
[4, 5], [4, 6], [4, 7],
[5, 5], [5, 6], [5, 7]
]
# blocks that form the body, can be 1/0
body = [
[4, 1], [5, 1],
[4, 2],
[3, 3], [4, 3],
[3, 4], [4, 4],
[2, 5], [3, 5], [4, 5],
[1, 6], [2, 6], [3, 6],
[1, 7], [2, 7], [3, 7],
[1, 8], [2, 8], [3, 8],
[1, 9], [2, 9], [3, 9], [4, 9],
[3, 10], [4, 10], [5, 10]
]
# return the object
{ empty, solid, cockpit, body }
# our Random generator
isOn: ->
Math.round(Math.random()) is 1
# draw`s a piece of the spaceship
__create: (section) ->
# returns a function used by forEach when drawing
iterator = (block) =>
# turn on a block "randomly"
@ship[section].push block if @isOn()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment