【问题标题】:Three.js calculate position at edge of rectangle when I know the angle from center当我知道与中心的角度时, Three.js 计算矩形边缘的位置
【发布时间】:2020-05-16 15:53:36
【问题描述】:

我正在尝试计算下图中的位置 2。

我从

知道位置 1
  this._end = new THREE.Vector3()
  this._end.copy( this._rectanglePos )
     .sub( this._circlePos ).setLength( 1.1 ).add( this._circlePos )

圆的半径是2.2

我现在正试图沿着这个交叉点计算矩形边缘的位置。

我把 found an equation written in pseudo code 变成了这个函数

 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);
    }
}

这种方法适用于矩形的顶部,但在 90 度后开始抛出疯狂的值。数学没有 coTan 函数,所以我通过谷歌搜索假设他们的意思是这个 cot 函数。

任何人都知道找到此位置 2 的更简单方法或如何将此功能转换为可用的东西。

【问题讨论】:

    标签: math three.js


    【解决方案1】:

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

    Live Example (JSFiddle)

    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;
    
    }
    

    【讨论】:

    • 像你这样的人让互联网变得如此神奇。
    • 我会避免建议在紧密循环中使用clone()。相反,将offset 作为函数参数传入并执行offset.copy( circle ).sub( rectangle )
    • 为了这个确切的目的,我实际上已经在范围之外初始化了偏移量,但后来忘记了修改它。更新了答案和活生生的例子。谢谢。
    【解决方案2】:

    你不需要任何超越函数。

    Vsb = (Spherecenter - rectanglecenter)

    P2 = rectanglecenter + ((vsb * rectangleheight * .5) / vsb.y)

    【讨论】:

    • 那是伪代码。向量 +-* 等当然必须是 JS 函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    相关资源
    最近更新 更多