Skip to content

Latest commit

 

History

History
160 lines (109 loc) · 3.81 KB

maths.md

File metadata and controls

160 lines (109 loc) · 3.81 KB

sugar. maths

Angles in sugarlove are turn-based, where 1 indicates a full turn. 1 is the equivalent of 360 degrees or 2π radians. More info here.

sugar.maths. cos (a)

  • Returns the cosine of a as a turn-based angle.

sugar.maths. sin (a)

  • Returns the sine of a as a turn-based angle.

sugar.maths. atan2 (x, y)

  • Converts {x; y} as an angle from 0 to 1. Returns that angle.


sugar.maths. lerp (a, b, i)

  • Returns the linear interpolation from a to b with the parameter i.
  • For the intended use, i should be between 0 and 1. However it is not limited to those value.

sugar.maths. flr (a)

  • Returns the closest integer that is equal or below a.

sugar.maths. ceil (a)

  • Returns the closest integer that is equal or above a.

sugar.maths. round (a)

  • Returns the closest integer to a.

sugar.maths. sgn (a)

  • Returns 1 if a is positive.
  • Returns -1 if a is negative.
  • Returns 0 if a is zero.

sugar.maths. sqr (a)

  • Returns a * a.

sugar.maths. cub (a)

  • Returns a * a * a.

sugar.maths. pow (a, b)

  • Returns the result of a to the power of b.
  • pow(a, 2) is much slower than sqr(a).

sugar.maths. sqrt (a)

  • Returns the square root of a.

sugar.maths. abs (a)

  • Returns the absolute (positive) value of a.

sugar.maths. min (a, b)

  • Returns the lower value between a and b.

sugar.maths. max (a, b)

  • Returns the higher value between a and b.

sugar.maths. mid (a, b, c)

  • Returns the middle value between a, b and c.
  • mid(1, 3, 2) will return 2.

sugar.maths. angle_diff (a1, a2)

  • Returns the difference between the turn-based angle a1 and the turn-based angle a2.

sugar.maths. dist (x1, y1, [x2, y2])

  • If x2 and y2 are set, returns the distance between {x1; y1} and {x2; y2}.
  • Otherwise, returns the distance between {0; 0} and {x1; y1}.

sugar.maths. sqrdist (x, y)

  • Returns the squared distance between {0; 0} and {x1; y1}.
  • Is faster than dist(...).


sugar.maths. srand (seed)

  • Sets the seed for the random number generation.

sugar.maths. raw_rnd ()

  • Returns a random number.
  • Always returns an integer.

sugar.maths. rnd (n)

  • Returns a random decimal number between 0 (included) and n (excluded).

sugar.maths. irnd (n)

  • Returns a random integer number between 0 (included) and n (excluded).

sugar.maths. pick (tab)

  • Takes an ordered table (with linear numeral keys) as parameter.
  • Returns a random element from the table.