Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* -----------------------------------
- Macros for referencing vector axes
- ---------------------------------- */
- #macro vec2_x 0
- #macro vec2_y 1
- /// @description vec2 [x [, y]] - Creates a 2d vector.
- /// @param [x]
- /// @param [y]
- /* ------------------------------------------------
- Usage Example
- vec2() - returns [0, 0]
- vec2(1) - returns [1, 1]
- vec2(2, 5) - returns [2, 5]
- ------------------------------------------------ */
- function vec2()
- {
- switch (argument_count)
- {
- case 0:
- return [0, 0];
- case 1:
- return [argument0, argument0];
- case 2:
- return [argument0, argument1];
- default:
- show_error("Error: vec2 can only hold 2 values but " + string(argument_count) + "were given", true);
- }
- }
- /// @description vec2_addf vector, value - Adds numeric value to vector.
- /// @param vec2
- /// @param value
- /* --------------------------------------------------------------
- Usage Example
- var vector = vec2(5, 4) - creates vector [5, 4]
- vec2_addf(vector, 9) => [5+9, 4+9] - returns [14, 13]
- -------------------------------------------------------------- */
- function vec2_addf(vector, value)
- {
- if not is_array(vector) show_error("vec2_addf expected vec2 as the first argument.", true);
- if not is_numeric(value) show_error("vec2_addf expected a numeric value as the second argument.", true);
- return vec2(vector[vec2_x] + value,
- vector[vec2_y] + value);
- }
- /// @description vec2_add vector_1, vector_2 - Adds two vectors together.
- /// @param vector_1
- /// @param vector_2
- /* ----------------------------------------------------------------------
- Usage Example
- var vector_1 = vec2(5, 4) - creates vector [5, 4]
- var vector_2 = vec2(4, 5) - creates vector [4, 5]
- vec2_add(vector_1, vector_2) => [5+4, 4+5] - returns [9, 9]
- ---------------------------------------------------------------------- */
- function vec2_add(vector_1, vector_2)
- {
- if not is_array(vector_1) show_error("vec2_add expected vec2 as the first argument.", true);
- if not is_array(vector_2) show_error("vec2_add expected vec2 as the second argument.", true);
- return vec2(vector_1[vec2_x] + vector_2[vec2_x],
- vector_1[vec2_y] + vector_2[vec2_y]);
- }
- /// @description vec2_subf vector, value - Subtracts numeric value from a vector.
- /// @param vec2
- /// @param value
- /* --------------------------------------------------------------
- Usage Example
- var vector = vec2(5, 4) - creates vector [5, 4]
- vec2_subf(vector, 3) => [5-3, 4-3] - returns [2, 1]
- -------------------------------------------------------------- */
- function vec2_subf(vector, value)
- {
- if not is_array(vector) show_error("vec2_subf expected vec2 as the first argument.", true);
- if not is_numeric(value) show_error("vec2_subf expected a numeric value as the second argument.", true);
- return vec2(vector[vec2_x] - value,
- vector[vec2_y] - value);
- }
- /// @description vec2_sub vector_1, vector_2 - Subtracts vector_2 from vector_1.
- /// @param vector_1
- /// @param vector_2
- /* ----------------------------------------------------------------------
- Usage Example
- var vector_1 = vec2(5, 4) - creates vector [5, 4]
- var vector_2 = vec2(4, 5) - creates vector [4, 5]
- vec2_sub(vector_1, vector_2) => [5-4, 4-5] - returns [1, -1]
- ---------------------------------------------------------------------- */
- function vec2_sub(vector_1, vector_2)
- {
- if not is_array(vector_1) show_error("vec2_sub expected vec2 as the first argument.", true);
- if not is_array(vector_2) show_error("vec2_sub expected vec2 as the second argument.", true);
- return vec2(vector_1[vec2_x] - vector_2[vec2_x],
- vector_1[vec2_y] - vector_2[vec2_y]);
- }
- /// @description vec2_mulf vector, value - Multiplies vector by a numeric value.
- /// @param vec2
- /// @param value
- /* ---------------------------------------------------------------
- Usage Example
- var vector = vec2(5, 4) - creates vector [5, 4]
- vec2_mulf(vector, 3) => [5*3, 4*3] - returns [15, 12]
- --------------------------------------------------------------- */
- function vec2_mulf(vector, value)
- {
- if not is_array(vector) show_error("vec2_mulf expected vec2 as the first argument.", true);
- if not is_numeric(value) show_error("vec2_mulf expected a numeric value as the second argument.", true);
- return vec2(vector[vec2_x] * value,
- vector[vec2_y] * value);
- }
- /// @description vec2_mul vector_1, vector_2 - Multiplies two vectors.
- /// @param vector_1
- /// @param vector_2
- /* ----------------------------------------------------------------------
- Usage Example
- var vector_1 = vec2(5, 4) - creates vector [5, 4]
- var vector_2 = vec2(4, 5) - creates vector [4, 5]
- vec2_mul(vector_1, vector_2) => [5*4, 4*5] - returns [20, 20]
- ---------------------------------------------------------------------- */
- function vec2_mul(vector_1, vector_2)
- {
- if not is_array(vector_1) show_error("vec2_mul expected vec2 as the first argument.", true);
- if not is_array(vector_2) show_error("vec2_mul expected vec2 as the second argument.", true);
- return vec2(vector_1[vec2_x] * vector_2[vec2_x],
- vector_1[vec2_y] * vector_2[vec2_y]);
- }
- /// @description vec2_divf vector, value - Divides vector by a numeric value.
- /// @param vec2
- /// @param value
- /* --------------------------------------------------------------------
- Usage Example
- var vector = vec2(69, 12) - creates vector [69, 12]
- vec2_divf(vector, 3) => [69/3, 12/3] - returns [23, 4]
- -------------------------------------------------------------------- */
- function vec2_divf(vector, value)
- {
- if not is_array(vector) show_error("vec2_divf expected vec2 as the first argument.", true);
- if not is_numeric(value) show_error("vec2_divf expected a numeric value as the second argument.", true);
- return vec2(vector[vec2_x] / value,
- vector[vec2_y] / value);
- }
- /// @description vec2_div vector_1, vector_2 - Divides two vectors.
- /// @param vector_1
- /// @param vector_2
- /* -------------------------------------------------------------------------
- Usage Example
- var vector_1 = vec2(12) - creates vector [12, 12]
- var vector_2 = vec2(3, 4) - creates vector [3, 4]
- vec2_div(vector_1, vector_2) => [12/3, 12/4] - returns [4, 3]
- ------------------------------------------------------------------------- */
- function vec2_div(vector_1, vector_2)
- {
- if not is_array(vector_1) show_error("vec2_div expected vec2 as the first argument.", true);
- if not is_array(vector_2) show_error("vec2_div expected vec2 as the second argument.", true);
- return vec2(vector_1[vec2_x] / vector_2[vec2_x],
- vector_1[vec2_y] / vector_2[vec2_y]);
- }
- /// @description vec2_dot vector_1, vector_2 [, normalized] - Returns dot product of two vectors.
- /// @param vector_1
- /// @param vector_2
- /// @param [normalized]
- function vec2_dot(vector_1, vector_2, normalized = false)
- {
- if not is_array(vector_1) show_error("vec2_dot expected vec2 as the first argument.", true);
- if not is_array(vector_2) show_error("vec2_dot expected vec2 as the second argument.", true);
- if normalized return dot_product_normalized(vector_1[vec2_x], vector_1[vec2_y],
- vector_2[vec2_x], vector_2[vec2_y]);
- return dot_product(vector_1[vec2_x], vector_1[vec2_y],
- vector_2[vec2_x], vector_2[vec2_y]);
- }
- /// @description vec2_mag vector - Returns magnitude of a vector.
- /// @param vector
- function vec2_mag(vector)
- {
- if not is_array(vector) show_error("vec2_mag expected vec2 as the first argument.", true);
- return sqrt(sqr(vector[vec2_x])+sqr(vector[vec2_y]));
- }
- /// @description vec2_dir vector - Returns direction of a vector.
- /// @param vector
- /// @param [result_in_degrees]
- function vec2_dir(vector, result_in_degrees = false)
- {
- if not is_array(vector) show_error("vec2_dir expected vec2 as the first argument.", true);
- if result_in_degrees return darctan(vector[vec2_y] / vector [vec2_x]);
- return arctan(vector[vec2_y] / vector [vec2_x]);
- }
- /// @description vec2_angdiff vector_1, vector_2 - Returns angle difference between two vectors.
- /// @param vector_1
- /// @param vector_2
- function vec2_angdiff(vector_1, vector_2)
- {
- if not is_array(vector_1) show_error("vec2_angdiff expected vec2 as the first argument.", true);
- if not is_array(vector_2) show_error("vec2_angdiff expected vec2 as the second argument.", true);
- return angle_difference(vec2_dir(vector_1, true),
- vec2_dir(vector_2, true));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement