This library provides a comprehensive set of geometric functions based on 2 simple types for point and line manipulation, point array calculations, some vector operations (Borrowed from RicardoSantos ), angle calculations, and basic polygon analysis. It offers tools for creating, transforming, and analyzing geometric shapes and their relationships.
View the source code for detailed documentation on each function and type.
This library enhances TradingView's Pine Script with robust geometric capabilities. It introduces the Point and Line types, along with a suite of functions for various geometric operations. These functionalities empower you to perform advanced calculations, manipulations, and analyses involving points, lines, vectors, angles, and polygons directly within your Pine scripts. The example is at the bottom of the script. ( Commented out )
█ CONCEPTS
This library revolves around two fundamental types:
• Point: Represents a point in 2D space with x and y coordinates, along with optional 'a' (angle) and 'v' (value) fields for versatile use. Crucially, for plotting, utilize the `.to_chart_point()` method to convert Points into plottable chart.point objects.
• Line: Defined by a starting Point and a slope, enabling calculations like getting y for a given x, or finding intersection points.
█ FEATURES
• Point Manipulation: Perform operations like addition, subtraction, scaling, rotation, normalization, calculating distances, dot products, cross products, midpoints, and more with Point objects.
• Line Operations: Create lines, determine their slope, calculate y from x (and vice versa), and find the intersection points of two lines.
• Angle Calculations: Compute angles between points in degrees or radians, including signed, unsigned, and 360-degree angles.
• Polygon Analysis: Calculate the area, perimeter, and centroid of polygons. Check if a point is inside a given polygon and determine the convex hull perimeter.
• Chart Plotting: Conveniently convert Point objects to chart.point objects for plotting lines and points on the chart. The library also includes functions for plotting lines between individual and series of points.
• Utility Functions: Includes helper functions such as square root, square, cosine, sine, tangent, arc cosine, arc sine, arc tangent, atan2, absolute distance, golden ratio tolerance check, fractional part, and safe index/check for chart plotting boundaries.
This description provides a concise overview. Consult the library's source code for in-depth documentation, including detailed descriptions, parameter types, and return values for each function and method. The source code is structured with comprehensive comments using the `//@` format for seamless integration with TradingView's auto-documentation features.
█ Possibilities..
Library "geo"
This library provides a comprehensive set of geometric functions and types, including point and line manipulation, vector operations, angle calculations, and polygon analysis. It offers tools for creating, transforming, and analyzing geometric shapes and their relationships.
sqrt(value) Square root function Parameters: value (float): (float) - The number to take the square root of Returns: (float) - The square root of the input value
sqr(x) Square function Parameters: x (float): (float) - The number to square Returns: (float) - The square of the input value
cos(v) Cosine function Parameters: v (float): (series float) - The value to find the cosine of Returns: (series float) - The cosine of the input value
sin(v) Sine function Parameters: v (float): (series float) - The value to find the sine of Returns: (series float) - The sine of the input value
tan(v) Tangent function Parameters: v (float): (series float) - The value to find the tangent of Returns: (series float) - The tangent of the input value
acos(v) Arc cosine function Parameters: v (float): (series float) - The value to find the arc cosine of Returns: (series float) - The arc cosine of the input value
asin(v) Arc sine function Parameters: v (float): (series float) - The value to find the arc sine of Returns: (series float) - The arc sine of the input value
atan(v) Arc tangent function Parameters: v (float): (series float) - The value to find the arc tangent of Returns: (series float) - The arc tangent of the input value
atan2(dy, dx) atan2 function Parameters: dy (float): (float) - The y-coordinate dx (float): (float) - The x-coordinate Returns: (float) - The angle in radians
gap(_value1, __value2) Absolute distance between any two float values Parameters: _value1 (float): First value __value2 (float) Returns: Absolute Positive Distance
phi_tol(a, b, tolerance) Check if the ratio is within the tolerance of the golden ratio Parameters: a (float): (float) The first number b (float): (float) The second number tolerance (float): (float) The tolerance percennt as 1 = 1 percent Returns: (bool) True if the ratio is within the tolerance, false otherwise
frac(x) frad Fractional Parameters: x (float): (float) - The number to convert to fractional Returns: (float) - The number converted to fractional
safeindex(x, limit) limiting int to hold the value within the chart range Parameters: x (float): (float) - The number to limit limit (int) Returns: (int) - The number limited to the chart range
safecheck(x, limit) limiting int check if within the chartplottable range Parameters: x (float): (float) - The number to limit limit (int) Returns: (int) - The number limited to the chart range
interpolate(a, b, t) interpolate between two values Parameters: a (float): (float) - The first value b (float): (float) - The second value t (float): (float) - The interpolation factor (0 to 1) Returns: (float) - The interpolated value
gcd(_numerator, _denominator) Greatest common divisor of two integers Parameters: _numerator (int) _denominator (int) Returns: (int) The greatest common divisor
method set_x(self, value) Set the x value of the point, and pass point for chaining Namespace types: Point Parameters: self (Point): (Point) The point to modify value (float): (float) The new x-coordinate
method set_y(self, value) Set the y value of the point, and pass point for chaining Namespace types: Point Parameters: self (Point): (Point) The point to modify value (float): (float) The new y-coordinate
method get_x(self) Get the x value of the point Namespace types: Point Parameters: self (Point): (Point) The point to get the x-coordinate from Returns: (float) The x-coordinate
method get_y(self) Get the y value of the point Namespace types: Point Parameters: self (Point): (Point) The point to get the y-coordinate from Returns: (float) The y-coordinate
method vmin(self) Lowest element of the point Namespace types: Point Parameters: self (Point): (Point) The point Returns: (float) The lowest value between x and y
method vmax(self) Highest element of the point Namespace types: Point Parameters: self (Point): (Point) The point Returns: (float) The highest value between x and y
method add(p1, p2) Addition Namespace types: Point Parameters: p1 (Point): (Point) - The first point p2 (Point): (Point) - The second point Returns: (Point) - the add of the two points
method sub(p1, p2) Subtraction Namespace types: Point Parameters: p1 (Point): (Point) - The first point p2 (Point): (Point) - The second point Returns: (Point) - the sub of the two points
method mul(p, scalar) Multiplication by scalar Namespace types: Point Parameters: p (Point): (Point) - The point scalar (float): (float) - The scalar to multiply by Returns: (Point) - the multiplied point of the point and the scalar
method div(p, scalar) Division by scalar Namespace types: Point Parameters: p (Point): (Point) - The point scalar (float): (float) - The scalar to divide by Returns: (Point) - the divided point of the point and the scalar
method rotate(p, angle) Rotate a point around the origin by an angle (in degrees) Namespace types: Point Parameters: p (Point): (Point) - The point to rotate angle (float): (float) - The angle to rotate by in degrees Returns: (Point) - the rotated point
method length(p) Length of the vector from origin to the point Namespace types: Point Parameters: p (Point): (Point) - The point Returns: (float) - the length of the point
method length_squared(p) Length squared of the vector Namespace types: Point Parameters: p (Point): (Point) The point Returns: (float) The squared length of the point
method normalize(p) Normalize the point to a unit vector Namespace types: Point Parameters: p (Point): (Point) - The point to normalize Returns: (Point) - the normalized point
method dot(p1, p2) Dot product Namespace types: Point Parameters: p1 (Point): (Point) - The first point p2 (Point): (Point) - The second point Returns: (float) - the dot of the two points
method cross(p1, p2) Cross product result (in 2D, this is a scalar) Namespace types: Point Parameters: p1 (Point): (Point) - The first point p2 (Point): (Point) - The second point Returns: (float) - the cross of the two points
method distance(p1, p2) Distance between two points Namespace types: Point Parameters: p1 (Point): (Point) - The first point p2 (Point): (Point) - The second point Returns: (float) - the distance of the two points
method Point(x, y, a, v) Point Create Convenience Namespace types: series float, simple float, input float, const float Parameters: x (float) y (float) a (float) v (float) Returns: (Point) new point
method angle(p1, p2) Angle between two points in degrees Namespace types: Point Parameters: p1 (Point): (Point) - The first point p2 (Point): (Point) - The second point Returns: (float) - the angle of the first point and the second point
method angle_between(p, pivot, other) Angle between two points in degrees from a pivot point Namespace types: Point Parameters: p (Point): (Point) - The point to calculate the angle from pivot (Point): (Point) - The pivot point other (Point): (Point) - The other point Returns: (float) - the angle between the two points
method translate(p, from_origin, to_origin) Translate a point from one origin to another Namespace types: Point Parameters: p (Point): (Point) - The point to translate from_origin (Point): (Point) - The origin to translate from to_origin (Point): (Point) - The origin to translate to Returns: (Point) - the translated point
method midpoint(p1, p2) Midpoint of two points Namespace types: Point Parameters: p1 (Point): (Point) - The first point p2 (Point): (Point) - The second point Returns: (Point) - The midpoint of the two points
method rotate_around(p, angle, pivot) Rotate a point around a pivot point by an angle (in degrees) Namespace types: Point Parameters: p (Point): (Point) - The point to rotate angle (float): (float) - The angle to rotate by in degrees pivot (Point): (Point) - The pivot point to rotate around Returns: (Point) - the rotated point
method multiply(_a, _b) Multiply vector _a with _b Namespace types: Point Parameters: _a (Point): (Point) The first point _b (Point): (Point) The second point Returns: (Point) The result of the multiplication
method divide(_a, _b) Divide vector _a by _b Namespace types: Point Parameters: _a (Point): (Point) The first point _b (Point): (Point) The second point Returns: (Point) The result of the division
method negate(_a) Negative of vector _a Namespace types: Point Parameters: _a (Point): (Point) The point to negate Returns: (Point) The negated point
method perp(_a) Perpendicular Vector of _a Namespace types: Point Parameters: _a (Point): (Point) The point Returns: (Point) The perpendicular point
method vfloor(_a) Compute the floor of argument vector _a Namespace types: Point Parameters: _a (Point): (Point) The point Returns: (Point) The floor of the point
method fractional(_a) Compute the fractional part of the elements from vector _a Namespace types: Point Parameters: _a (Point): (Point) The point Returns: (Point) The fractional part of the point
method vsin(_a) Compute the sine of argument vector _a Namespace types: Point Parameters: _a (Point): (Point) The point Returns: (Point) The sine of the point
lcm(a, b) Least common multiple of two integers Parameters: a (int): (int) The first integer b (int): (int) The second integer Returns: (int) The least common multiple
method vabs(_a) Compute the absolute of argument vector _a Namespace types: Point Parameters: _a (Point): (Point) The point Returns: (Point) The absolute of the point
method vmod(_a, _b) Compute the mod of argument vector _a Namespace types: Point Parameters: _a (Point): (Point) The point _b (float): (float) The mod Returns: (Point) The mod of the point
method vsign(_a) Compute the sign of argument vector _a Namespace types: Point Parameters: _a (Point): (Point) The point Returns: (Point) The sign of the point
method vround(_a) Compute the round of argument vector _a Namespace types: Point Parameters: _a (Point): (Point) The point Returns: (Point) The round of the point
method normalize_y(p, height) normalizes the y value of a point to an input height Namespace types: Point Parameters: p (Point): (Point) - The point to normalize height (float): (float) - The height to normalize to Returns: (Point) - the normalized point
centroid(points) Calculate the centroid of multiple points Parameters: points (array<Point>): (array<Point>) The array of points Returns: (Point) The centroid point
random_point(_height, _width, _origin, _centered) Random Point in a given height and width Parameters: _height (float): (float) The height of the area to generate the point in _width (float): (float) The width of the area to generate the point in _origin (Point): (Point) The origin of the area to generate the point in (default: na, will create a Point(0, 0)) _centered (bool): (bool) Center the origin point in the area, otherwise, positive h/w (default: false) Returns: (Point) The random point in the given area
random_point_array(_origin, _height, _width, _centered, _count) Random Point Array in a given height and width Parameters: _origin (Point): (Point) The origin of the area to generate the array (default: na, will create a Point(0, 0)) _height (float): (float) The height of the area to generate the array _width (float): (float) The width of the area to generate the array _centered (bool): (bool) Center the origin point in the area, otherwise, positive h/w (default: false) _count (int): (int) The number of points to generate (default: 50) Returns: (array<Point>) The random point array in the given area
method sort_points(points, by_x) Sorts an array of points by x or y coordinate Namespace types: array<Point> Parameters: points (array<Point>): (array<Point>) The array of points to sort by_x (bool): (bool) Whether to sort by x-coordinate (true) or y-coordinate (false) Returns: (array<Point>) The sorted array of points
method equals(_a, _b) Compares two points for equality Namespace types: Point Parameters: _a (Point): (Point) The first point _b (Point): (Point) The second point Returns: (bool) True if the points are equal, false otherwise
method max(origin, _a, _b) Maximum of two points from origin, using dot product Namespace types: Point Parameters: origin (Point) _a (Point): (Point) The first point _b (Point): (Point) The second point Returns: (Point) The maximum point
method min(origin, _a, _b) Minimum of two points from origin, using dot product Namespace types: Point Parameters: origin (Point) _a (Point): (Point) The first point _b (Point): (Point) The second point Returns: (Point) The minimum point
method avg_x(points) Average x of point array Namespace types: array<Point> Parameters: points (array<Point>): (array<Point>) The array of points Returns: (float) The average x-coordinate
method avg_y(points) Average y of point array Namespace types: array<Point> Parameters: points (array<Point>): (array<Point>) The array of points Returns: (float) The average y-coordinate
method range_x(points) Range of x values in point array Namespace types: array<Point> Parameters: points (array<Point>): (array<Point>) The array of points Returns: (float) The range of x-coordinates
method range_y(points) Range of y values in point array Namespace types: array<Point> Parameters: points (array<Point>): (array<Point>) The array of points Returns: (float) The range of y-coordinates
method max_x(points) max of x values in point array Namespace types: array<Point> Parameters: points (array<Point>): (array<Point>) The array of points Returns: (float) The max of x-coordinates
method min_y(points) min of x values in point array Namespace types: array<Point> Parameters: points (array<Point>): (array<Point>) The array of points Returns: (float) The min of x-coordinates
method scale(_a, _scalar) Scale a point by a scalar Namespace types: Point Parameters: _a (Point): (Point) The point to scale _scalar (float): (float) The scalar value Returns: (Point) The scaled point
method rescale(_a, _length) Rescale a point to a new magnitude Namespace types: Point Parameters: _a (Point): (Point) The point to rescale _length (float): (float) The new magnitude Returns: (Point) The rescaled point
method rotate_rad(_a, _radians) Rotate a point by an angle in radians Namespace types: Point Parameters: _a (Point): (Point) The point to rotate _radians (float): (float) The angle in radians Returns: (Point) The rotated point
method rotate_degree(_a, _degree) Rotate a point by an angle in degrees Namespace types: Point Parameters: _a (Point): (Point) The point to rotate _degree (float): (float) The angle in degrees Returns: (Point) The rotated point
method vceil(_a, _digits) Ceil a point to a certain number of digits Namespace types: Point Parameters: _a (Point): (Point) The point to ceil _digits (int): (int) The number of digits to ceil to Returns: (Point) The ceiled point
method vpow(_a, _exponent) Raise both point elements to a power Namespace types: Point Parameters: _a (Point): (Point) The point _exponent (float): (float) The exponent Returns: (Point) The point with elements raised to the power
method perpendicular_distance(_a, _b, _c) Distance from point _a to line between _b and _c Namespace types: Point Parameters: _a (Point): (Point) The point _b (Point): (Point) The start point of the line _c (Point): (Point) The end point of the line Returns: (float) The perpendicular distance
method project(_a, _axis) Project a point onto another Namespace types: Point Parameters: _a (Point): (Point) The point to project _axis (Point): (Point) The point to project onto Returns: (Point) The projected point
method projectN(_a, _axis) Project a point onto a point of unit length Namespace types: Point Parameters: _a (Point): (Point) The point to project _axis (Point): (Point) The unit length point to project onto Returns: (Point) The projected point
method reflect(_a, _axis) Reflect a point on another Namespace types: Point Parameters: _a (Point): (Point) The point to reflect _axis (Point): (Point) The point to reflect on Returns: (Point) The reflected point
method reflectN(_a, _axis) Reflect a point to an arbitrary axis Namespace types: Point Parameters: _a (Point): (Point) The point to reflect _axis (Point): (Point) The axis to reflect to Returns: (Point) The reflected point
method angle_rad(_a) Angle in radians of a point Namespace types: Point Parameters: _a (Point): (Point) The point Returns: (float) The angle in radians
method angle_unsigned(_a, _b) Unsigned degree angle between 0 and +180 by given two points Namespace types: Point Parameters: _a (Point): (Point) The first point _b (Point): (Point) The second point Returns: (float) The unsigned angle in degrees
method angle_signed(_a, _b) Signed degree angle between -180 and +180 by given two points Namespace types: Point Parameters: _a (Point): (Point) The first point _b (Point): (Point) The second point Returns: (float) The signed angle in degrees
method angle_360(_a, _b) Degree angle between 0 and 360 by given two points Namespace types: Point Parameters: _a (Point): (Point) The first point _b (Point): (Point) The second point Returns: (float) The angle in degrees (0-360)
method clamp(_a, _vmin, _vmax) Restricts a point between a min and max value Namespace types: Point Parameters: _a (Point): (Point) The point to restrict _vmin (Point): (Point) The minimum point _vmax (Point): (Point) The maximum point Returns: (Point) The restricted point
method lerp(_a, _b, _rate_of_move) Linearly interpolates between points a and b by _rate_of_move Namespace types: Point Parameters: _a (Point): (Point) The starting point _b (Point): (Point) The ending point _rate_of_move (float): (float) The rate of movement (0-1) Returns: (Point) The interpolated point
method slope(p1, p2) Slope of a line between two points Namespace types: Point Parameters: p1 (Point): (Point) - The first point p2 (Point): (Point) - The second point Returns: (float) - The slope of the line
method gety(self, x) Get y-coordinate of a point on the line given its x-coordinate Namespace types: Line Parameters: self (Line): (Line) - The line x (float): (float) - The x-coordinate Returns: (float) - The y-coordinate
method getx(self, y) Get x-coordinate of a point on the line given its y-coordinate Namespace types: Line Parameters: self (Line): (Line) - The line y (float): (float) - The y-coordinate Returns: (float) - The x-coordinate
method intersection(self, other) Intersection point of two lines Namespace types: Line Parameters: self (Line): (Line) - The first line other (Line): (Line) - The second line Returns: (Point) - The intersection point
method calculate_arc_point(self, b, p3) Calculate a point on the arc defined by three points Namespace types: Point Parameters: self (Point): (Point) The starting point of the arc b (Point): (Point) The middle point of the arc p3 (Point): (Point) The end point of the arc Returns: (Point) A point on the arc
approximate_center(point1, point2, point3) Approximate the center of a spiral using three points Parameters: point1 (Point): (Point) The first point point2 (Point): (Point) The second point point3 (Point): (Point) The third point Returns: (Point) The approximate center point
createEdge(center, radius, angle) Get coordinate from center by radius and angle Parameters: center (Point): (Point) - The center point radius (float): (float) - The radius of the circle angle (float): (float) - The angle in degrees Returns: (Point) - The coordinate on the circle
getGrowthFactor(p1, p2, p3) Get growth factor of spiral point Parameters: p1 (Point): (Point) - The first point p2 (Point): (Point) - The second point p3 (Point): (Point) - The third point Returns: (float) - The growth factor
method to_chart_point(point) Convert Point to chart.point using chart.point.from_index(safeindex(point.x), point.y) Namespace types: Point Parameters: point (Point): (Point) - The point to convert Returns: (chart.point) - The chart.point representation of the input point
method plotline(p1, p2, col, width) Draw a line from p1 to p2 Namespace types: Point Parameters: p1 (Point): (Point) First point p2 (Point): (Point) Second point col (color) width (int) Returns: (line) Line object
method drawlines(points, col, ignore_boundary) Draw lines between points in an array Namespace types: array<Point> Parameters: points (array<Point>): (array<Point>) The array of points col (color): (color) The color of the lines ignore_boundary (bool): (bool) The color of the lines
method to_chart_points(points) Draw an array of points as chart points on the chart with line.new(chartpoint1, chartpoint2, color=linecolor) Namespace types: array<Point> Parameters: points (array<Point>): (array<Point>) - The points to draw Returns: (array<chart.point>) The array of chart points
polygon_area(points) Calculate the area of a polygon defined by an array of points Parameters: points (array<Point>): (array<Point>) The array of points representing the polygon vertices Returns: (float) The area of the polygon
polygon_perimeter(points) Calculate the perimeter of a polygon Parameters: points (array<Point>): (array<Point>) Array of points defining the polygon Returns: (float) Perimeter of the polygon
is_point_in_polygon(point, _polygon) Check if a point is inside a polygon Parameters: point (Point): (Point) The point to check _polygon (array<Point>) Returns: (bool) True if the point is inside the polygon, false otherwise
method perimeter(points) Calculates the convex hull perimeter of a set of points Namespace types: array<Point> Parameters: points (array<Point>): (array<Point>) The array of points Returns: (array<Point>) The array of points forming the convex hull perimeter
Point A Point, can be used for vector, floating calcs, etc. Use the cp method for plots Fields: x (series float): (float) The x-coordinate y (series float): (float) The y-coordinate a (series float): (float) An Angle storage spot v (series float): (float) A Value
Line Line Fields: point (Point): (Point) The starting point of the line slope (series float): (float) The slope of the line