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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值