在球体表面上随机生成3D点

问题描述 投票:0回答:1

我正在生成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

javascript random 3d effect demo
1个回答
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
    ] ;
}
© www.soinside.com 2019 - 2024. All rights reserved.