心率监测器

时间:2015-12-03 19:27:50

标签: javascript html5 canvas

好的,所以我按照之前的帖子创建了一个听力率监视器并调整了一下以适应我的网页设计。如何设置Var Data,而不是一遍又一遍地将1到300之间的数字随机化,直到它总共达到200个随机数并绘制它们?谢谢你的时间。这是我的代码,但是我拿出了大部分的Var Data,因为它长了200个数字!

        

        glRotatef(thetaY,crossv2y[0],crossv2y[1],crossv2y[2]);
        glRotatef(thetaZ,crossv3z[0],crossv3z[1],crossv3z[2]);
        glRotatef(thetaX,crossv1x[0],crossv1x[1],crossv1x[2]);

        glTranslatef(-meanX, -meanY, -meanZ);
        glColor3f(1.0f,0.0f,0.0f);
        AOBB(1); //Creates an axis aligned box.

        glRotatef(-thetaX,crossv1x[0],crossv1x[1],crossv1x[2]);
        glRotatef(-thetaZ,crossv3z[0],crossv3z[1],crossv3z[2]);
        glRotatef(-thetaY,crossv2y[0],crossv2y[1],crossv2y[2]);

3 个答案:

答案 0 :(得分:1)

本质将是这样的:一个大小为200的数组,因为你需要200个值,并使用循环随机填充值。

Math.random将生成介于0(含)和1(不包含)之间的数字,乘以300将为您提供0到299.99之间的任何内容... Math.floor()删除小数位(范围)变为0和299);所以我们最后加1来得到你想要的1到300范围。

希望这有帮助

var data = [];
for(var i = 0; i < 200; ++i) {
   data[i] = Math.floor(Math.random() * 300) + 1;
}

答案 1 :(得分:0)

执行此操作的一种简单方法如下:

// creating a new Array with a length of 20 (obviously
// adjust the '20' to '200' for your own use-case):
var arr = new Array(20);

// using Array.prototype.fill() to assign a value (an
// empty String) to each array element, in order that
// Array.prototype.forEach() can iterate over each array
// element:
arr.fill('').forEach(function(needle, i, haystack){
  // the arguments are automagically available to the
  // the anonymous function;
  // needle: the current array-element of the Array,
  // i: the index of the current array-element,
  // haystack: the Array itself.

  // here we set the array value using bracket notation:
  haystack[i] = Math.floor(Math.random() * (300 - 1) + 1);
});

// outputting the array to the console:
console.log( arr );

&#13;
&#13;
var arr = new Array(20);

arr.fill('').forEach(function(needle, i, haystack) {
  haystack[i] = Math.floor(Math.random() * (300 - 1) + 1);
});

// Setting the array as the text of the <body> element,
// given the lack of a Snippet console:
document.body.textContent = arr.join(', ') + '.';
&#13;
&#13;
&#13;

JS Fiddle demo

已发布解决方案的一部分,用于生成范围内的随机数:

Math.floor(Math.random() * (300 - 1) + 1)

实际上借用了网站上其他地方的答案(https://stackoverflow.com/a/1527820/82548撰写的Ionut G. Stan),并在该答案中进行了解释。

然而,简而言之,它会生成0到1之间的随机数;乘以299(300 - 1),使整数部分成为0到299之间的数字;然后添加1以确保整数现在介于1和300之间。

之后我们应用Math.float()将该随机数舍入到数字的整数部分。我上面提到的答案更完整地解释了它。

参考文献:

答案 2 :(得分:0)

优雅的单行:

var data = Array.apply(null, Array(200)).map(function(){ 
    return Math.floor(Math.random() * 299) + 1;
});

以上内容适用于数组中的稀疏/虚假值。