FunctionGenerateRandomPointsInShape
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5

// @description Generate random vector points in geometric shape (parallelogram, triangle)
library(title='FunctionGenerateRandomPointsInShape')

// reference: https://blogs.sas.com/content/iml/2020/10/19/random-points-in-triangle.html

import RicardoSantos/Vector2Operations/1 as vector

// @function Generate random vector point in a parallelogram shape.
// @param vector_a float array, vector of (x, y) shape.
// @param vector_b float array, vector of (x, y) shape.
// @returns float array, vector of (x, y) shape.
export random_parallelogram (float[] vector_a, float[] vector_b) => //{
    // u = [random(), random()] // >(0, 1)<
    vector.add(vector.scale(vector_a, math.random()), vector.scale(vector_b, math.random()))
//{ usage:
// a = vector.new(300.0, 200.0)
// b = vector.new(100.0, -200.0)
// for _i=0 to 40
//     v = random_parallelogram(a, b)
//     label.new(int(bar_index + vector.get_x(v)), vector.get_y(v), ' ', style=label.style_circle)
//}}

// @function Generate random vector point in a triangle shape.
// @param vector_a float array, vector of (x, y) shape.
// @param vector_b float array, vector of (x, y) shape.
// @returns float array, vector of (x, y) shape.
export random_triangle (float[] vector_p1, float[] vector_p2, float[] vector_p3) => //{
    float[] vector_a = vector.subtract(vector_p2, vector_p1)
    float[] vector_b = vector.subtract(vector_p3, vector_p1)
    // u = [random(), random()] // >(0, 1)<
    float _ux = math.random()
    float _uy = math.random()
    if (_ux + _uy) >= 1.0
        _ux := 1.0 - _ux
        _uy := 1.0 - _uy
    vector.add(vector.scale(vector_a, _ux), vector.scale(vector_b, _uy))
//{ usage:
a = vector.new(100.0, 200.0)
b = vector.new(400.0, 400.0)
c = vector.new(200.0, 000.0)
for _i=0 to 40
    v = random_triangle(a, b, c)
    label.new(int(bar_index + vector.get_x(v)), vector.get_y(v), ' ', style=label.style_circle)
//}}