Skip to content

Instantly share code, notes, and snippets.

@s-shin
Created February 22, 2014 14:34
Show Gist options
  • Select an option

  • Save s-shin/9155724 to your computer and use it in GitHub Desktop.

Select an option

Save s-shin/9155724 to your computer and use it in GitHub Desktop.
Simple 2d vector class. Public domain :)
class Vector2
@fromArray: (a) -> new Vector2 a[0], a[1]
constructor: (x, y) -> @set x, y
set: (x, y) -> @[0] = x; @[1] = y; @x = x; @y = y; @
clone: -> new Vector2 @x, @y
add: (v) -> @clone().iadd v
iadd: (v) -> @set @x+v.x, @y+v.y
sub: (v) -> @clone().isub v
isub: (v) -> @set @x-v.x, @y-v.y
muls: (s) -> @clone().imuls s
imuls: (s) -> @set @x*s, @y*s
len: -> Math.sqrt(@x*@x + @y*@y)
norm: -> @clone().inorm()
inorm: -> len = @len(); @set @x / len, @y / len; @
normal: -> @clone().inormal()
inormal: -> @set(@y, -@x).inorm()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment