# Code coverage report for src/math/Value.js

# all files / src/math/ Value.js

41.51% Statements 22/53
0% Branches 0/6
5.88% Functions 1/17
41.51% Lines 22/53

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144

    1×

    1×

    1×

    1×
    1×

    1×

    1×
    1×

    1×

    1×
    1×

    1×

    1×
    1×

    1×

    1×
    1×

    1×

    1×

    1×

    1×

    1×

     import Vector3 from './Vector3';
    import Vector2 from './Vector2';

    /**
     * Random or constant 1d, 2d, 3d vector generator
     * @constructor
     * @alias clay.Value
     */
    var Value = function() {};

    /**
     * @function
     * @param {number|clay.Vector2|clay.Vector3} [out]
     * @return {number|clay.Vector2|clay.Vector3}
     */
    Value.prototype.get = function(out) {};

    // Constant
    var ConstantValue = function(val) {
        this.get = function() {
            return val;
        };
    };
    ConstantValue.prototype = new Value();
    ConstantValue.prototype.constructor = ConstantValue;

    // Vector
    var VectorValue = function(val) {
        var Constructor = val.constructor;
        this.get = function(out) {
            if (!out) {
                out = new Constructor();
            }
            out.copy(val);
            return out;
        };
    };
    VectorValue.prototype = new Value();
    VectorValue.prototype.constructor = VectorValue;
    //Random 1D
    var Random1D = function(min, max) {
        var range = max - min;
        this.get = function() {
            return Math.random() * range + min;
        };
    };
    Random1D.prototype = new Value();
    Random1D.prototype.constructor = Random1D;

    // Random2D
    var Random2D = function(min, max) {
        var rangeX = max.x - min.x;
        var rangeY = max.y - min.y;

        this.get = function(out) {
            if (!out) {
                out = new Vector2();
            }
            Vector2.set(
                out,
                rangeX * Math.random() + min.array[0],
                rangeY * Math.random() + min.array[1]
            );

            return out;
        };
    };
    Random2D.prototype = new Value();
    Random2D.prototype.constructor = Random2D;

    var Random3D = function(min, max) {
        var rangeX = max.x - min.x;
        var rangeY = max.y - min.y;
        var rangeZ = max.z - min.z;

        this.get = function(out) {
            if (!out) {
                out = new Vector3();
            }
            Vector3.set(
                out,
                rangeX * Math.random() + min.array[0],
                rangeY * Math.random() + min.array[1],
                rangeZ * Math.random() + min.array[2]
            );

            return out;
        };
    };
    Random3D.prototype = new Value();
    Random3D.prototype.constructor = Random3D;

    // Factory methods

    /**
     * Create a constant 1d value generator
     * @param  {number} constant
     * @return {clay.Value}
     */
    Value.constant = function(constant) {
        return new ConstantValue(constant);
    };

    /**
     * Create a constant vector value(2d or 3d) generator
     * @param  {clay.Vector2|clay.Vector3} vector
     * @return {clay.Value}
     */
    Value.vector = function(vector) {
        return new VectorValue(vector);
    };

    /**
     * Create a random 1d value generator
     * @param  {number} min
     * @param  {number} max
     * @return {clay.Value}
     */
    Value.random1D = function(min, max) {
        return new Random1D(min, max);
    };

    /**
     * Create a random 2d value generator
     * @param  {clay.Vector2} min
     * @param  {clay.Vector2} max
     * @return {clay.Value}
     */
    Value.random2D = function(min, max) {
        return new Random2D(min, max);
    };

    /**
     * Create a random 3d value generator
     * @param  {clay.Vector3} min
     * @param  {clay.Vector3} max
     * @return {clay.Value}
     */
    Value.random3D = function(min, max) {
        return new Random3D(min, max);
    };

    export default Value;