找到线和高度之间的交点

时间:2014-12-29 20:14:28

标签: java geometry point

我试图找到一条线与该线的高度之间的交点。所以给定信息,两个点创建一条线和一个点来绘制高度,我想找到最接近点的线上的点,(因此线上的一个点形成一条垂直线与给定点。)

(希望在没有注册的情况下有意义)

给出一条线和一条点,我怎么能找到这个新的点?

public static Point2D findPointOnTwoLines(Line2D line, Point2D point) {
    double slope = line.getSlope();
    double oppRec = line.getOppRecip(); // returns the opposite reciprocal of the slope
    double x = ((slope * line.getPoint1().getX()) - (line.getPoint1().getY())) 
            / ((slope) - (oppRec));
    double y = (slope * ((oppRec * point.getX()) - point.getY())) - (oppRec * ((slope * point.getX()) - point.getY()))
            / ((slope) - (oppRec));

    return new Point2D(x, y);
}

这是我尝试使用确定数求解方程式,但是当我通过时它未能给出正确的坐标:

Line2D line = new Line2D(2, 3, 1, 1); // line from (2, 3) to (1, 1)
Point2D point = new Point2D(0, 2);

如果您知道如何使用此方法找到正确的点,(或任何其他方法),将非常感谢!!

ASKII艺术的意思,如果你能把这个真实的形象发送给我,我会乐意使用它。

1
 \       3
  \     /    
   \   /      the number points are thrown in as arguments, and the "X" point is returned
    \ /
     X    <---- this is a 90 degree angle
      \
       \        the "X" is suppose to represent the point on the line closest to the point "3"
        \
         2

1 个答案:

答案 0 :(得分:1)

double x = (point.getY() - oppRec * point.getX() - line.getPoint1().getY() + slope * line.getPoint1().getX()) / (slope - oppRec);
double y = oppRec * x + point.getY() - oppRec * point.getX();