TUF_LOGICThe TUF_LOGIC library incorporates three-valued logic (also known as trilean logic) into Pine Script, enabling the representation of states beyond the binary True and False to include an 'Uncertain' state. This addition is particularly apt for financial market contexts where information may not always be black or white, accommodating scenarios of partial or ambiguous data.
Key Features:
Trilean Data Type: Defines a tri type, facilitating the representation of True (1), Uncertain (0), and False (-1) states, thus accommodating a more nuanced approach to logical evaluation.
Validation and Conversion: Includes methods like validate, to ensure trilean variables conform to expected states, and to_bool, for converting trilean to boolean values, enhancing interoperability with binary logic systems.
Core Logical Operations: Extends traditional logical operators (AND, OR, NOT, XOR, EQUALITY) to work within the trilean domain, enabling complex conditionals that reflect real-world uncertainties.
Specialized Logical Operations:
Implication Operators: Features IMP_K (Kleene's), IMP_L (Łukasiewicz's), and IMP_RM3, offering varied approaches to logical implication within the trilean framework.
Possibility, Necessity, and Contingency Operators: Implements MA ("it is possible that..."), LA ("it is necessary that..."), and IA ("it is unknown/contingent that..."), derived from Tarski-Łukasiewicz's modal logic attempts, enriching the library with modal logic capabilities.
Unanimity Functions: The UNANIMOUS operator assesses complete agreement among trilean values, useful for scenarios requiring consensus or uniformity across multiple indicators or conditions.
This library is developed to support scenarios in financial trading and analysis where decisions might hinge on more than binary outcomes. By incorporating modal logic aspects and providing a framework for handling uncertainty through the MA, LA, and IA operations, TUF_LOGIC bridges the gap between classical binary logic and the realities of uncertain information, making it a valuable tool for developing sophisticated trading strategies and analytical models.
Library "TUF_LOGIC"
3VL Implementation (TUF stands for True, Uncertain, False.)
method validate(self)
Ensures a valid trilean variable. This works by clamping the variable to the range associated with the trilean type.
Namespace types: tri
Parameters:
self (tri)
Returns: Validated trilean object.
method to_bool(self)
Converts a trilean object into a boolean object. True -> True, Uncertain -> na, False -> False.
Namespace types: tri
Parameters:
self (tri)
Returns: A boolean variable.
method NOT(self)
Negates the trilean object. True -> False, Uncertain -> Uncertain, False -> True
Namespace types: tri
Parameters:
self (tri)
Returns: Negated trilean object.
method AND(self, comparator)
Logical AND operation for trilean objects.
Namespace types: tri
Parameters:
self (tri) : The first trilean object.
comparator (tri) : The second trilean object to compare with.
Returns: `tri` Result of the AND operation as a trilean object.
method OR(self, comparator)
Logical OR operation for trilean objects.
Namespace types: tri
Parameters:
self (tri) : The first trilean object.
comparator (tri) : The second trilean object to compare with.
Returns: `tri` Result of the OR operation as a trilean object.
method EQUALITY(self, comparator)
Logical EQUALITY operation for trilean objects.
Namespace types: tri
Parameters:
self (tri) : The first trilean object.
comparator (tri) : The second trilean object to compare with.
Returns: `tri` Result of the EQUALITY operation as a trilean object, True if both are equal, False otherwise.
method XOR(self, comparator)
Logical XOR (Exclusive OR) operation for trilean objects.
Namespace types: tri
Parameters:
self (tri) : The first trilean object.
comparator (tri) : The second trilean object to compare with.
Returns: `tri` Result of the XOR operation as a trilean object.
method IMP_K(self, comparator)
Material implication using Kleene's logic for trilean objects.
Namespace types: tri
Parameters:
self (tri) : The antecedent trilean object.
comparator (tri) : The consequent trilean object.
Returns: `tri` Result of the implication operation as a trilean object.
method IMP_L(self, comparator)
Logical implication using Łukasiewicz's logic for trilean objects.
Namespace types: tri
Parameters:
self (tri) : The antecedent trilean object.
comparator (tri) : The consequent trilean object.
Returns: `tri` Result of the implication operation as a trilean object.
method IMP_RM3(self, comparator)
Logical implication using RM3 logic for trilean objects.
Namespace types: tri
Parameters:
self (tri) : The antecedent trilean object.
comparator (tri) : The consequent trilean object.
Returns: `tri` Result of the RM3 implication as a trilean object.
method MA(self)
Evaluates to True if the trilean object is either True or Uncertain, False otherwise.
Namespace types: tri
Parameters:
self (tri) : The trilean object to evaluate.
Returns: `tri` Result of the operation as a trilean object.
method LA(self)
Evaluates to True if the trilean object is True, False otherwise.
Namespace types: tri
Parameters:
self (tri) : The trilean object to evaluate.
Returns: `tri` Result of the operation as a trilean object.
method IA(self)
Evaluates to True if the trilean object is Uncertain, False otherwise.
Namespace types: tri
Parameters:
self (tri) : The trilean object to evaluate.
Returns: `tri` Result of the operation as a trilean object.
UNANIMOUS(self, comparator)
Evaluates the unanimity between two trilean values.
Parameters:
self (tri) : The first trilean value.
comparator (tri) : The second trilean value.
Returns: `tri` Returns True if both values are True, False if both are False, and Uncertain otherwise.
method UNANIMOUS(self)
Evaluates the unanimity among an array of trilean values.
Namespace types: array
Parameters:
self (array) : The array of trilean values.
Returns: `tri` Returns True if all values are True, False if all are False, and Uncertain otherwise.
tri
Three Value Logic (T.U.F.), or trilean. Can be True (1), Uncertain (0), or False (-1).
Fields:
v (series int) : Value of the trilean variable. Can be True (1), Uncertain (0), or False (-1).
TYPE
CandlesGroup_TypesLibrary "CandlesGroup_Types"
CandlesGroup Type allows you to efficiently store and access properties of all the candles in your chart.
You can easily manipulate large datasets, work with multiple timeframes, or analyze multiple symbols simultaneously. By encapsulating the properties of each candle within a CandlesGroup object, you gain a convenient and organized way to handle complex candlestick patterns and data.
For usage instructions and detailed examples, please refer to the comments and examples provided in the source code.
method init(_self)
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup)
method init(_self, propertyNames)
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup)
propertyNames (string )
method get(_self, key)
get values array from a given property name
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
key (string) : : key name of selected property. Default is "index"
Returns: values array
method size(_self)
get size of values array. By default it equals to current bar_index
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
Returns: size of values array
method push(_self, key, value)
push single value to specific property
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
key (string) : : key name of selected property
value (float) : : property value
Returns: CandlesGroup object
method push(_self, arr)
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup)
arr (float )
method populate(_self, ohlc)
populate ohlc to CandlesGroup
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
ohlc (float ) : : array of ohlc
Returns: CandlesGroup object
method populate(_self, values, propertiesNames)
populate values base on given properties Names
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
values (float ) : : array of property values
propertiesNames (string ) : : an array stores property names. Use as keys to get values
Returns: CandlesGroup object
method populate(_self)
populate values (default setup)
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
Returns: CandlesGroup object
method lookback(arr, bars_lookback)
get property value on previous candles. For current candle, use *.lookback()
Namespace types: float
Parameters:
arr (float ) : : array of selected property values
bars_lookback (int) : : number of candles lookback. 0 = current candle. Default is 0
Returns: single property value
method highest_within_bars(_self, hiSource, start, end, useIndex)
get the highest property value between specific candles
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
hiSource (string) : : key name of selected property
start (int) : : start bar for calculation. Default is candles lookback value from current candle. 'index' value is used if 'useIndex' = true
end (int) : : end bar for calculation. Default is candles lookback value from current candle. 'index' value is used if 'useIndex' = true. Default is 0
useIndex (bool) : : use index instead of lookback value. Default = false
Returns: the highest value within candles
method highest_within_bars(_self, returnWithIndex, hiSource, start, end, useIndex)
get the highest property value and bar index between specific candles
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
returnWithIndex (bool) : : the function only applicable when it is true
hiSource (string) : : key name of selected property
start (int) : : start bar for calculation. Default is candles lookback value from current candle. 'index' value is used if 'useIndex' = true
end (int) : : end bar for calculation. Default is candles lookback value from current candle. 'index' value is used if 'useIndex' = true. Default is 0
useIndex (bool) : : use index instead of lookback value. Default = false
Returns:
method highest_point_within_bars(_self, hiSource, start, end, useIndex)
get a Point object which contains highest property value between specific candles
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
hiSource (string) : : key name of selected property
start (int) : : start bar for calculation. Default is candles lookback value from current candle. 'index' value is used if 'useIndex' = true
end (int) : : end bar for calculation. Default is candles lookback value from current candle. 'index' value is used if 'useIndex' = true. Default is 0
useIndex (bool) : : use index instead of lookback value. Default = false
Returns: Point object contains highest property value
method lowest_within_bars(_self, loSource, start, end, useIndex)
get the lowest property value between specific candles
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
loSource (string) : : key name of selected property
start (int) : : start bar for calculation. Default is candles lookback value from current candle. 'index' value is used if 'useIndex' = true
end (int) : : end bar for calculation. Default is candles lookback value from current candle. 'index' value is used if 'useIndex' = true. Default is 0
useIndex (bool) : : use index instead of lookback value. Default = false
Returns: the lowest value within candles
method lowest_within_bars(_self, returnWithIndex, loSource, start, end, useIndex)
get the lowest property value and bar index between specific candles
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
returnWithIndex (bool) : : the function only applicable when it is true
loSource (string) : : key name of selected property
start (int) : : start bar for calculation. Default is candles lookback value from current candle. 'index' value is used if 'useIndex' = true
end (int) : : end bar for calculation. Default is candles lookback value from current candle. 'index' value is used if 'useIndex' = true. Default is 0
useIndex (bool) : : use index instead of lookback value. Default = false
Returns:
method lowest_point_within_bars(_self, loSource, start, end, useIndex)
get a Point object which contains lowest property value between specific candles
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
loSource (string) : : key name of selected property
start (int) : : start bar for calculation. Default is candles lookback value from current candle. 'index' value is used if 'useIndex' = true
end (int) : : end bar for calculation. Default is candles lookback value from current candle. 'index' value is used if 'useIndex' = true. Default is 0
useIndex (bool) : : use index instead of lookback value. Default = false
Returns: Point object contains lowest property value
method time2bar(_self, t)
Convert UNIX time to bar index of active chart
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
t (int) : : UNIX time
Returns: bar index
method time2bar(_self, timezone, YYYY, MMM, DD, hh, mm, ss)
Convert timestamp to bar index of active chart. User defined timezone required
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
timezone (string) : : User defined timezone
YYYY (int) : : Year
MMM (int) : : Month
DD (int) : : Day
hh (int) : : Hour. Default is 0
mm (int) : : Minute. Default is 0
ss (int) : : Second. Default is 0
Returns: bar index
method time2bar(_self, YYYY, MMM, DD, hh, mm, ss)
Convert timestamp to bar index of active chart
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
YYYY (int) : : Year
MMM (int) : : Month
DD (int) : : Day
hh (int) : : Hour. Default is 0
mm (int) : : Minute. Default is 0
ss (int) : : Second. Default is 0
Returns: bar index
method get_prop_from_time(_self, key, t)
get single property value from UNIX time
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
key (string) : : key name of selected property
t (int) : : UNIX time
Returns: single property value
method get_prop_from_time(_self, key, timezone, YYYY, MMM, DD, hh, mm, ss)
get single property value from timestamp. User defined timezone required
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
key (string) : : key name of selected property
timezone (string) : : User defined timezone
YYYY (int) : : Year
MMM (int) : : Month
DD (int) : : Day
hh (int) : : Hour. Default is 0
mm (int) : : Minute. Default is 0
ss (int) : : Second. Default is 0
Returns: single property value
method get_prop_from_time(_self, key, YYYY, MMM, DD, hh, mm, ss)
get single property value from timestamp
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
key (string) : : key name of selected property
YYYY (int) : : Year
MMM (int) : : Month
DD (int) : : Day
hh (int) : : Hour. Default is 0
mm (int) : : Minute. Default is 0
ss (int) : : Second. Default is 0
Returns: single property value
method bar2time(_self, index)
Convert bar index of active chart to UNIX time
Namespace types: CandlesGroup
Parameters:
_self (CandlesGroup) : : CandlesGroup object
index (int) : : bar index
Returns: UNIX time
Point
A point on chart
Fields:
price (series float) : : price value
bar (series int) : : bar index
bartime (series int) : : time in UNIX format of bar
Property
Property object which contains values of all candles
Fields:
name (series string) : : name of property
values (float ) : : an array stores values of all candles. Size of array = bar_index
CandlesGroup
Candles Group object which contains properties of all candles
Fields:
propertyNames (string ) : : an array stores property names. Use as keys to get values
properties (Property ) : : array of Property objects
Simple Ultimate Oscillator█ OVERVIEW
This indicator as an educational and showcase the usage of user-defined types (UDT) or objects for Ultimate Oscillator.
█ CREDITS
TradingView
█ FEATURES
1. Color of plot is based on contrast color of chart background.
2. Plot fill of overbought and oversold.
3. Support Multi Timeframe.
Regression Channel Alternative MTF V2█ OVERVIEW
This indicator is a predecessor to Regression Channel Alternative MTF , which is coded based on latest update of type, object and method.
█ IMPORTANT NOTES
This indicator is NOT true Multi Timeframe (MTF) but considered as Alternative MTF which calculate 100 bars for Primary MTF, can be refer from provided line helper.
The timeframe scenarios are defined based on Position, Swing and Intraday Trader.
Suppported Timeframe : W, D, 60, 15, 5 and 1.
Channel drawn based on regression calculation.
Angle channel is NOT supported.
█ INSPIRATIONS
These timeframe scenarios are defined based on Harmonic Trading : Volume Three written by Scott M Carney.
By applying channel on each timeframe, MW or ABCD patterns can be easily identified manually.
This can also be applied on other chart patterns.
█ CREDITS
Scott M Carney, Harmonic Trading : Volume Three (Reaction vs. Reversal)
█ TIMEFRAME EXPLAINED
Higher / Distal : The (next) longer or larger comparative timeframe after primary pattern has been identified.
Primary / Clear : Timeframe that possess the clearest pattern structure.
Lower / Proximate : The (next) shorter timeframe after primary pattern has been identified.
Lowest : Check primary timeframe as main reference.
█ FEATURES
Color is determined by trend or timeframe.
Some color is depends on chart contrast color.
Color is determined by trend or timeframe.
█ EXAMPLE OF USAGE / EXPLAINATION
Zigzag Array ExperimentalThis is experimental script for zigzag which uses type, method and array. Not recommend for actual usage, for pine script study maybe useful.
In this experiment, I use type as coded below. It seems have limitation as specially when push as array. As Trading View recommendation, pushing float and int into array especially for type not guarantee to work. I agree with that. Preferred to push array as line or label especially for types.
// @type Used for point especially for array
// @field x int value for bar_index
// @field y float value for price
// @field sty label style
// @field col color for text label
// @field str high or low string
type point
int x = na
float y = na
string sty = na
color col = na
string str = na
I simulate the arrays as below.
var dirLine = array.new()
var dirLabel = array.new()
var dirPoint = array.new()
....
dirPoint.unshift(zigzag.createPoint(0))
dirLabel.unshift(zigzag.createLabel(fontSize, 0, true))
dirLine.unshift(zigzag.createLine(width, switchLine, 0, true))
Here are some results.
RSI Divergence Method█ OVERVIEW
This is a divergence indicator based on Relative Strength Index (RSI).
My attempt to make this indicator updated based on latest pine script features such as type, object and method.
█ FEATURES
1. Color of plot and label is based on contrast color of chart background. Able to customize color from style menu.
2. Big divergence (Regular Divergence) is based on lime / red color.
3. Small divergence (Hidden Divergence) is based on contrast color of chart background.
█ EXAMPLES / USAGES
[-_-] DictionaryThe script shows an example implementation of dictionary-like data type which can store key:value pairs (Python style). Both keys and values can have any of the following type:
• string
• integer
• float
• boolean
• color
You can add items of different types to the same dictionary (e.g. key = 12 and value = "value" stored in the same dictionary with key = "key" and value = 0.23).
Under the hood dictionary is a custom Object (see www.tradingview.com), that has two array fields (one for storing keys, another for storing values). Keys and values of different types are converted into a string representation when adding a new item to the dictionary. The value is then converted back to certain type (bool/color/etc.) from that string representation when being retrieved. Script also utilises the new Methods (see www.tradingview.com).
The following methods are implemented:
• init() -> initialises the array fields of dictionary (without this the script throws an error "Array methods can't be called when ID of array is na"
• set(key, value) -> add a new item to dictionary; if an item for given key already exists - change it to new value
• getS(key) -> get value of string type
• getI(key) -> get value of integer type
• getF(key) -> get value of float type
• getB(key) -> get value of boolean type
• getC(key) -> get value of color type
• remove(key) -> removes item from dictionary
• len() -> get length of dictionary (the number of keys)
I could not make just one "get" function that returns any type of value (color/string/etc.), so instead I created a get function for each value type. Example usage:
• you add a string item: dictionary.set(2, "string here")
• you add a float item: dictionary.set(3, 24.56)
• to retrieve first value (key=2) do this: dictionary.getS(2)
• to retrieve second value (key=3) do this: dictionary.getF(3)
Vector2ArrayLibrary "Vector2Array"
functions to handle vector2 Array operations.
.
references:
docs.unity3d.com
gist.github.com
github.com
gist.github.com
gist.github.com
gist.github.com
.
from(source, prop_sep, vect_sep)
Generate array of vector2 from string.
Parameters:
source : string Source string of the vectors.
prop_sep : string Separator character of the vector properties (x`,`y).
vect_sep : string Separator character of the vectors ((x,y)`;`(x,y)).
Returns: array.
max(vectors)
Combination of the highest elements in column of a array of vectors.
Parameters:
vectors : array, Array of Vector2 objects.
Returns: Vector2.Vector2, Vector2 object.
-> usage:
`a = Vector2.from(1.0) , b = Vector2.from(2.0), c = Vector2.from(3.0), d = max(array.from(a, b, c)) , plot(d.x)`
min(vectors)
Combination of the lowest elements in column of a array of vectors.
Parameters:
vectors : array, Array of Vector2 objects.
Returns: Vector2.Vector2, Vector2 object.
-> usage:
`a = Vector2.from(1.0) , b = Vector2.from(2.0), c = Vector2.from(3.0), d = min(array.from(a, b, c)) , plot(d.x)`
sum(vectors)
Total sum of all vectors.
Parameters:
vectors : array, ID of the vector2 array.
Returns: Vector2.Vector2, vector2 object.
-> usage:
`a = Vector2.from(1.0) , b = Vector2.from(2.0), c = Vector2.from(3.0), d = sum(array.from(a, b, c)) , plot(d.x)`
center(vectors)
Finds the vector center of the array.
Parameters:
vectors : array, ID of the vector2 array.
Returns: Vector2.Vector2, vector2 object.
-> usage:
`a = Vector2.from(1.0) , b = Vector2.from(2.0), c = Vector2.from(3.0), d = center(array.from(a, b, c)) , plot(d.x)`
rotate(vectors, center, degree)
Rotate Array vectors around origin vector by a angle.
Parameters:
vectors : array, ID of the vector2 array.
center : Vector2.Vector2 , Vector2 object. Center of the rotation.
degree : float , Angle value.
Returns: rotated points array.
-> usage:
`a = Vector2.from(1.0) , b = Vector2.from(2.0), c = Vector2.from(3.0), d = rotate(array.from(a, b, c), b, 45.0)`
scale(vectors, center, rate)
Scale Array vectors based on a origin vector perspective.
Parameters:
vectors : array, ID of the vector2 array.
center : Vector2.Vector2 , Vector2 object. Origin center of the transformation.
rate : float , Rate to apply transformation.
Returns: rotated points array.
-> usage:
`a = Vector2.from(1.0) , b = Vector2.from(2.0), c = Vector2.from(3.0), d = scale(array.from(a, b, c), b, 1.25)`
move(vectors, center, rate)
Move Array vectors by a rate of the distance to center position (LERP).
Parameters:
vectors : array, ID of the vector2 array.
center
rate
Returns: Moved points array.
-> usage:
`a = Vector2.from(1.0) , b = Vector2.from(2.0), c = Vector2.from(3.0), d = move(array.from(a, b, c), b, 1.25)`
to_string(id, separator)
Reads a array of vectors into a string, of the form ` `.
Parameters:
id : array, ID of the vector2 array.
separator : string separator for cell splitting.
Returns: string Translated complex array into string.
-> usage:
`a = Vector2.from(1.0) , b = Vector2.from(2.0), c = Vector2.from(3.0), d = to_string(array.from(a, b, c))`
to_string(id, format, separator)
Reads a array of vectors into a string, of the form ` `.
Parameters:
id : array, ID of the vector2 array.
format : string , Format to apply transformation.
separator : string , Separator for cell splitting.
Returns: string Translated complex array into string.
-> usage:
`a = Vector2.from(1.234) , b = Vector2.from(2.23), c = Vector2.from(3.1234), d = to_string(array.from(a, b, c), "#.##")`
Segment2Library "Segment2"
Structure representation of a directed straight line in two dimensions from origin to target vectors.
.
reference:
graphics.stanford.edu
.
new(origin, target)
Generate a new segment.
Parameters:
origin : Vector2 . Origin of the segment.
target : Vector2 . Target of the segment.
Returns: Segment2.
new(origin_x, origin_y, target_x, target_y)
Generate a new segment.
Parameters:
origin_x : float . Origin of the segment x coordinate.
origin_y : float . Origin of the segment y coordinate.
target_x : float . Target of the segment x coordinate.
target_y : float . Target of the segment y coordinate.
Returns: Segment2.
copy(this)
Copy a segment.
Parameters:
this : Vector2 . Segment to copy.
Returns: Segment2.
length_squared(this)
Squared length of the normalized segment vector. For comparing vectors this is computationaly lighter.
Parameters:
this : Segment2 . Sorce segment.
Returns: float.
length(this)
Length of the normalized segment vector.
Parameters:
this : Segment2 . Sorce segment.
Returns: float.
opposite(this)
Reverse the direction of the segment.
Parameters:
this : Segment2 . Source segment.
Returns: Segment2.
is_degenerate(this)
Segment is degenerate when origin and target are equal.
Parameters:
this : Segment2 . Source segment.
Returns: bool.
is_horizontal(this)
Segment is horizontal?.
Parameters:
this : Segment2 . Source segment.
Returns: bool.
is_horizontal(this, precision)
Segment is horizontal?.
Parameters:
this : Segment2 . Source segment.
precision : float . Limit of precision.
Returns: bool.
is_vertical(this)
Segment is vertical?.
Parameters:
this : Segment2 . Source segment.
Returns: bool.
is_vertical(this, precision)
Segment is vertical?.
Parameters:
this : Segment2 . Source segment.
precision : float . Limit of precision.
Returns: bool.
equals(this, other)
Tests two segments for equality (share same origin and target).
Parameters:
this : Segment2 . Source segment.
other : Segment2 . Target segment.
Returns: bool.
nearest_to_point(this, point)
Find the nearest point in a segment to another point.
Parameters:
this : Segment2 . Source segment.
point : Vector2 . Point to aproximate.
Returns: Vector2.
intersection(this, other)
Find the intersection vector of 2 lines.
Parameters:
this : Segment2 . Segment A.
other : Segment2 . Segment B.
Returns: Vector2.Vector2 Object.
extend(this, at_origin, at_target)
Extend a segment by the percent ratio provided.
Parameters:
this : Segment2 . Source segment.
at_origin : float . Percent ratio to extend at origin vector.
at_target : float . Percent ratio to extend at target vector.
Returns: Segment2.
to_string(this)
Translate segment to string format `( (x,y), (x,y) )`.
Parameters:
this : Segment2 . Source segment.
Returns: string.
to_string(this, format)
Translate segment to string format `((x,y), (x,y))`.
Parameters:
this : Segment2 . Source segment.
format : string . Format string to apply.
Returns: string.
to_array(this)
Translate segment to array format.
Parameters:
this : Segment2 . Source segment.
Returns: array.
CommonTypesDrawingLibrary "CommonTypesDrawing"
Provides a common library source for common types of used graphical drawing structures.
Includes: `Triangle, Quad, Polygon`
Triangle
Representation of a triangle using lines and linefill.
Fields:
ab : Edge of point a to b.
bc : Edge of point b to c.
ca : Edge of point c to a.
fill : Fill of the object.
solid : Check if polygon should have a fill.
Quad
Representation of a quadrilateral using lines and linefill.
Fields:
ab : Edge of point a to b.
bc : Edge of point b to c.
cd : Edge of point c to d.
da : Edge of point d to a.
fill : Fill of the object.
solid : Check if polygon should have a fill.
Polygon
Representation of a polygon using lines and linefill.
Fields:
edges : List of edges in the polygon.
fills : Fills of the object.
closed : Check if polygon line should connect last vertice to first.
solid : Check if polygon should have a fill.
Vector2Library "Vector2"
Representation of two dimensional vectors or points.
This structure is used to represent positions in two dimensional space or vectors,
for example in spacial coordinates in 2D space.
~~~
references:
docs.unity3d.com
gist.github.com
github.com
gist.github.com
gist.github.com
gist.github.com
~~~
new(x, y)
Create a new Vector2 object.
Parameters:
x : float . The x value of the vector, default=0.
y : float . The y value of the vector, default=0.
Returns: Vector2. Vector2 object.
-> usage:
`unitx = Vector2.new(1.0) , plot(unitx.x)`
from(value)
Assigns value to a new vector `x,y` elements.
Parameters:
value : float, x and y value of the vector.
Returns: Vector2. Vector2 object.
-> usage:
`one = Vector2.from(1.0), plot(one.x)`
from(value, element_sep, open_par, close_par)
Assigns value to a new vector `x,y` elements.
Parameters:
value : string . The `x` and `y` value of the vector in a `x,y` or `(x,y)` format, spaces and parentesis will be removed automatically.
element_sep : string . Element separator character, default=`,`.
open_par : string . Open parentesis character, default=`(`.
close_par : string . Close parentesis character, default=`)`.
Returns: Vector2. Vector2 object.
-> usage:
`one = Vector2.from("1.0,2"), plot(one.x)`
copy(this)
Creates a deep copy of a vector.
Parameters:
this : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = Vector2.new(1.0) , b = a.copy() , plot(b.x)`
down()
Vector in the form `(0, -1)`.
Returns: Vector2. Vector2 object.
left()
Vector in the form `(-1, 0)`.
Returns: Vector2. Vector2 object.
right()
Vector in the form `(1, 0)`.
Returns: Vector2. Vector2 object.
up()
Vector in the form `(0, 1)`.
Returns: Vector2. Vector2 object.
one()
Vector in the form `(1, 1)`.
Returns: Vector2. Vector2 object.
zero()
Vector in the form `(0, 0)`.
Returns: Vector2. Vector2 object.
minus_one()
Vector in the form `(-1, -1)`.
Returns: Vector2. Vector2 object.
unit_x()
Vector in the form `(1, 0)`.
Returns: Vector2. Vector2 object.
unit_y()
Vector in the form `(0, 1)`.
Returns: Vector2. Vector2 object.
nan()
Vector in the form `(float(na), float(na))`.
Returns: Vector2. Vector2 object.
xy(this)
Return the values of `x` and `y` as a tuple.
Parameters:
this : Vector2 . Vector2 object.
Returns: .
-> usage:
`a = Vector2.new(1.0, 1.0) , = a.xy() , plot(ax)`
length_squared(this)
Length of vector `a` in the form. `a.x^2 + a.y^2`, for comparing vectors this is computationaly lighter.
Parameters:
this : Vector2 . Vector2 object.
Returns: float. Squared length of vector.
-> usage:
`a = Vector2.new(1.0, 1.0) , plot(a.length_squared())`
length(this)
Magnitude of vector `a` in the form. `sqrt(a.x^2 + a.y^2)`
Parameters:
this : Vector2 . Vector2 object.
Returns: float. Length of vector.
-> usage:
`a = Vector2.new(1.0, 1.0) , plot(a.length())`
normalize(a)
Vector normalized with a magnitude of 1, in the form. `a / length(a)`.
Parameters:
a : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = normalize(Vector2.new(3.0, 2.0)) , plot(a.y)`
isNA(this)
Checks if any of the components is `na`.
Parameters:
this : Vector2 . Vector2 object.
Returns: bool.
usage:
p = Vector2.new(1.0, na) , plot(isNA(p)?1:0)
add(a, b)
Adds vector `b` to `a`, in the form `(a.x + b.x, a.y + b.y)`.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = one() , b = one() , c = add(a, b) , plot(c.x)`
add(a, b)
Adds vector `b` to `a`, in the form `(a.x + b, a.y + b)`.
Parameters:
a : Vector2 . Vector2 object.
b : float . Value.
Returns: Vector2. Vector2 object.
-> usage:
`a = one() , b = 1.0 , c = add(a, b) , plot(c.x)`
add(a, b)
Adds vector `b` to `a`, in the form `(a + b.x, a + b.y)`.
Parameters:
a : float . Value.
b : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = 1.0 , b = one() , c = add(a, b) , plot(c.x)`
subtract(a, b)
Subtract vector `b` from `a`, in the form `(a.x - b.x, a.y - b.y)`.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = one() , b = one() , c = subtract(a, b) , plot(c.x)`
subtract(a, b)
Subtract vector `b` from `a`, in the form `(a.x - b, a.y - b)`.
Parameters:
a : Vector2 . vector2 object.
b : float . Value.
Returns: Vector2. Vector2 object.
-> usage:
`a = one() , b = 1.0 , c = subtract(a, b) , plot(c.x)`
subtract(a, b)
Subtract vector `b` from `a`, in the form `(a - b.x, a - b.y)`.
Parameters:
a : float . value.
b : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = 1.0 , b = one() , c = subtract(a, b) , plot(c.x)`
multiply(a, b)
Multiply vector `a` with `b`, in the form `(a.x * b.x, a.y * b.y)`.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = one() , b = one() , c = multiply(a, b) , plot(c.x)`
multiply(a, b)
Multiply vector `a` with `b`, in the form `(a.x * b, a.y * b)`.
Parameters:
a : Vector2 . Vector2 object.
b : float . Value.
Returns: Vector2. Vector2 object.
-> usage:
`a = one() , b = 1.0 , c = multiply(a, b) , plot(c.x)`
multiply(a, b)
Multiply vector `a` with `b`, in the form `(a * b.x, a * b.y)`.
Parameters:
a : float . Value.
b : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = 1.0 , b = one() , c = multiply(a, b) , plot(c.x)`
divide(a, b)
Divide vector `a` with `b`, in the form `(a.x / b.x, a.y / b.y)`.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = from(3.0) , b = from(2.0) , c = divide(a, b) , plot(c.x)`
divide(a, b)
Divide vector `a` with value `b`, in the form `(a.x / b, a.y / b)`.
Parameters:
a : Vector2 . Vector2 object.
b : float . Value.
Returns: Vector2. Vector2 object.
-> usage:
`a = from(3.0) , b = 2.0 , c = divide(a, b) , plot(c.x)`
divide(a, b)
Divide value `a` with vector `b`, in the form `(a / b.x, a / b.y)`.
Parameters:
a : float . Value.
b : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = 3.0 , b = from(2.0) , c = divide(a, b) , plot(c.x)`
negate(a)
Negative of vector `a`, in the form `(-a.x, -a.y)`.
Parameters:
a : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = from(3.0) , b = a.negate , plot(b.x)`
pow(a, b)
Raise vector `a` with exponent vector `b`, in the form `(a.x ^ b.x, a.y ^ b.y)`.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = from(3.0) , b = from(2.0) , c = pow(a, b) , plot(c.x)`
pow(a, b)
Raise vector `a` with value `b`, in the form `(a.x ^ b, a.y ^ b)`.
Parameters:
a : Vector2 . Vector2 object.
b : float . Value.
Returns: Vector2. Vector2 object.
-> usage:
`a = from(3.0) , b = 2.0 , c = pow(a, b) , plot(c.x)`
pow(a, b)
Raise value `a` with vector `b`, in the form `(a ^ b.x, a ^ b.y)`.
Parameters:
a : float . Value.
b : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = 3.0 , b = from(2.0) , c = pow(a, b) , plot(c.x)`
sqrt(a)
Square root of the elements in a vector.
Parameters:
a : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = from(3.0) , b = sqrt(a) , plot(b.x)`
abs(a)
Absolute properties of the vector.
Parameters:
a : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = from(-3.0) , b = abs(a) , plot(b.x)`
min(a)
Lowest element of a vector.
Parameters:
a : Vector2 . Vector2 object.
Returns: float.
-> usage:
`a = new(3.0, 1.5) , b = min(a) , plot(b)`
max(a)
Highest element of a vector.
Parameters:
a : Vector2 . Vector2 object.
Returns: float.
-> usage:
`a = new(3.0, 1.5) , b = max(a) , plot(b)`
vmax(a, b)
Highest elements of two vectors.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 2.0) , b = new(2.0, 3.0) , c = vmax(a, b) , plot(c.x)`
vmax(a, b, c)
Highest elements of three vectors.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
c : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 2.0) , b = new(2.0, 3.0) , c = new(1.5, 4.5) , d = vmax(a, b, c) , plot(d.x)`
vmin(a, b)
Lowest elements of two vectors.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 2.0) , b = new(2.0, 3.0) , c = vmin(a, b) , plot(c.x)`
vmin(a, b, c)
Lowest elements of three vectors.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
c : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 2.0) , b = new(2.0, 3.0) , c = new(1.5, 4.5) , d = vmin(a, b, c) , plot(d.x)`
perp(a)
Perpendicular Vector of `a`, in the form `(a.y, -a.x)`.
Parameters:
a : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = perp(a) , plot(b.x)`
floor(a)
Compute the floor of vector `a`.
Parameters:
a : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = floor(a) , plot(b.x)`
ceil(a)
Ceils vector `a`.
Parameters:
a : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = ceil(a) , plot(b.x)`
ceil(a, digits)
Ceils vector `a`.
Parameters:
a : Vector2 . Vector2 object.
digits : int . Digits to use as ceiling.
Returns: Vector2. Vector2 object.
round(a)
Round of vector elements.
Parameters:
a : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = round(a) , plot(b.x)`
round(a, precision)
Round of vector elements.
Parameters:
a : Vector2 . Vector2 object.
precision : int . Number of digits to round vector "a" elements.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(0.123456, 1.234567) , b = round(a, 2) , plot(b.x)`
fractional(a)
Compute the fractional part of the elements from vector `a`.
Parameters:
a : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.123456, 1.23456) , b = fractional(a) , plot(b.x)`
dot_product(a, b)
dot_product product of 2 vectors, in the form `a.x * b.x + a.y * b.y.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: float.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = dot_product(a, b) , plot(c)`
cross_product(a, b)
cross product of 2 vectors, in the form `a.x * b.y - a.y * b.x`.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: float.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = cross_product(a, b) , plot(c)`
equals(a, b)
Compares two vectors
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: bool. Representing the equality.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = equals(a, b) ? 1 : 0 , plot(c)`
sin(a)
Compute the sine of argument vector `a`.
Parameters:
a : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = sin(a) , plot(b.x)`
cos(a)
Compute the cosine of argument vector `a`.
Parameters:
a : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = cos(a) , plot(b.x)`
tan(a)
Compute the tangent of argument vector `a`.
Parameters:
a : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = tan(a) , plot(b.x)`
atan2(x, y)
Approximation to atan2 calculation, arc tangent of `y/x` in the range (-pi,pi) radians.
Parameters:
x : float . The x value of the vector.
y : float . The y value of the vector.
Returns: float. Value with angle in radians. (negative if quadrante 3 or 4)
-> usage:
`a = new(3.0, 1.5) , b = atan2(a.x, a.y) , plot(b)`
atan2(a)
Approximation to atan2 calculation, arc tangent of `y/x` in the range (-pi,pi) radians.
Parameters:
a : Vector2 . Vector2 object.
Returns: float, value with angle in radians. (negative if quadrante 3 or 4)
-> usage:
`a = new(3.0, 1.5) , b = atan2(a) , plot(b)`
distance(a, b)
Distance between vector `a` and `b`.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: float.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = distance(a, b) , plot(c)`
rescale(a, length)
Rescale a vector to a new magnitude.
Parameters:
a : Vector2 . Vector2 object.
length : float . Magnitude.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = 2.0 , c = rescale(a, b) , plot(c.x)`
rotate(a, radians)
Rotates vector by a angle.
Parameters:
a : Vector2 . Vector2 object.
radians : float . Angle value in radians.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = 2.0 , c = rotate(a, b) , plot(c.x)`
rotate_degree(a, degree)
Rotates vector by a angle.
Parameters:
a : Vector2 . Vector2 object.
degree : float . Angle value in degrees.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = 45.0 , c = rotate_degree(a, b) , plot(c.x)`
rotate_around(this, center, angle)
Rotates vector `target` around `origin` by angle value.
Parameters:
this
center : Vector2 . Vector2 object.
angle : float . Angle value in degrees.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = rotate_around(a, b, 45.0) , plot(c.x)`
perpendicular_distance(a, b, c)
Distance from point `a` to line between `b` and `c`.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
c : Vector2 . Vector2 object.
Returns: float.
-> usage:
`a = new(1.5, 2.6) , b = from(1.0) , c = from(3.0) , d = perpendicular_distance(a, b, c) , plot(d.x)`
project(a, axis)
Project a vector onto another.
Parameters:
a : Vector2 . Vector2 object.
axis : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = project(a, b) , plot(c.x)`
projectN(a, axis)
Project a vector onto a vector of unit length.
Parameters:
a : Vector2 . Vector2 object.
axis : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = projectN(a, b) , plot(c.x)`
reflect(a, axis)
Reflect a vector on another.
Parameters:
a : Vector2 . Vector2 object.
axis
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = reflect(a, b) , plot(c.x)`
reflectN(a, axis)
Reflect a vector to a arbitrary axis.
Parameters:
a : Vector2 . Vector2 object.
axis
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = reflectN(a, b) , plot(c.x)`
angle(a)
Angle in radians of a vector.
Parameters:
a : Vector2 . Vector2 object.
Returns: float.
-> usage:
`a = new(3.0, 1.5) , b = angle(a) , plot(b)`
angle_unsigned(a, b)
unsigned degree angle between 0 and +180 by given two vectors.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: float.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = angle_unsigned(a, b) , plot(c)`
angle_signed(a, b)
Signed degree angle between -180 and +180 by given two vectors.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: float.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = angle_signed(a, b) , plot(c)`
angle_360(a, b)
Degree angle between 0 and 360 by given two vectors
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
Returns: float.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = angle_360(a, b) , plot(c)`
clamp(a, min, max)
Restricts a vector between a min and max value.
Parameters:
a : Vector2 . Vector2 object.
min
max
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = from(2.5) , d = clamp(a, b, c) , plot(d.x)`
clamp(a, min, max)
Restricts a vector between a min and max value.
Parameters:
a : Vector2 . Vector2 object.
min : float . Lower boundary value.
max : float . Higher boundary value.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = clamp(a, 2.0, 2.5) , plot(b.x)`
lerp(a, b, rate)
Linearly interpolates between vectors a and b by rate.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
rate : float . Value between (a:-infinity -> b:1.0), negative values will move away from b.
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = lerp(a, b, 0.5) , plot(c.x)`
herp(a, b, rate)
Hermite curve interpolation between vectors a and b by rate.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
rate : Vector2 . Vector2 object. Value between (a:0 > 1:b).
Returns: Vector2. Vector2 object.
-> usage:
`a = new(3.0, 1.5) , b = from(2.0) , c = from(2.5) , d = herp(a, b, c) , plot(d.x)`
transform(position, mat)
Transform a vector by the given matrix.
Parameters:
position : Vector2 . Source vector.
mat : M32 . Transformation matrix
Returns: Vector2. Transformed vector.
transform(position, mat)
Transform a vector by the given matrix.
Parameters:
position : Vector2 . Source vector.
mat : M44 . Transformation matrix
Returns: Vector2. Transformed vector.
transform(position, mat)
Transform a vector by the given matrix.
Parameters:
position : Vector2 . Source vector.
mat : matrix . Transformation matrix, requires a 3x2 or a 4x4 matrix.
Returns: Vector2. Transformed vector.
transform(this, rotation)
Transform a vector by the given quaternion rotation value.
Parameters:
this : Vector2 . Source vector.
rotation : Quaternion . Rotation to apply.
Returns: Vector2. Transformed vector.
area_triangle(a, b, c)
Find the area in a triangle of vectors.
Parameters:
a : Vector2 . Vector2 object.
b : Vector2 . Vector2 object.
c : Vector2 . Vector2 object.
Returns: float.
-> usage:
`a = new(1.0, 2.0) , b = from(2.0) , c = from(1.0) , d = area_triangle(a, b, c) , plot(d.x)`
random(max)
2D random value.
Parameters:
max : Vector2 . Vector2 object. Vector upper boundary.
Returns: Vector2. Vector2 object.
-> usage:
`a = from(2.0) , b = random(a) , plot(b.x)`
random(max)
2D random value.
Parameters:
max : float, Vector upper boundary.
Returns: Vector2. Vector2 object.
-> usage:
`a = random(2.0) , plot(a.x)`
random(min, max)
2D random value.
Parameters:
min : Vector2 . Vector2 object. Vector lower boundary.
max : Vector2 . Vector2 object. Vector upper boundary.
Returns: Vector2. Vector2 object.
-> usage:
`a = from(1.0) , b = from(2.0) , c = random(a, b) , plot(c.x)`
random(min, max)
2D random value.
Parameters:
min : Vector2 . Vector2 object. Vector lower boundary.
max : Vector2 . Vector2 object. Vector upper boundary.
Returns: Vector2. Vector2 object.
-> usage:
`a = random(1.0, 2.0) , plot(a.x)`
noise(a)
2D Noise based on Morgan McGuire @morgan3d.
Parameters:
a : Vector2 . Vector2 object.
Returns: Vector2. Vector2 object.
-> usage:
`a = from(2.0) , b = noise(a) , plot(b.x)`
to_string(a)
Converts vector `a` to a string format, in the form `"(x, y)"`.
Parameters:
a : Vector2 . Vector2 object.
Returns: string. In `"(x, y)"` format.
-> usage:
`a = from(2.0) , l = barstate.islast ? label.new(bar_index, 0.0, to_string(a)) : label(na)`
to_string(a, format)
Converts vector `a` to a string format, in the form `"(x, y)"`.
Parameters:
a : Vector2 . Vector2 object.
format : string . Format to apply transformation.
Returns: string. In `"(x, y)"` format.
-> usage:
`a = from(2.123456) , l = barstate.islast ? label.new(bar_index, 0.0, to_string(a, "#.##")) : label(na)`
to_array(a)
Converts vector to a array format.
Parameters:
a : Vector2 . Vector2 object.
Returns: array.
-> usage:
`a = from(2.0) , b = to_array(a) , plot(array.get(b, 0))`
to_barycentric(this, a, b, c)
Captures the barycentric coordinate of a cartesian position in the triangle plane.
Parameters:
this : Vector2 . Source cartesian coordinate position.
a : Vector2 . Triangle corner `a` vertice.
b : Vector2 . Triangle corner `b` vertice.
c : Vector2 . Triangle corner `c` vertice.
Returns: bool.
from_barycentric(this, a, b, c)
Captures the cartesian coordinate of a barycentric position in the triangle plane.
Parameters:
this : Vector2 . Source barycentric coordinate position.
a : Vector2 . Triangle corner `a` vertice.
b : Vector2 . Triangle corner `b` vertice.
c : Vector2 . Triangle corner `c` vertice.
Returns: bool.
to_complex(this)
Translate a Vector2 structure to complex.
Parameters:
this : Vector2 . Source vector.
Returns: Complex.
to_polar(this)
Translate a Vector2 cartesian coordinate into polar coordinates.
Parameters:
this : Vector2 . Source vector.
Returns: Pole. The returned angle is in radians.
CommonTypesMathLibrary "CommonTypesMath"
Provides a common library source for common types of useful mathematical structures.
Includes: `complex, Vector2, Vector3, Vector4, Quaternion, Segment2, Segment3, Pole, Plane, M32, M44`
complex
Representation of a Complex Number, a complex number `z` is a number in the form `z = x + yi`,
Fields:
re : Real part of the complex number.
im : Imaginary part of the complex number.
Vector2
Representation of a two dimentional vector with components `(x:float,y:float)`.
Fields:
x : Coordinate `x` of the vector.
y : Coordinate `y` of the vector.
Vector3
Representation of a three dimentional vector with components `(x:float,y:float,z:float)`.
Fields:
x : Coordinate `x` of the vector.
y : Coordinate `y` of the vector.
z : Coordinate `z` of the vector.
Vector4
Representation of a four dimentional vector with components `(x:float,y:float,z:float,w:float)`.
Fields:
x : Coordinate `x` of the vector.
y : Coordinate `y` of the vector.
z : Coordinate `z` of the vector.
w : Coordinate `w` of the vector.
Quaternion
Representation of a four dimentional vector with components `(x:float,y:float,z:float,w:float)`.
Fields:
x : Coordinate `x` of the vector.
y : Coordinate `y` of the vector.
z : Coordinate `z` of the vector.
w : Coordinate `w` of the vector, specifies the rotation component.
Segment2
Representation of a line in two dimentional space.
Fields:
origin : Origin coordinates.
target : Target coordinates.
Segment3
Representation of a line in three dimentional space.
Fields:
origin : Origin coordinates.
target : Target coordinates.
Pole
Representation of polar coordinates `(radius:float,angle:float)`.
Fields:
radius : Radius of the pole.
angle : Angle in radians of the pole.
Plane
Representation of a 3D plane.
Fields:
normal : Normal vector of the plane.
distance : Distance of the plane along its normal from the origin.
M32
Representation of a 3x2 matrix.
Fields:
m11 : First element of the first row.
m12 : Second element of the first row.
m21 : First element of the second row.
m22 : Second element of the second row.
m31 : First element of the third row.
m32 : Second element of the third row.
M44
Representation of a 4x4 matrix.
Fields:
m11 : First element of the first row.
m12 : Second element of the first row.
m13 : Third element of the first row.
m14 : fourth element of the first row.
m21 : First element of the second row.
m22 : Second element of the second row.
m23 : Third element of the second row.
m24 : fourth element of the second row.
m31 : First element of the third row.
m32 : Second element of the third row.
m33 : Third element of the third row.
m34 : fourth element of the third row.
m41 : First element of the fourth row.
m42 : Second element of the fourth row.
m43 : Third element of the fourth row.
m44 : fourth element of the fourth row.
Sort array alphabetically - educational🔶 OVERVIEW
• This educational script will sort an array of tickers alphabetically and place these values in an table , together with the according current price value next to each ticker .
🔶 SORT ALPHABETICALLY
🔹 I. We make a User Defined Type (UDT) obj , with:
· ticker - the string name of the ticker
· price - the current price (close)
• From this UDT we make an object obj.new() for each ticker
🔹 II. 2 array's are made:
• array of objects aObj , containing obj type obj.new() for every ticker
• array of strings sort , the ticker part of each object obj.new()
🔹 III. Now we make an object of each ticker with the createObject(sym ) function
object_1 = createObject("TICKER")
• the object object_1 consists off:
· ticker -> "TICKER"
· price -> current Daily close through request.security("TICKER") (non-repainting)
• object_1 will be added to the aObj array
• "TICKER" ( string ticker part of object ) will be added to the sort array
🔹 IV. The latter array is sorted alphabetically by using array.sort_indices()
EXAMPLE
originalArray = array.from("B", "A", "C")
indicesArray = // sorted indices
array.get(originalArray, 1) -> "A"
array.get(originalArray, 0) -> "B"
array.get(originalArray, 2) -> "C"
IMPORTANT
Alphabetically sorting is case sensitive , just like Java compareTo(String anotherString) !
• The comparison is based on the Unicode value of each character in the string, the lowest "Dec" values are sorted first in line.
• Comparing the "Dec" values at unicodelookup explains why default CAPITAL lettres will be sorted first,
• Default you would get this (A= 65, B= 66, a= 97, b= 98)
Aa
Ba
ab
bb
• Adding str.lower(string) in the toLowerCase() function will result to the following:
Aa
ab
Ba
bb
• (A= 65 is transformed to a= 97, ...)
• As a side note, should you write "AMZN" as "ÀMZN" this would be placed at the end, even after transforming to lower case the "Dec" values are higher (À= 192, à= 224).
• You can toggle "To Lower Case" to verify.
🔹 V. Values are placed in a table , using these sorted indices.
• With the usage of UDTs and objects , the current price has the same index in the aObj as their ticker ,
giving the advantage it is fairly easy to place every value correctly next to each other.
• The same can be done by make 2 separate arrays , 1 for the current price , the other for "TICKER" .
🔶 OTHER TECHNIQUES USED
• Alternative technique for adding comment
Instead of
// this is a comment
You can also do this:
_=" this is a comment "
• Alternate colour
· During a loop , alternate colour when i is even or odd , using the modulo operation (%) .
· This is the remainder when dividing.
EXAMPLE
· 3 % 2 = 1 -> 3 / 2 -> 1 * 2, 1 left (remainder)
· 4 % 2 = 0 -> 4 / 2 -> 2 * 2, 0 left (remainder)
· 5 % 2 = 1 -> 5 / 2 -> 2 * 2, 1 left (remainder)
for i = 0 to 10
even = i % 2 == 0
col = even ? thisColor : otherColor
• Adjust colour in script by using colour picker
Cheers!
How To Identify Argument Type Of Number Using OverloadsExample overload functions accept loading of _value for types float, int, or string, then positively identifies the actual argument type of that specific loaded _value.
How To Identify Type Of NumberExample function accepts loading of _value for types float, int, or string, then identifies whether the loaded _value is a string number, string, or number.
[CLX][#02] Registry (type-based)This script only provides a basic __setter and __getter registration function with a type-based limitation.
We don't want to blow the code with additional conditions. The suggestion was to get the basic functionality.
Benefits:
- Get/set/update global-like variables between functions
- No init needed. You can call a entry before you set it.
Get-Functions:
- f_reg_getInt(_key)
- f_reg_getFloat(_key)
- f_reg_getBool(_key)
- f_reg_getString(_key)
- f_reg_getColor(_key)
- f_reg_getLabel(_key)
- f_reg_getLine(_key)
Set-Functions:
- f_reg_setInt(_key, _value)
- f_reg_setFloat(_key, _value)
- f_reg_setBool(_key, _value)
- f_reg_setString(_key, _value)
- f_reg_setColor(_key, _value)
- f_reg_setLabel(_key, _value)
- f_reg_setLine(_key, _value)
Feel free to contribute for an extended version. :)
We hope you enjoy it! 🎉
CRYPTOLINX - jango_blockchained 😊👍
Disclaimer:
Trading success is all about following your trading strategy and the indicators should fit within your trading strategy, and not to be traded upon solely.
The script is for informational and educational purposes only. Use of the script does not constitute professional and/or financial advice. You alone have the sole responsibility of evaluating the script output and risks associated with the use of the script. In exchange for using the script, you agree not to hold dgtrd TradingView user liable for any possible claim for damages arising from any decision you make based on use of the script.