 1107 visningar
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 radians.
Parameters:
• _a: vector2 in the form of a array .
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 .
• _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 .
• _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 .
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 .
Returns: float, y value of the vector _a.

get_xy(_a) Return the tuple of vector _a in the form
Parameters:
• _a: vector2 in the form of a array .
Returns:

length_squared(_a) Length of vector _a in the form. , for comparing vectors this is computationaly lighter.
Parameters:
• _a: vector in the form of a array .
Returns: float, squared length of vector.

length(_a) Magnitude of vector _a in the form.
Parameters:
• _a: vector in the form of a array .
Returns: float, Squared length of vector.

vmin(_a) Lowest element of vector.
Parameters:
• _a: vector in the form of a array .
Returns: float

vmax(_a) Highest element of vector.
Parameters:
• _a: vector in the form of a array .
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 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 vector.

down() Vector in the form . Returns: float vector.

left() Vector in the form . Returns: float vector.

one() Vector in the form . Returns: float vector.

right() Vector in the form . Returns: float vector

up() Vector in the form . Returns: float vector

zero() Vector in the form . Returns: float vector

.
Parameters:
• _a: vector in the form of a array .
• _b: vector in the form of a array .
Returns:

subtract(_a, _b) Subtract vector _b from _a, in the form
.
Parameters:
• _a: vector in the form of a array .
• _b: vector in the form of a array .
Returns:

multiply(_a, _b) Multiply vector _a with _b, in the form

Parameters:
• _a: vector in the form of a array .
• _b: vector in the form of a array .
Returns:

divide(_a, _b) Divide vector _a with _b, in the form

Parameters:
• _a: vector in the form of a array .
• _b: vector in the form of a array .
Returns:

negate(_a) Negative of vector _a, in the form
Parameters:
• _a: vector in the form of a array .
Returns:

perp(_a) Perpendicular Vector of _a.
Parameters:
• _a: vector in the form of a array .
Returns:

vfloor(_a) Compute the floor of argument vector _a.
Parameters:
• _a: vector in the form of a array .
Returns:

fractional(_a) Compute the fractional part of the elements from vector _a.
Parameters:
• _a: vector in the form of a array .
Returns:

vsin(_a) Compute the sine of argument vector _a.
Parameters:
• _a: vector in the form of a array .
Returns:

equals(_a, _b) Compares two vectors
Parameters:
• _a: vector in the form of a array .
• _b: vector in the form of a array .
Returns: boolean value representing the equality.

dot(_a, _b) Dot product of 2 vectors, in the form
Parameters:
• _a: vector in the form of a array .
• _b: vector in the form of a array .
Returns: float

cross_product(_a, _b) cross product of 2 vectors, in the form
Parameters:
• _a: vector in the form of a array .
• _b: vector in the form of a array .
Returns: float

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

normalize(_a) Vector _a normalized with a magnitude of 1, in the form.
Parameters:
• _a: vector in the form of a array .
Returns: float vector

rescale(_a) Rescale a vector to a new Magnitude.
Parameters:
• _a: vector in the form of a array .
Returns:

rotate(_a, _radians) Rotates vector _a by angle value
Parameters:
• _a: vector in the form of a array .
Returns:

rotate_degree(_a, _degree) Rotates vector _a by angle value
Parameters:
• _a: vector in the form of a array .
• _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 .
• _target: vector in the form of a array .
• _degree: Angle value.
Returns:

vceil(_a, _digits) Ceils vector _a
Parameters:
• _a: vector in the form of a array .
• _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 .
Returns:

distance(_a, _b) vector distance between 2 vectors.
Parameters:
• _a: vector in the form of a array .
• _b: vector in the form of a array .
Returns: float, distance.

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

projectN(_a, _axis) Project a vector onto a vector of unit length.
Parameters:
• _a: vector in the form of a array .
• _axis: vector in the form of a array .
Returns: float vector

reflect(_a, _b) Reflect a vector on another.
Parameters:
• _a: vector in the form of a array .
• _b: vector in the form of a array .
Returns: float vector

reflectN(_a, _b) Reflect a vector to a arbitrary axis.
Parameters:
• _a: vector in the form of a array .
• _b: vector in the form of a array .
Returns: float vector

angle(_a) Angle in radians of a vector.
Parameters:
• _a: vector in the form of a array .
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 .
• _b: vector in the form of a array .
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 .
• _b: vector in the form of a array .
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 .
• _b: vector in the form of a array .
Returns: float

clamp(_a, _vmin, _vmax) Restricts a vector between a min and max value.
Parameters:
• _a: vector in the form of a array .
• _vmin: vector in the form of a array .
• _vmax: vector in the form of a array .
Returns: float 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 .
• _b: vector in the form of a array .
• _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

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 .
• _b: vector in the form of a array .
• _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

area_triangle(_a, _b, _c) Find the area in a triangle of vectors.
Parameters:
• _a: vector in the form of a array .
• _b: vector in the form of a array .
• _c: vector in the form of a array .
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 .
Returns: string in "(x, y)" format

vrandom(_max) 2D random value
Parameters:
• _max: float vector, vector upper bound
Returns: vector in the form of a array

noise(_a) 2D Noise based on Morgan McGuire @morgan3d
Parameters:
• _a: vector in the form of a array .
Returns: vector in the form of a array

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 .
Returns: _vector_array complex Array in the form of a array

array_size(_id) number of 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
Parameters:
• _id: ID of the array.
• _index: Index of the vector.
Returns: vector in the form of a array

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 .

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

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

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

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

array_sum(_id) Total sum of all vectors.
Parameters:
• _id: ID of the array.
Returns: vector in the form of a array

array_center(_id) Finds the vector center of the array.
Parameters:
• _id: ID of the array.
Returns: vector in the form of a array

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 """
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.
Parameters:
• _a: vector, in the form .
• _b: vector, in the form .
Returns:

line_get_a(_line) Start vector of a line.
Parameters:
• _line: vector4, in the form .
Returns: float vector2

line_get_b(_line) End vector of a line.
Parameters:
• _line: vector4, in the form .
Returns: float 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 .

draw_line(_line, _xloc, _extend, _color, _style, _width) Draws a line using line prototype.
Parameters:
• _line: vector4, in the form .
• _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 .
• _v2: vector4, in the form .
• _v3: vector4, in the form .
• _xloc: string
• _color: color
• _style: string
• _width: int
Returns: tuple with 3 line objects.

draw_rect(_v1, _size, _angle, _xloc, _color, _style, _width) Draws a square using vector2 line prototype.
Parameters:
• _v1: vector4, in the form .
• _size: float
• _angle: float
• _xloc: string
• _color: color
• _style: string
• _width: int
Returns: tuple with 3 line objects.

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 use it freely. Cheers to the author!

Want to use this library?

Copy text to clipboard and paste it in your script.