class Vector2

class Vector2

lib/geometry/vector2.tya:2

Vector2 provides the geometry/Vector2 standard library API.

Source
# Vector2 provides the geometry/Vector2 standard library API.
class Vector2
  # Vector2.x stores instance state.
  # @type Int
  x: 0

  # Vector2.y stores instance state.
  # @type Int
  y: 0

  # Vector2.initialize provides the geometry/Vector2 standard library operation.
  # @param x Int x value.
  # @param y Int y value.
  # @return Self the initialized object.
  initialize: x = 0, y = 0 ->
    self.x = self.number(x, "x")
    self.y = self.number(y, "y")

  # Vector2.abs provides the geometry/Vector2 standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  abs: value ->
    if value < 0
      return 0 - value
    value

  # Vector2.add provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @return Any the resulting value.
  add: a, b = nil ->
    if b == nil
      b = a
      a = self
    self.expect(a, "a")
    self.expect(b, "b")
    Vector2(a.x + b.x, a.y + b.y)

  # Vector2.clamp provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @param min_v Any min v value.
  # @param max_v Any max v value.
  # @return Any the resulting value.
  clamp: v, min_v, max_v ->
    self.expect(v, "v")
    self.expect(min_v, "min_v")
    self.expect(max_v, "max_v")
    Vector2(
      self.clamp_number(v.x, min_v.x, max_v.x),
      self.clamp_number(v.y, min_v.y, max_v.y)
    )

  # Vector2.clamp_number provides the geometry/Vector2 standard library operation.
  # @param value String value value.
  # @param lo Any lo value.
  # @param hi Any hi value.
  # @return Any the resulting value.
  clamp_number: value, lo, hi ->
    if value < lo
      return lo
    if value > hi
      return hi
    value

  # Vector2.distance provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @return Any the resulting value.
  distance: a, b = nil ->
    if b == nil
      b = a
      a = self
    math_sqrt(self.distance_squared(a, b))

  # Vector2.distance_squared provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @return Any the resulting value.
  distance_squared: a, b = nil ->
    if b == nil
      b = a
      a = self
    self.length_squared(self.sub(a, b))

  # Vector2.div provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @param k Any k value.
  # @return Any the resulting value.
  div: v, k = nil ->
    if k == nil
      k = v
      v = self
    self.expect(v, "v")
    self.number(k, "k")
    if k == 0
      raise error("geometry.Vector2.div: divide by zero")
    Vector2(v.x / k, v.y / k)

  # Vector2.dot provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @return Any the resulting value.
  dot: a, b = nil ->
    if b == nil
      b = a
      a = self
    self.expect(a, "a")
    self.expect(b, "b")
    a.x * b.x + a.y * b.y

  # Vector2.equal? provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @return Boolean whether the condition is true.
  equal?: a, b = nil ->
    if b == nil
      b = a
      a = self
    a.class == Vector2 and b.class == Vector2 and a.x == b.x and a.y == b.y

  # Vector2.expect provides the geometry/Vector2 standard library operation.
  # @param value String value value.
  # @param name String name value.
  # @return Any the resulting value.
  expect: value, name ->
    if value.class != Vector2
      raise error("geometry.Vector2: " + name + " must be a Vector2")

  # Vector2.from_array provides the geometry/Vector2 standard library operation.
  # @param values Array values value.
  # @return Array the resulting value.
  from_array: values ->
    if values.class != Array or values.len() != 2
      raise error("geometry.Vector2.from_array: expected two-number array")
    Vector2(values[0], values[1])

  # Vector2.length provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @return Any the resulting value.
  length: v = nil ->
    if v == nil
      v = self
    math_sqrt(self.length_squared(v))

  # Vector2.length_squared provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @return Any the resulting value.
  length_squared: v = nil ->
    if v == nil
      v = self
    self.expect(v, "v")
    self.dot(v, v)

  # Vector2.lerp provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @param t Any t value.
  # @return Any the resulting value.
  lerp: a, b, t ->
    self.expect(a, "a")
    self.expect(b, "b")
    self.number(t, "t")
    Vector2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t)

  # Vector2.nearly_equal? provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @param epsilon Any epsilon value.
  # @return Boolean whether the condition is true.
  nearly_equal?: a, b, epsilon = nil ->
    if epsilon == nil
      epsilon = b
      b = a
      a = self
    self.expect(a, "a")
    self.expect(b, "b")
    if epsilon < 0
      raise error("geometry.Vector2.nearly_equal?: epsilon must be non-negative")
    self.abs(a.x - b.x) <= epsilon and self.abs(a.y - b.y) <= epsilon

  # Vector2.new provides the geometry/Vector2 standard library operation.
  # @param x Int x value.
  # @param y Int y value.
  # @return Self the resulting value.
  new: x, y ->
    Vector2(x, y)

  # Vector2.normalize provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @return String the resulting value.
  normalize: v = nil ->
    if v == nil
      v = self
    geometry_len = self.length(v)
    if geometry_len == 0
      return self.zero()
    self.div(v, geometry_len)

  # Vector2.number provides the geometry/Vector2 standard library operation.
  # @param value String value value.
  # @param name String name value.
  # @return Any the resulting value.
  number: value, name ->
    if value.class != Number
      raise error("geometry.Vector2: " + name + " must be a number")
    value

  # Vector2.one provides the geometry/Vector2 standard library operation.
  # @return Any the resulting value.
  one: ->
    Vector2(1, 1)

  # Vector2.scale provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @param k Any k value.
  # @return Any the resulting value.
  scale: v, k = nil ->
    if k == nil
      k = v
      v = self
    self.expect(v, "v")
    self.number(k, "k")
    Vector2(v.x * k, v.y * k)

  # Vector2.sub provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @return Any the resulting value.
  sub: a, b = nil ->
    if b == nil
      b = a
      a = self
    self.expect(a, "a")
    self.expect(b, "b")
    Vector2(a.x - b.x, a.y - b.y)

  # Vector2.to_array provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @return Array the resulting value.
  to_array: v = nil ->
    if v == nil
      v = self
    self.expect(v, "v")
    [v.x, v.y]

  # Vector2.zero provides the geometry/Vector2 standard library operation.
  # @return Any the resulting value.
  zero: ->
    Vector2(0, 0)

Instance Variables

x

Vector2.x

lib/geometry/vector2.tya:5

Vector2.x stores instance state.

Source
  # Vector2.x stores instance state.
  # @type Int
  x: 0

y

Vector2.y

lib/geometry/vector2.tya:9

Vector2.y stores instance state.

Source
  # Vector2.y stores instance state.
  # @type Int
  y: 0

Methods

abs

Vector2.abs(value)

lib/geometry/vector2.tya:22

Vector2.abs provides the geometry/Vector2 standard library operation.

Source
  # Vector2.abs provides the geometry/Vector2 standard library operation.
  # @param value String value value.
  # @return Any the resulting value.
  abs: value ->
    if value < 0
      return 0 - value
    value

add

Vector2.add(a, b = nil)

lib/geometry/vector2.tya:31

Vector2.add provides the geometry/Vector2 standard library operation.

Source
  # Vector2.add provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @return Any the resulting value.
  add: a, b = nil ->
    if b == nil
      b = a
      a = self
    self.expect(a, "a")
    self.expect(b, "b")
    Vector2(a.x + b.x, a.y + b.y)

clamp

Vector2.clamp(v, min_v, max_v)

lib/geometry/vector2.tya:44

Vector2.clamp provides the geometry/Vector2 standard library operation.

Source
  # Vector2.clamp provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @param min_v Any min v value.
  # @param max_v Any max v value.
  # @return Any the resulting value.
  clamp: v, min_v, max_v ->
    self.expect(v, "v")
    self.expect(min_v, "min_v")
    self.expect(max_v, "max_v")
    Vector2(
      self.clamp_number(v.x, min_v.x, max_v.x),
      self.clamp_number(v.y, min_v.y, max_v.y)
    )

clamp_number

Vector2.clamp_number(value, lo, hi)

lib/geometry/vector2.tya:58

Vector2.clamp_number provides the geometry/Vector2 standard library operation.

Source
  # Vector2.clamp_number provides the geometry/Vector2 standard library operation.
  # @param value String value value.
  # @param lo Any lo value.
  # @param hi Any hi value.
  # @return Any the resulting value.
  clamp_number: value, lo, hi ->
    if value < lo
      return lo
    if value > hi
      return hi
    value

distance

Vector2.distance(a, b = nil)

lib/geometry/vector2.tya:69

Vector2.distance provides the geometry/Vector2 standard library operation.

Source
  # Vector2.distance provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @return Any the resulting value.
  distance: a, b = nil ->
    if b == nil
      b = a
      a = self
    math_sqrt(self.distance_squared(a, b))

distance_squared

Vector2.distance_squared(a, b = nil)

lib/geometry/vector2.tya:79

Vector2.distance_squared provides the geometry/Vector2 standard library operation.

Source
  # Vector2.distance_squared provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @return Any the resulting value.
  distance_squared: a, b = nil ->
    if b == nil
      b = a
      a = self
    self.length_squared(self.sub(a, b))

div

Vector2.div(v, k = nil)

lib/geometry/vector2.tya:89

Vector2.div provides the geometry/Vector2 standard library operation.

Source
  # Vector2.div provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @param k Any k value.
  # @return Any the resulting value.
  div: v, k = nil ->
    if k == nil
      k = v
      v = self
    self.expect(v, "v")
    self.number(k, "k")
    if k == 0
      raise error("geometry.Vector2.div: divide by zero")
    Vector2(v.x / k, v.y / k)

dot

Vector2.dot(a, b = nil)

lib/geometry/vector2.tya:103

Vector2.dot provides the geometry/Vector2 standard library operation.

Source
  # Vector2.dot provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @return Any the resulting value.
  dot: a, b = nil ->
    if b == nil
      b = a
      a = self
    self.expect(a, "a")
    self.expect(b, "b")
    a.x * b.x + a.y * b.y

equal?

Vector2.equal?(a, b = nil)

lib/geometry/vector2.tya:115

Vector2.equal? provides the geometry/Vector2 standard library operation.

Source
  # Vector2.equal? provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @return Boolean whether the condition is true.
  equal?: a, b = nil ->
    if b == nil
      b = a
      a = self
    a.class == Vector2 and b.class == Vector2 and a.x == b.x and a.y == b.y

expect

Vector2.expect(value, name)

lib/geometry/vector2.tya:125

Vector2.expect provides the geometry/Vector2 standard library operation.

Source
  # Vector2.expect provides the geometry/Vector2 standard library operation.
  # @param value String value value.
  # @param name String name value.
  # @return Any the resulting value.
  expect: value, name ->
    if value.class != Vector2
      raise error("geometry.Vector2: " + name + " must be a Vector2")

from_array

Vector2.from_array(values)

lib/geometry/vector2.tya:132

Vector2.from_array provides the geometry/Vector2 standard library operation.

Source
  # Vector2.from_array provides the geometry/Vector2 standard library operation.
  # @param values Array values value.
  # @return Array the resulting value.
  from_array: values ->
    if values.class != Array or values.len() != 2
      raise error("geometry.Vector2.from_array: expected two-number array")
    Vector2(values[0], values[1])

initialize

Vector2.initialize(x = 0, y = 0)

lib/geometry/vector2.tya:15

Vector2.initialize provides the geometry/Vector2 standard library operation.

Source
  # Vector2.initialize provides the geometry/Vector2 standard library operation.
  # @param x Int x value.
  # @param y Int y value.
  # @return Self the initialized object.
  initialize: x = 0, y = 0 ->
    self.x = self.number(x, "x")
    self.y = self.number(y, "y")

length

Vector2.length(v = nil)

lib/geometry/vector2.tya:140

Vector2.length provides the geometry/Vector2 standard library operation.

Source
  # Vector2.length provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @return Any the resulting value.
  length: v = nil ->
    if v == nil
      v = self
    math_sqrt(self.length_squared(v))

length_squared

Vector2.length_squared(v = nil)

lib/geometry/vector2.tya:148

Vector2.length_squared provides the geometry/Vector2 standard library operation.

Source
  # Vector2.length_squared provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @return Any the resulting value.
  length_squared: v = nil ->
    if v == nil
      v = self
    self.expect(v, "v")
    self.dot(v, v)

lerp

Vector2.lerp(a, b, t)

lib/geometry/vector2.tya:159

Vector2.lerp provides the geometry/Vector2 standard library operation.

Source
  # Vector2.lerp provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @param t Any t value.
  # @return Any the resulting value.
  lerp: a, b, t ->
    self.expect(a, "a")
    self.expect(b, "b")
    self.number(t, "t")
    Vector2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t)

nearly_equal?

Vector2.nearly_equal?(a, b, epsilon = nil)

lib/geometry/vector2.tya:170

Vector2.nearly_equal? provides the geometry/Vector2 standard library operation.

Source
  # Vector2.nearly_equal? provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @param epsilon Any epsilon value.
  # @return Boolean whether the condition is true.
  nearly_equal?: a, b, epsilon = nil ->
    if epsilon == nil
      epsilon = b
      b = a
      a = self
    self.expect(a, "a")
    self.expect(b, "b")
    if epsilon < 0
      raise error("geometry.Vector2.nearly_equal?: epsilon must be non-negative")
    self.abs(a.x - b.x) <= epsilon and self.abs(a.y - b.y) <= epsilon

new

Vector2.new(x, y)

lib/geometry/vector2.tya:185

Vector2.new provides the geometry/Vector2 standard library operation.

Source
  # Vector2.new provides the geometry/Vector2 standard library operation.
  # @param x Int x value.
  # @param y Int y value.
  # @return Self the resulting value.
  new: x, y ->
    Vector2(x, y)

normalize

Vector2.normalize(v = nil)

lib/geometry/vector2.tya:191

Vector2.normalize provides the geometry/Vector2 standard library operation.

Source
  # Vector2.normalize provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @return String the resulting value.
  normalize: v = nil ->
    if v == nil
      v = self
    geometry_len = self.length(v)
    if geometry_len == 0
      return self.zero()
    self.div(v, geometry_len)

number

Vector2.number(value, name)

lib/geometry/vector2.tya:203

Vector2.number provides the geometry/Vector2 standard library operation.

Source
  # Vector2.number provides the geometry/Vector2 standard library operation.
  # @param value String value value.
  # @param name String name value.
  # @return Any the resulting value.
  number: value, name ->
    if value.class != Number
      raise error("geometry.Vector2: " + name + " must be a number")
    value

one

Vector2.one()

lib/geometry/vector2.tya:210

Vector2.one provides the geometry/Vector2 standard library operation.

Source
  # Vector2.one provides the geometry/Vector2 standard library operation.
  # @return Any the resulting value.
  one: ->
    Vector2(1, 1)

scale

Vector2.scale(v, k = nil)

lib/geometry/vector2.tya:217

Vector2.scale provides the geometry/Vector2 standard library operation.

Source
  # Vector2.scale provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @param k Any k value.
  # @return Any the resulting value.
  scale: v, k = nil ->
    if k == nil
      k = v
      v = self
    self.expect(v, "v")
    self.number(k, "k")
    Vector2(v.x * k, v.y * k)

sub

Vector2.sub(a, b = nil)

lib/geometry/vector2.tya:229

Vector2.sub provides the geometry/Vector2 standard library operation.

Source
  # Vector2.sub provides the geometry/Vector2 standard library operation.
  # @param a Int a value.
  # @param b Int b value.
  # @return Any the resulting value.
  sub: a, b = nil ->
    if b == nil
      b = a
      a = self
    self.expect(a, "a")
    self.expect(b, "b")
    Vector2(a.x - b.x, a.y - b.y)

to_array

Vector2.to_array(v = nil)

lib/geometry/vector2.tya:240

Vector2.to_array provides the geometry/Vector2 standard library operation.

Source
  # Vector2.to_array provides the geometry/Vector2 standard library operation.
  # @param v Any v value.
  # @return Array the resulting value.
  to_array: v = nil ->
    if v == nil
      v = self
    self.expect(v, "v")
    [v.x, v.y]

zero

Vector2.zero()

lib/geometry/vector2.tya:248

Vector2.zero provides the geometry/Vector2 standard library operation.

Source
  # Vector2.zero provides the geometry/Vector2 standard library operation.
  # @return Any the resulting value.
  zero: ->
    Vector2(0, 0)