admin管理员组文章数量:1318570
I have the coordinates of 2 points A1 (x1,y1) and A2 (x2,y2) and a distance d. I need to find the coordinates of point A3 that is the distance d from point A2 on the linear graph defined by A1 and A2. How can i do that with JavaScript? similar to where the angle is known
I have the coordinates of 2 points A1 (x1,y1) and A2 (x2,y2) and a distance d. I need to find the coordinates of point A3 that is the distance d from point A2 on the linear graph defined by A1 and A2. How can i do that with JavaScript? similar to https://softwareengineering.stackexchange./questions/179389/find-the-new-coordinates-using-a-starting-point-a-distance-and-an-angle where the angle is known
Share Improve this question edited Nov 24, 2017 at 10:18 user24957 asked Nov 24, 2017 at 9:46 user24957user24957 3153 silver badges11 bronze badges2 Answers
Reset to default 9var A1 = {
x : 2,
y : 2
};
var A2 = {
x : 4,
y : 4
};
// Distance
var d= 2;
// Find Slope of the line
var slope = (A2.y-A1.y)/(A2.x-A1.x);
// Find angle of line
var theta = Math.atan(slope);
// the coordinates of the A3 Point
var A3x= A2.x + d * Math.cos(theta);
var A3y= A2.y + d * Math.sin(theta);
console.log(A3x);
console.log(A3y);
So you have to start from A1 and go to A2 direction by the distance between A1 and A2 + d
Assuming your point are objects of a Point class with x and y properties and a distance method you can do this:
function move_to(origin, direction, dist){
let dx = direction.x - origin.x;
let dy = direction.y - origin.y;
let coef = dist / origin.distance(direction);
let x = origin.x + dx * coef;
let y = origin.y + dy *coef;
return new Point(x, y)
}
move_to(A1, A2, A1.distance(A2) + d)
Here's a simple Point class implementation if you want:
class Point {
constructor(x, y){
this.x = x;
this.y = y;
}
distance(point){
return Math.sqrt((this.x - point.x) ** 2 + (this.y - point.y) ** 2)
}
}
版权声明:本文标题:line - How to find coordinates of a point where 2 points and distance are given with Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742047499a2417876.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论