如何从角度获得矩形圆周点

时间:2017-12-17 16:07:56

标签: javascript math

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

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

    return ???
}

1 个答案:

答案 0 :(得分:0)

假设参照系以矩形中心为中心。可以通过首先确定矩形的哪一侧受角度的特定值“影响”然后计算丢失的第二坐标来进行:

df = df.pivot_table(index='indexer', columns='Field', values='Value', aggfunc=','.join)

然后打印:

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