【问题标题】:How to get rectangle circumference point from angle如何从角度获取矩形圆周点
【发布时间】:2017-12-17 16:07:56
【问题描述】:

如何获取矩形的圆周点及其大小和角度?

function getRectangleCircumferencePoint(rectangle ,angle) {
    // rectangle.width
    // rectangle.height
    // angle

    return ???
}

【问题讨论】:

  • 如果您在此处发布的代码有问题,也许可以this help。但你需要先做你自己的代码

标签: javascript math


【解决方案1】:

假设参考系以矩形中心为中心。可以先确定矩形的哪一侧受到特定角度值的“影响”,然后计算缺失的第二个坐标:

W = 8;
H = 4;

function point(W, H, angle){

    var phi = Math.abs(Math.atan2(H, W));

    //transform angle into interval [0, 2*PI)
    var alpha = angle - Math.PI*2*Math.floor(angle / (Math.PI*2));
    var x, y;

    if(alpha <= phi || alpha >= 2*Math.PI-phi){ //right border
        x = +W/2;
    }else if(alpha >= Math.PI - phi && alpha <= Math.PI + phi){ //left border
        x = -W/2;
    }else if(alpha > phi && alpha < Math.PI - phi){ //top border
        y = +H/2;
    }else{ //bottom border
        y = -H/2;
    }

    if(x === undefined){
        //alpha might be here pi/2 so it's probably better to expand tan(alpha) rather
        //than divide by "+inf"
        x = y * Math.cos(alpha) / Math.sin(alpha);
    }else if(y === undefined){
        y = x * Math.tan(alpha);
    }

    return [x, y];
}

N = 4;
for(var i = 0; i < N; ++i){
    var alpha = i*2*Math.PI/N;
    var pnt = point(W, H, alpha);
    console.log(pnt[0].toFixed(8), pnt[1].toFixed(8));
}

然后打印:

4.00000000 0.00000000
0.00000000 2.00000000
-4.00000000 0.00000000
-0.00000000 -2.00000000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多