球面上随机生成的3D点

时间:2018-09-17 13:11:18

标签: javascript random 3d effect demo

我正在生成3D点并对其进行3D旋转:

var Points = [] ;
for (var i=0 ; i < 20 ; i++) {
    Points[i] = [
        Math.floor(Math.random()*256),
        Math.floor(Math.random()*256),
        Math.floor(Math.random()*256)
    ] ;
}
Process3DRotation() ;

但是如何在这样的隐藏式快门上随机生成3D点:

enter image description here

1 个答案:

答案 0 :(得分:1)

好的,这里是简单的代码,可以在球体上均匀采样。有关其背后的理论,请查看http://mathworld.wolfram.com/SpherePointPicking.html

var radius = 10. ;
var Points = [] ;
for (var i=0 ; i < 20 ; i++) {
    var phi  = 2. * 3.1415926 * Math.random();
    var csth = 1.0 - 2.0 * Math.random();
    var snth = Math.sqrt(1.0 - csth*csth);
    Points[i] = [
        radius * snth * Math.cos(phi),
        radius * snth * Math.sin(phi),
        radius * csth
    ] ;
}
相关问题