Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Graphics 二维图形中两条直线之间的距离_Graphics_Geometry - Fatal编程技术网

Graphics 二维图形中两条直线之间的距离

Graphics 二维图形中两条直线之间的距离,graphics,geometry,Graphics,Geometry,我们有两行(A和B-见下图)。线A从p1开始到p2结束线B从p3开始到p4结束 点p5是线A与线B相交的点(如果延伸) 如何计算p2和p5之间的距离 编辑: 更清楚地说,我如何找到p5点 致以最良好的祝愿 使用创建两个方程式。然后,使用查找距离。通过 // each pair of points decides a line // return their intersection point Point intersection(const Point& l11, const Poi

我们有两行(A和B-见下图)。线A从p1开始到p2结束线B从p3开始到p4结束

点p5是线A与线B相交的点(如果延伸)

如何计算p2和p5之间的距离

编辑: 更清楚地说,我如何找到p5点


致以最良好的祝愿

使用创建两个方程式。然后,使用查找距离。

通过

// each pair of points decides a line
// return their intersection point
Point intersection(const Point& l11, const Point& l12, const Point& l21, const Point& l22)
{
  double a1 = l12.y-l11.y, b1 = l11.x-l12.x;
  double c1 = a1*l11.x + b1*l11.y;
  double a2 = l22.y-l21.y, b2 = l21.x-l22.x;
  double c2 = a2*l21.x + b2*l21.y;
  double det = a1*b2-a2*b1;
  assert(fabs(det) > EPS);
  double x = (b2*c1-b1*c2)/det;
  double y = (a1*c2-a2*c1)/det;

  return Point(x,y);
}

// here is the point class
class Point
{
public:
  double x;
  double y;
public:
  Point(double xx, double yy)
    : x(xx), y(yy)
  {}

  double dist(const Point& p2) const 
  {
    return sqrt((x-p2.x)*(x-p2.x) + (y-p2.y)*(y-p2.y));
  }
};

然后你可以打电话给p2.dist(p5)来获得距离。

你找不到p5,或者找不到p2和p5之间的距离?我在纸上写下了答案。