Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算与直线垂直的点_Java_Javascript - Fatal编程技术网

Java 计算与直线垂直的点

Java 计算与直线垂直的点,java,javascript,Java,Javascript,我有两个点存储在两个变量中,形成一条直线。我想找到一个点,从这条线的一个端点开始,垂直于这条线 假设我有两个点P1(x1,y1)和P2(x2,y2),然后我想找到第三个点P3,这样线(P1-P2)垂直于线(P2,P3)并在P2处相交 首先,角度: public static double angle (double x1, double y1, double x2, double y2) { double xdiff = x1 - x2; double ydiff = y1 -

我有两个点存储在两个变量中,形成一条直线。我想找到一个点,从这条线的一个端点开始,垂直于这条线

假设我有两个点P1(x1,y1)和P2(x2,y2),然后我想找到第三个点P3,这样线(P1-P2)垂直于线(P2,P3)并在P2处相交

首先,角度:

public static double angle (double x1, double y1, double x2, double y2) {
    double xdiff = x1 - x2;
    double ydiff = y1 - y2;
    //double tan = xdiff / ydiff;
    double atan = Math.atan2(ydiff, xdiff);
    return atan;
}
要获得垂直,必须将PI/2添加到两点定义的直线角度

获得该角度后,公式为:

x = interceptPt.x + sin(perp_angle) * distance;
y = interceptPt.y + cos(perp_angle) * distance;
首先,角度:

public static double angle (double x1, double y1, double x2, double y2) {
    double xdiff = x1 - x2;
    double ydiff = y1 - y2;
    //double tan = xdiff / ydiff;
    double atan = Math.atan2(ydiff, xdiff);
    return atan;
}
要获得垂直,必须将PI/2添加到两点定义的直线角度

获得该角度后,公式为:

x = interceptPt.x + sin(perp_angle) * distance;
y = interceptPt.y + cos(perp_angle) * distance;
我终于得到了答案

我终于得到了答案


您可以将点存储在
vec2d
中,然后使用一些数学方程来获得垂直点

vec2d getPerpendicularPoint(vec2d A, vec2d B, float distance)
{
   vec2d M = (A + B) / 2;
   vec2d p = A - B;
   vec2d n = (-p.y, p.x);
   int norm_length = sqrt((n.x * n.x) + (n.y * n.y));
   n.x /= norm_length;
   n.y /= norm_length;
   return (M + (distance * n));
}

您可以将点存储在
vec2d
中,然后使用一些数学方程来获得垂直点

vec2d getPerpendicularPoint(vec2d A, vec2d B, float distance)
{
   vec2d M = (A + B) / 2;
   vec2d p = A - B;
   vec2d n = (-p.y, p.x);
   int norm_length = sqrt((n.x * n.x) + (n.y * n.y));
   n.x /= norm_length;
   n.y /= norm_length;
   return (M + (distance * n));
}

如果您想使用Java,我可以推荐您使用。创建并使用pointAlongOffset方法。给定p1点和p2点,代码如下所示:

// create LineSegment
LineSegment ls = new LineSegment(p1.getX(), p1.getY(), p2.getX(), p2.getY());
// perpendicular distance to line
double offsetDistance = 10;
// calculate Point right to start point
Coordinate startRight = ls.pointAlongOffset(0, offsetDistance);
// calculate Point left to start point
Coordinate startLeft = ls.pointAlongOffset(0, -offsetDistance);
// calculate Point right to end point
Coordinate endRight = ls.pointAlongOffset(1, offsetDistance);
// calculate Point left to end point
Coordinate endLeft = ls.pointAlongOffset(1, -offsetDistance);

如果您想使用Java,我可以推荐您使用。创建并使用pointAlongOffset方法。给定p1点和p2点,代码如下所示:

// create LineSegment
LineSegment ls = new LineSegment(p1.getX(), p1.getY(), p2.getX(), p2.getY());
// perpendicular distance to line
double offsetDistance = 10;
// calculate Point right to start point
Coordinate startRight = ls.pointAlongOffset(0, offsetDistance);
// calculate Point left to start point
Coordinate startLeft = ls.pointAlongOffset(0, -offsetDistance);
// calculate Point right to end point
Coordinate endRight = ls.pointAlongOffset(1, offsetDistance);
// calculate Point left to end point
Coordinate endLeft = ls.pointAlongOffset(1, -offsetDistance);

应该有更多的约束(至少有一个类似于与P1P2的距离),因为您提到的条件适用于位于垂直于P1P2并穿过P2的直线上的任何点。垂直点与直线之间的距离,它是任意选择的吗?你在这里找到解决方案了吗?应该有更多的约束(至少有一个类似于P1P2的距离),因为你提到的条件适用于位于垂直于P1P2并穿过P2的直线上的任何点。垂直点和直线之间的距离,它是任意选择的吗?你在这里找到解决方案了吗?刚刚得到一个ans示例,所以我将其转换为Java程序。但是我分享了一个原始的例子,几乎没有什么变化。我得到了一个例子,所以我把它转换成了Java程序。但是我分享了一个原始的例子,几乎没有什么变化。请再详细一点,公式中涉及的变量是谁?你所说的“两点定义的直线角度”是什么意思还不是很清楚。点1由点坐标(x1,y1)定义,点2由点坐标(x2,y2)定义。点1和点2可以一起解释为一条直线的点。你能再详细说明一下公式中涉及的变量是谁吗?你所说的“两点定义的直线角度”是什么意思还不是很清楚。点1由点坐标(x1,y1)定义,点2由点坐标(x2,y2)定义。点1和点2一起可以解释为直线的点