如何在 javascript 中将 2d 点投影到 2d 轴上
How to project a 2d point onto a 2d axis in javascript
我有一个定义为 {x:(x value), y: (y value)}
的点,并且我有一个带有斜率和 y 截距的轴。我正在尝试将点投影到线上。我搜索了 'project point onto line' 并环顾了很长时间,但我找不到任何将点投影到斜率和截距线上的东西。
实际答案很简单。我只需要与接触投影点的另一条线做一条垂直线。那么,投影就是两条线的交点。所以我在javascript中实现了一个函数,参数是点的x,点的y,直线的斜率,直线的y截距,它returns投影为{x, y}
。
function project (x, y, slope, yint) {
var slope2 = -1 / slope;
var yint2 = y - slope2 * x;
var nx = (yint2 - yint) / (slope - slope2);
return {x: nx, y: (slope2 * nx) + yint}; //thanks to @igobivo for fixing that mistake
}
此代码对我有用(与 OP 的回答略有不同):
function project (x, y, slope, yint) {
var slope2 = -1 / slope;
var yint2 = y - slope2 * x;
var nx = (yint2 - yint) / (slope - slope2);
return {x: nx, y: (slope2 * nx) + yint};
}
我有一个定义为 {x:(x value), y: (y value)}
的点,并且我有一个带有斜率和 y 截距的轴。我正在尝试将点投影到线上。我搜索了 'project point onto line' 并环顾了很长时间,但我找不到任何将点投影到斜率和截距线上的东西。
实际答案很简单。我只需要与接触投影点的另一条线做一条垂直线。那么,投影就是两条线的交点。所以我在javascript中实现了一个函数,参数是点的x,点的y,直线的斜率,直线的y截距,它returns投影为{x, y}
。
function project (x, y, slope, yint) {
var slope2 = -1 / slope;
var yint2 = y - slope2 * x;
var nx = (yint2 - yint) / (slope - slope2);
return {x: nx, y: (slope2 * nx) + yint}; //thanks to @igobivo for fixing that mistake
}
此代码对我有用(与 OP 的回答略有不同):
function project (x, y, slope, yint) {
var slope2 = -1 / slope;
var yint2 = y - slope2 * x;
var nx = (yint2 - yint) / (slope - slope2);
return {x: nx, y: (slope2 * nx) + yint};
}