PINE LIBRARY

Vector2Operations

Updated
Library "Vector2Operations"
functions to handle vector2 operations.

math_fractional(_value) computes the fractional part of the argument value.
Parameters:
  • _value: float, value to compute.

Returns: float, fractional part.

atan2(_a) Approximation to atan2 calculation, arc tangent of y/ x in the range [-pi,pi] radians.
Parameters:
  • _a: vector2 in the form of a array [x, y].

Returns: float, value with angle in radians. (negative if quadrante 3 or 4)

set_x(_a, _value) Set the x value of vector _a.
Parameters:
  • _a: vector2 in the form of a array [x, y].
  • _value: value to replace x value of _a.

Returns: void Modifies vector _a.

set_y(_a, _value) Set the y value of vector _a.
Parameters:
  • _a: vector in the form of a array [x, y].
  • _value: value to replace y value of _a.

Returns: void Modifies vector _a.

get_x(_a) Get the x value of vector _a.
Parameters:
  • _a: vector in the form of a array [x, y].

Returns: float, x value of the vector _a.

get_y(_a) Get the y value of vector _a.
Parameters:
  • _a: vector in the form of a array [x, y].

Returns: float, y value of the vector _a.

get_xy(_a) Return the tuple of vector _a in the form [x, y]
Parameters:
  • _a: vector2 in the form of a array [x, y].

Returns: [float, float]

length_squared(_a) Length of vector _a in the form. [pow(_a, 2)], for comparing vectors this is computationaly lighter.
Parameters:
  • _a: vector in the form of a array [x, y].

Returns: float, squared length of vector.

length(_a) Magnitude of vector _a in the form. [sqrt(pow(_a, 2))]
Parameters:
  • _a: vector in the form of a array [x, y].

Returns: float, Squared length of vector.

vmin(_a) Lowest element of vector.
Parameters:
  • _a: vector in the form of a array [x, y].

Returns: float

vmax(_a) Highest element of vector.
Parameters:
  • _a: vector in the form of a array [x, y].

Returns: float

from(_value) Assigns value to a new vector x,y elements.
Parameters:
  • _value: x and y value of the vector. optional.

Returns: float[x, y] vector.

new(_x, _y) Creates a prototype array to handle vectors.
Parameters:
  • _x: float, x value of the vector. optional.
  • _y: float, y number of the vector. optional.

Returns: float[x, y] vector.

down() Vector in the form [0, -1]. Returns: float[x, y] vector.

left() Vector in the form [-1, 0]. Returns: float[x, y] vector.

one() Vector in the form [1, 1]. Returns: float[x, y] vector.

right() Vector in the form [1, 0]. Returns: float[x, y] vector

up() Vector in the form [0, 1]. Returns: float[x, y] vector

zero() Vector in the form [0, 0]. Returns: float[x, y] vector

add(_a, _b) Adds vector _b to _a, in the form
[_a.x + _b.x, _a.y + _b.y].
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].

Returns:

subtract(_a, _b) Subtract vector _b from _a, in the form
[_a.x - _b.x, _a.y - _b.y].
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].

Returns:

multiply(_a, _b) Multiply vector _a with _b, in the form
[(_a.x * _b.x), (_a.y * _b.y)]
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].

Returns:

divide(_a, _b) Divide vector _a with _b, in the form
[(_a.x / _b.x), (_a.y / _b.y)]
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].

Returns:

negate(_a) Negative of vector _a, in the form [-_a.x, -_a.y]
Parameters:
  • _a: vector in the form of a array [x, y].

Returns:

perp(_a) Perpendicular Vector of _a.
Parameters:
  • _a: vector in the form of a array [x, y].

Returns:

vfloor(_a) Compute the floor of argument vector _a.
Parameters:
  • _a: vector in the form of a array [x, y].

Returns:

fractional(_a) Compute the fractional part of the elements from vector _a.
Parameters:
  • _a: vector in the form of a array [x, y].

Returns:

vsin(_a) Compute the sine of argument vector _a.
Parameters:
  • _a: vector in the form of a array [x, y].

Returns:

equals(_a, _b) Compares two vectors
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].

Returns: boolean value representing the equality.

dot(_a, _b) Dot product of 2 vectors, in the form [_a.x * _b.x + _a.y * _b.y]
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].

Returns: float

cross_product(_a, _b) cross product of 2 vectors, in the form [_a.x * _b.y - _a.y * _b.x]
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].

Returns: float

scale(_a, _scalar) Multiply a vector by a scalar.
Parameters:
  • _a: vector in the form of a array [x, y].
  • _scalar: value to multiply vector elements by.

Returns: float[x, y] vector

normalize(_a) Vector _a normalized with a magnitude of 1, in the form. [_a / length(_a)]
Parameters:
  • _a: vector in the form of a array [x, y].

Returns: float[x, y] vector

rescale(_a) Rescale a vector to a new Magnitude.
Parameters:
  • _a: vector in the form of a array [x, y].

Returns:

rotate(_a, _radians) Rotates vector _a by angle value
Parameters:
  • _a: vector in the form of a array [x, y].
  • _radians: Angle value.

Returns:

rotate_degree(_a, _degree) Rotates vector _a by angle value
Parameters:
  • _a: vector in the form of a array [x, y].
  • _degree: Angle value.

Returns:

rotate_around(_center, _target, _degree) Rotates vector _target around _origin by angle value
Parameters:
  • _center: vector in the form of a array [x, y].
  • _target: vector in the form of a array [x, y].
  • _degree: Angle value.

Returns:

vceil(_a, _digits) Ceils vector _a
Parameters:
  • _a: vector in the form of a array [x, y].
  • _digits: digits to use as ceiling.

Returns:

vpow(_a) Raise both vector elements by a exponent.
Parameters:
  • _a: vector in the form of a array [x, y].

Returns:

distance(_a, _b) vector distance between 2 vectors.
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].

Returns: float, distance.

project(_a, _axis) Project a vector onto another.
Parameters:
  • _a: vector in the form of a array [x, y].
  • _axis: float[x, y] vector2

Returns: float[x, y] vector

projectN(_a, _axis) Project a vector onto a vector of unit length.
Parameters:
  • _a: vector in the form of a array [x, y].
  • _axis: vector in the form of a array [x, y].

Returns: float[x, y] vector

reflect(_a, _b) Reflect a vector on another.
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].

Returns: float[x, y] vector

reflectN(_a, _b) Reflect a vector to a arbitrary axis.
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].

Returns: float[x, y] vector

angle(_a) Angle in radians of a vector.
Parameters:
  • _a: vector in the form of a array [x, y].

Returns: float

angle_unsigned(_a, _b) unsigned degree angle between 0 and +180 by given two vectors.
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].

Returns: float

angle_signed(_a, _b) Signed degree angle between -180 and +180 by given two vectors.
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].

Returns: float

angle_360(_a, _b) Degree angle between 0 and 360 by given two vectors
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].

Returns: float

clamp(_a, _vmin, _vmax) Restricts a vector between a min and max value.
Parameters:
  • _a: vector in the form of a array [x, y].
  • _vmin: vector in the form of a array [x, y].
  • _vmax: vector in the form of a array [x, y].

Returns: float[x, y] vector

lerp(_a, _b, _rate_of_move) Linearly interpolates between vectors a and b by _rate_of_move.
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].
  • _rate_of_move: float value between (a:-infinity -> b:1.0), negative values will move away from b.

Returns: vector in the form of a array [x, y]

herp(_a, _b, _rate_of_move) Hermite curve interpolation between vectors a and b by _rate_of_move.
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].
  • _rate_of_move: float value between (a-infinity -> b1.0), negative values will move away from b.

Returns: vector in the form of a array [x, y]

area_triangle(_a, _b, _c) Find the area in a triangle of vectors.
Parameters:
  • _a: vector in the form of a array [x, y].
  • _b: vector in the form of a array [x, y].
  • _c: vector in the form of a array [x, y].

Returns: float

to_string(_a) Converts vector _a to a string format, in the form "(x, y)"
Parameters:
  • _a: vector in the form of a array [x, y].

Returns: string in "(x, y)" format

vrandom(_max) 2D random value
Parameters:
  • _max: float[x, y] vector, vector upper bound

Returns: vector in the form of a array [x, y]

noise(_a) 2D Noise based on Morgan McGuire @morgan3d
thebookofshaders.com/11/
shadertoy.com/view/4dS3Wd
Parameters:
  • _a: vector in the form of a array [x, y].

Returns: vector in the form of a array [x, y]

array_new(_size, _initial_vector) Prototype to initialize a array of vectors.
Parameters:
  • _size: size of the array.
  • _initial_vector: vector to be used as default value, in the form of array [x, y].

Returns: _vector_array complex Array in the form of a array [0:x, 1:y, 2:x, 3:y,...]

array_size(_id) number of [x, y] vector elements in array.
Parameters:
  • _id: ID of the array.

Returns: int

array_get(_id, _index) Get the vector in a array, in the form of a array [x, y]
Parameters:
  • _id: ID of the array.
  • _index: Index of the vector.

Returns: vector in the form of a array [x, y]

array_set(_id, _index, _a) Sets the values vector in a array.
Parameters:
  • _id: ID of the array.
  • _index: Index of the vector.
  • _a: vector, in the form [x, y].

Returns: Void, updates array _id.

array_push(_id, _a) inserts the vector at the end of array.
Parameters:
  • _id: ID of the array.
  • _a: vector, in the form [x, y].

Returns: Void, updates array _id.

array_unshift(_id, _a) inserts the vector at the begining of array.
Parameters:
  • _id: ID of the array.
  • _a: vector, in the form [x, y].

Returns: Void, updates array _id.

array_pop(_id, _a) removes the last vector of array and returns it.
Parameters:
  • _id: ID of the array.
  • _a: vector, in the form [x, y].

Returns: vector2, updates array _id.

array_shift(_id, _a) removes the first vector of array and returns it.
Parameters:
  • _id: ID of the array.
  • _a: vector, in the form [x, y].

Returns: vector2, updates array _id.

array_sum(_id) Total sum of all vectors.
Parameters:
  • _id: ID of the array.

Returns: vector in the form of a array [x, y]

array_center(_id) Finds the vector center of the array.
Parameters:
  • _id: ID of the array.

Returns: vector in the form of a array [x, y]

array_rotate_points(_id) Rotate Array vectors around origin vector by a angle.
Parameters:
  • _id: ID of the array.

Returns: rotated points array.

array_scale_points(_id) Scale Array vectors based on a origin vector perspective.
Parameters:
  • _id: ID of the array.

Returns: rotated points array.

array_tostring(_id, _separator) Reads a array of vectors into a string, of the form "[ (x, y), ... ]""
Parameters:
  • _id: ID of the array.
  • _separator: string separator for cell splitting.

Returns: string Translated complex array into string.

line_new(_a, _b) 2 vector line in the form. [a.x, a.y, b.x, b.y]
Parameters:
  • _a: vector, in the form [x, y].
  • _b: vector, in the form [x, y].

Returns:

line_get_a(_line) Start vector of a line.
Parameters:
  • _line: vector4, in the form [a.x, a.y, b.x, b.y].

Returns: float[x, y] vector2

line_get_b(_line) End vector of a line.
Parameters:
  • _line: vector4, in the form [a.x, a.y, b.x, b.y].

Returns: float[x, y] vector2

line_intersect(_line1, _line2) Find the intersection vector of 2 lines.
Parameters:
  • _line1: line of 2 vectors in the form of a array [ax, ay, bx, by].
  • _line2: line of 2 vectors in the form of a array [ax, ay, bx, by].

Returns: vector in the form of a array [x, y].

draw_line(_line, _xloc, _extend, _color, _style, _width) Draws a line using line prototype.
Parameters:
  • _line: vector4, in the form [a.x, a.y, b.x, b.y].
  • _xloc: string
  • _extend: string
  • _color: color
  • _style: string
  • _width: int

Returns: draw line object

draw_triangle(_v1, _v2, _v3, _xloc, _color, _style, _width) Draws a triangle using line prototype.
Parameters:
  • _v1: vector4, in the form [a.x, a.y, b.x, b.y].
  • _v2: vector4, in the form [a.x, a.y, b.x, b.y].
  • _v3: vector4, in the form [a.x, a.y, b.x, b.y].
  • _xloc: string
  • _color: color
  • _style: string
  • _width: int

Returns: tuple with 3 line objects. [line, line, line]

draw_rect(_v1, _size, _angle, _xloc, _color, _style, _width) Draws a square using vector2 line prototype.
Parameters:
  • _v1: vector4, in the form [a.x, a.y, b.x, b.y].
  • _size: float[x, y]
  • _angle: float
  • _xloc: string
  • _color: color
  • _style: string
  • _width: int

Returns: tuple with 3 line objects. [line, line, line]
Release Notes
v2 changed label to console for displaying examples.

Added:
perpendicular_distance(_a, _b, _c) Distance from point _a to line between _b and _c.
  Parameters:
    _a: vector in the form of a array [x, y].
    _b: vector in the form of a array [x, y].
    _c: vector in the form of a array [x, y].
  Returns: float
arraysMATHvector

Pine library

In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in a publication is governed by House rules.

Disclaimer