Threejs Line3.js 中 closestPointToPointParameter 函数的意义

<h4> Line3.js中 closestPointToPointParameter() 函数意义 ;返回一个基于点投影到线段上的点的参数(就是参数point投影到线段的位置),
   <hr> 如果 clampToLine  为true,那么返回值将是0和1之间</h4>
<script>
    /*-----------------  函数简单测试 -----------------*/
    function closestPointToPointParameter(point) {
        this.start = new Vector3(0,0,0);
        this.end = new Vector3(2,0,0);

        var startP = new Vector3();
        var startEnd = new Vector3();
        startP.subVectors(point,this.start);
        startEnd.subVectors(this.end,this.start);
        /* a,b两向量点积 |a|*|b|*cosθ; 由于它们的角度为0,所以cosθ为1;
        *  a * b  =  |a|*|b|;得到的是两个长相乘
        * */
        var startEnd2 = startEnd.dot(startEnd);
        console.log(startEnd2);
        /*它两的点积得到的是两个向量所形成的矩形面积
        * */
        var startEnd_startP  = startEnd.dot(startP);
        console.log(startEnd_startP);
    
        var t = startEnd_startP / startEnd2;
        /* // t大于1返回1,小于0返回0,在0 - 1范围间返回它本身
           if ( clampToLine ) {

            t = THREE.Math.clamp( t, 0, 1 );

         }
        * */
        console.log(t);
    }
    var Vector3 = function(x,y,z){
        this.x = x;
        this.y = y;
        this.z = z;
    }
    Vector3.prototype.subVectors = function(a,b){
        this.x = a.x - b.x;
        this.y = a.y - b.y;
        this.z = a.z - b.z;
        return this;
    }
    Vector3.prototype.dot = function(v){
        return this.x * v.x +this.y * v.y + this.z * v.z;
    }
    var point = new Vector3(1,1,0);
    closestPointToPointParameter(point);
</script>

猜你喜欢

转载自blog.csdn.net/qq_25909453/article/details/82427715