计算两点之间形成的角度和方向

角度计算

function swipeAngle(x1, x2, y1, y2) {
    var xDelta, yDelta, r, angle;
    xDelta = x2 - x1;
    yDelta = y2 - y1;
    r = Math.atan2(yDelta, xDelta);
    angle = Math.round(r * 180 / Math.PI);
    angle = angle < 0 ? Math.abs(angle) : (360 - angle);
    return angle;
};

方向计算(快速方法):

function swipeDirection(x1, x2, y1, y2) {
    var xDelta = Math.abs(x1 - x2),
    yDelta = Math.abs(y1 - y2);
    return xDelta >= yDelta ? (x1 - x2 > 0 ? 'left' : 'right') : (y1 - y2 > 0 ? 'up' : 'down');
}


如需转载,请注明出处: https://chadou.me/p/145

最新发布