gpt4 book ai didi

javascript - 根据距离和方向计算坐标

转载 作者:行者123 更新时间:2023-11-30 14:04:21 28 4
gpt4 key购买 nike

这是我的便捷方案:

enter image description here

给定:

  • 这是二维的,不用担心第三轴
  • ABAB 都具有 xy 坐标
  • width,到每个 Angular 的距离:width/2
  • direction 度数(0 向上,90 向右,180 向下等)

如何计算 Angular 1-4xy 坐标?


const A = {x: 0, y: 0};
const B = {x: 10, y: 0};
const direction = 90;
const width = 10;
const halfWidth = width / 2;

// tried the following one that I found but
// no lock, I assume somethings off with angles
function getCorner(point, angle, length) {
return {
x: point.x + Math.cos(angle) * length,
y: point.y + Math.sin(angle) * length
};
}

// EXPECTED
// bottom left: {x: 0, y: 5}
// bottom right: {x: 0, y: -5}
// top left: {x: 10, y: 5}
// top right: {x: 10, y: -5}

console.log(
"bottom left:",
getCorner(A, direction - 90, halfWidth)
);
console.log(
"bottom right:",
getCorner(A, direction + 90, halfWidth)
);

console.log("---");

console.log(
"top left:",
getCorner(B, direction - 90, halfWidth)
);
console.log(
"top right:",
getCorner(B, direction + 90, halfWidth)
);

最佳答案

通过提示Y轴向上,可以简单解决如下:

enter image description here

我们看到每个 Angular 点都位于 AB 线段周围 +/- 90 度 Angular 。考虑下图,很容易计算出 dxdy 值(从 A 或 B 到达每个 Angular 点):

dx = (W/2).sin(alpha)
dy = (W/2).cos(alpha)

其中 alpha 等于 90 - direction。因此,我们可以编写如下代码片段的js代码:

const A = {x: 0, y: 0};
const B = {x: 10, y: 0};
const direction = 90;
const width = 10;

var length = width / 2;
var angle = (90 - direction) * (Math.PI / 180);

var dx = Math.sin(angle) * length;
var dy = Math.cos(angle) * length;

console.log( "bottom left:", {x: A.x - dx, y: A.y + dy} );
console.log( "bottom right:", {x: A.x + dx, y: A.y - dy} );
console.log("---");
console.log( "top left:", {x: B.x - dx, y: B.y + dy} );
console.log( "top right:", {x: B.x + dx, y: B.y - dy} );

关于javascript - 根据距离和方向计算坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55722999/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com