Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Math 当我知道与中心的角度时,计算矩形边缘的位置_Math_Three.js - Fatal编程技术网

Math 当我知道与中心的角度时,计算矩形边缘的位置

Math 当我知道与中心的角度时,计算矩形边缘的位置,math,three.js,Math,Three.js,我试图计算下图中的位置2 我从你那里知道第一个职位 this._end = new THREE.Vector3() this._end.copy( this._rectanglePos ) .sub( this._circlePos ).setLength( 1.1 ).add( this._circlePos ) 其中圆的半径为2.2 我现在试着在这个相交点的矩形边上计算出一个位置 我已经把它转换成了这个函数 function positionAtEdge(phi, w

我试图计算下图中的位置2

我从你那里知道第一个职位

  this._end = new THREE.Vector3()
  this._end.copy( this._rectanglePos )
     .sub( this._circlePos ).setLength( 1.1 ).add( this._circlePos )
其中圆的半径为2.2

我现在试着在这个相交点的矩形边上计算出一个位置

我已经把它转换成了这个函数

 function positionAtEdge(phi, width, height){
   let c = Math.cos(phi)
   let s = Math.sin(phi)
   let x = width/2
   let y = height/2

    if (width * Math.abs(s) < height * Math.abs(c)){
        x -= Math.sign(c) * width / 2
        y -= Math.tan(phi) * x
    } 
    else{
        y -= Math.sign(s) * height / 2
        x -= cot(phi) * y
    }

    return {x, y, z: 0}

    function cot(aValue){
        return 1/Math.tan(aValue);
    }
}
功能位置边缘(φ、宽度、高度){
设c=Math.cos(φ)
设s=Math.sin(φ)
设x=宽度/2
设y=高度/2
如果(宽度*数学abs(s)<高度*数学abs(c)){
x-=数学符号(c)*宽度/2
y-=数学tan(φ)*x
} 
否则{
y-=数学符号)*高度/2
x-=cot(φ)*y
}
返回{x,y,z:0}
功能cot(aValue){
返回1/Math.tan(aValue);
}
}
这种方法适用于矩形的顶部,但90度后开始抛出疯狂的值。数学没有coTan函数,所以我在谷歌上搜索了一下,就认为这是cot函数


任何人都知道找到这个位置2的更简单的方法,或者如何将这个函数转换成可用的东西

这不需要任何超越函数

Vsb=(球体中心-矩形中心)


P2=矩形中心+((vsb*矩形高度*.5)/vsb.y)

这不需要任何超越函数

Vsb=(球体中心-矩形中心)


P2=矩形中心+((vsb*矩形高度*.5)/vsb.y)

这是一个通用解决方案,与它们的相对位置无关


这是一个通用解决方案,与它们的相对位置无关


这是伪代码。向量+-*等当然必须是JS函数,这是伪代码。vector+-*等当然必须是JS函数。正是像你这样的人让互联网变得惊人。我不建议在一个紧密的循环中使用
clone()
。相反,作为函数参数传入
offset
,并执行
offset.copy(circle).sub(rectangle)
。我实际上是为了这个目的而在范围之外初始化了offset,但后来忘了修改它。更新答案和实例。谢谢。正是像你这样的人让互联网变得神奇。我不建议在一个严密的循环中使用
clone()
。相反,作为函数参数传入
offset
,并执行
offset.copy(circle).sub(rectangle)
。我实际上是为了这个目的而在范围之外初始化了offset,但后来忘了修改它。更新答案和实例。非常感谢。
function getIntersection( circle, rectangle, width, height ) {

    // offset is a utility Vector3.
    // initialized outside the function scope.
    offset.copy( circle ).sub( rectangle );

    let ratio = Math.min( 
        width * 0.5 / Math.abs( offset.x ),
        height * 0.5 / Math.abs( offset.y )
    );

    offset.multiplyScalar( ratio ).add( rectangle );

    return offset;

}