Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit c9d4d74f5e08eff5bed1f8cef0e46e1c532df905 @dodo committed
Showing with 156 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +2 −0 .npmignore
  3. +12 −0 Cakefile
  4. +2 −0 cube.js
  5. +17 −0 package.json
  6. +50 −0 src/cube.coffee
  7. +20 −0 src/index.coffee
  8. +48 −0 src/vector.coffee
5 .gitignore
@@ -0,0 +1,5 @@
+*~
+*swp
+lib/*
+.lock-wscript
+node_modules/
2 .npmignore
@@ -0,0 +1,2 @@
+Cakefile
+src/
12 Cakefile
@@ -0,0 +1,12 @@
+path = require 'path'
+{ run, compileScript } = require 'muffin'
+
+task 'build', 'compile coffeescript → javascript', (options) ->
+ run
+ options:options
+ files:[
+ "./src/**/*.coffee"
+ ]
+ map:
+ 'src/(.+).coffee': (m) ->
+ compileScript m[0], path.join("lib" ,"#{m[1]}.js"), options
2 cube.js
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib/index')
17 package.json
@@ -0,0 +1,17 @@
+{ "name": "surrender-cube"
+, "description": "surrender to the cube!"
+, "version": "0.0.0"
+, "homepage": "https://github.com/dodo/node-surrender-cube"
+, "author": "dodo (https://github.com/dodo)"
+, "repository": {"type": "git", "url": "git://github.com/dodo/node-surrender-cube.git"}
+, "main": "cube.js"
+, "engines": {"node": ">= 0.4.x"}
+, "keywords": ["terminal", "wireframe", "cube", "loading", "indicator"]
+, "scripts": {
+ "prepublish": "cake build"}
+, "dependencies": {
+ "surrender": "*"}
+, "devDependencies": {
+ "muffin": ">= 0.2.6",
+ "coffee-script": ">= 1.2.0"}
+}
50 src/cube.coffee
@@ -0,0 +1,50 @@
+Vector = require './vector'
+
+
+# maps Vector to a 2dim list
+toList = (vec) ->
+ return [vec.x, vec.y]
+
+
+class Cube
+ constructor: (@ctx) ->
+ @vertices = [
+ new Vector(-1, -1, -1)
+ new Vector( 1, -1, -1)
+ new Vector(-1, 1, -1)
+ new Vector(-1, -1, 1)
+ new Vector(-1, 1, 1)
+ new Vector( 1, -1, 1)
+ new Vector( 1, 1, -1)
+ new Vector( 1, 1, 1)
+ ]
+
+ @edges = [[0,1], [1,5], [5,3], [3,0] # bottom
+ [2,6], [6,7], [7,4], [4,2] # top
+ [0,2], [1,6], [5,7], [3,4]]
+
+ map: (method, args...) ->
+ for vertice in @vertices
+ vertice[method](args...)
+ this
+
+ _rotate: (type, angle) ->
+ @map("rotate#{type}", angle)
+
+ rotateX: (angle) -> @_rotate('X', angle)
+ rotateY: (angle) -> @_rotate('Y', angle)
+ rotateZ: (angle) -> @_rotate('Z', angle)
+
+ scale: (value) ->
+ @map("mul", value)
+
+ render: () ->
+ @ctx.charm.reset()
+ for edge in @edges
+ @ctx.line(toList(@vertices[edge[0]].project(3, 3, 2, 6)),
+ toList(@vertices[edge[1]].project(3, 3, 2, 6)))
+
+# exports
+
+module.exports = Cube
+
20 src/index.coffee
@@ -0,0 +1,20 @@
+surrender = require 'surrender'
+Cube = require './cube'
+{PI, cos, sin} = Math
+
+cube = new Cube(surrender from:[ [ 0, 3 ], [ 0, 3 ] ])
+cube.scale(2)
+
+i = 0
+step = ->
+ rad = (i+1) * PI / 180
+ i = (++i)%360
+ cosa = cos(rad) * 10
+ sina = sin(rad) * 10
+
+ cube.rotateX(cosa)
+ cube.rotateY(sina)
+ cube.rotateZ(sina-cosa)
+ do cube.render
+setInterval(step, 100)
+
48 src/vector.coffee
@@ -0,0 +1,48 @@
+{PI, cos, sin} = Math
+
+class Vector
+ constructor: (x=0, y=0, z=0) ->
+ [@x, @y, @z] = [x, y, z]
+
+ rotateX: (angle) ->
+ rad = angle * PI / 180
+ cosa = cos(rad)
+ sina = sin(rad)
+ y = @y * cosa - @z * sina
+ z = @y * sina + @z * cosa
+ [@y, @z] = [y, z]
+ this
+
+ rotateY: (angle) ->
+ rad = angle * PI / 180
+ cosa = cos(rad)
+ sina = sin(rad)
+ z = @z * cosa - @x * sina
+ x = @z * sina + @x * cosa
+ [@x, @z] = [x, z]
+ this
+
+ rotateZ: (angle) ->
+ rad = angle * PI / 180
+ cosa = cos(rad)
+ sina = sin(rad)
+ x = @x * cosa - @y * sina
+ y = @x * sina + @y * cosa
+ [@x, @y] = [x, y]
+ this
+
+ project: (width, height, fov, viewer_distance) ->
+ factor = fov / (viewer_distance + @z)
+ x = @x * factor + width / 2
+ y = -@y * factor + height / 2
+ new Vector(x, y, 1)
+
+ mul: (value) ->
+ @x *= value
+ @y *= value
+ @z *= value
+ this
+
+# exports
+
+module.exports = Vector

0 comments on commit c9d4d74

Please sign in to comment.
Something went wrong with that request. Please try again.