fieldkit
Version:
Basic building blocks for computational design projects. Written in CoffeeScript for browser and server environments.
188 lines (138 loc) • 4.32 kB
text/coffeescript
#
# Vector 2D
#
class Vec2
x: 0
y: 0
constructor: (=0, =0) ->
set: (v) -> = v.x; = v.y; this
set2: (x, y) -> = x; = y; this
zero: -> = = 0; this
add: (v) -> += v.x; += v.y; this
add_: (v) -> new Vec2( + v.x, + v.y)
sub: (v) -> -= v.x; -= v.y; this
sub_: (v) -> new Vec2( - v.x, - v.y)
mul: (v) -> *= v.x; *= v.y; this
mul_: (v) -> new Vec2( * v.x, * v.y)
div: (v) -> /= v.x; /= v.y; this
div_: (v) -> new Vec2( / v.x, / v.y)
scale: (value) -> *= value; *= value; this
scale_: (value) -> new Vec2( * value, * value)
length: -> Math.sqrt * + *
lengthSquared: -> * + *
normalize: ->
l =
unless l is 0
/= l
/= l
this
normalize_: -> .normalize()
normalizeTo: (length) ->
magnitude = Math.sqrt( * + * )
if magnitude > 0
magnitude = length / magnitude
*= magnitude
*= magnitude
this
normalizeTo_: (length) -> .normalizeTo_(length)
distance: (v) -> Math.sqrt
distanceSquared: (v) -> v.x, v.y
distanceSquared2: (x, y) ->
dx = - x
dy = - y
dx * dx + dy * dy
dot: (v) -> * v.x + * v.y
rotate: (angle) ->
sina = Math.sin(angle)
cosa = Math.cos(angle)
rx = * cosa - * sina
ry = * sina + * cosa
= rx
= ry
jitter: (amount) ->
+= (Math.random() * 2 - 1) * amount
+= (Math.random() * 2 - 1) * amount
this
jitter_: (amount) -> (new Vec2(, )).jitter amount
lerp: (target, delta) ->
= * (1 - delta) + target.x * delta
= * (1 - delta) + target.y * delta
this
lerp_: (target, delta) ->
(new Vec2(, )).lerp target, delta
equals: (v) -> is v.x and is v.y
clone: -> new Vec2(, )
toString: -> "Vec2(#{@x}, #{@y})"
#
# Vector 3D
#
class Vec3
x: 0
y: 0
z: 0
constructor: (=0, =0, =0) ->
set: (v) -> = v.x; = v.y; = v.z; this
set3: (x, y, z) -> = x; = y; = z; this
zero: -> = = = 0; this
add: (v) -> += v.x; += v.y; += v.z; this
add_: (v) -> new Vec3( + v.x, + v.y, + v.z)
sub: (v) -> -= v.x; -= v.y; -= v.z; this
sub_: (v) -> new Vec3( - v.x, - v.y, - v.z)
mul: (v) -> *= v.x; *= v.y; this
mul_: (v) -> new Vec3( * v.x, * v.y, * v.z)
div: (v) -> /= v.x; /= v.y; /= v.z; this
div_: (v) -> new Vec3( / v.x, / v.y, = v.z)
scale: (value) -> *= value; *= value; *= value; this
scale_: (value) -> new Vec3( * value, * value, * value)
length: -> Math.sqrt * + * + *
lengthSquared: -> * + * + *
normalize: ->
l =
unless l is 0
/= l
/= l
/= l
this
normalize_: -> .normalize()
normalizeTo: (length) ->
magnitude = Math.sqrt( * + * + * )
if magnitude > 0
magnitude = length / magnitude
*= magnitude
*= magnitude
this
normalizeTo_: (length) -> .normalizeTo_(length)
distance: (v) -> Math.sqrt
distanceSquared: (v) -> v.x, v.y, v.z
distanceSquared3: (x, y, z) ->
dx = - x
dy = - y
dz = - z
dx * dx + dy * dy + dz * dz
dot: (v) -> * v.x + * v.y + * v.z
# rotate: (angle) ->
# sina = Math.sin(angle)
# cosa = Math.cos(angle)
# rx = * cosa - * sina
# ry = * sina + * cosa
# = rx
# = ry
jitter: (amount) ->
+= (Math.random() * 2 - 1) * amount
+= (Math.random() * 2 - 1) * amount
+= (Math.random() * 2 - 1) * amount
this
jitter_: (amount) -> (new Vec3(, , )).jitter amount
lerp: (target, delta) ->
= * (1 - delta) + target.x * delta
= * (1 - delta) + target.y * delta
= * (1 - delta) + target.z * delta
this
lerp_: (target, delta) ->
(new Vec3(, , )).lerp target, delta
equals: (v) -> is v.x and is v.y and is v.z
clone: -> new Vec3(, , )
toString: -> "Vec3(#{@x}, #{@y}, #{@z})"
module.exports =
Vec2: Vec2
Vec3: Vec3