Boost.Geometry 找不到多边形线交点的第二个点

Boost.Geometry doesn't find second point of polygon-line intersecion

我正在尝试使用 Boost.Geometry 库来查找正方形和直线的交点,

model::ring<model::d2::point_xy<double>> ring { {0, 0}, {2, 0}, {2, 2}, {0, 2} };
model::polygon<model::d2::point_xy<double>> pol;
pol.inners().push_back (ring);

model::linestring<model::d2::point_xy<double>> line { {1, 3}, {-1, -1} };

model::multi_point<model::d2::point_xy<double>> out;

intersection (pol, line, out);  //out returns only {0.5, 2}, but not {0, 1}

但是returns只有一个点,虽然实际上有两个交点

如何找到所有交点?

合上戒指并按预期顺序放置(默认顺时针,see default template parameters):

model::ring<model::d2::point_xy<double>> ring {
    {0, 0}, {0, 2}, {2, 2}, {2, 0}, {0, 0}
};

您的戒指无效,即未满足指定模板参数的要求。

As per documentation (see under rules) 使用无效的几何图形作为输入可能会给出错误的结果,算法既不会检查也不会更正有效性。

环也不会在构造时或首次使用前自动关闭(它怎么知道你不会添加更多点数?)。 Here 是具有重复关闭点的示例构造。

但是 is_valid and correct 可以解决这个问题。

您可能还想将点推到外环,即 pol.outer()。您的多边形需要有一个外环,内环决定孔。可以直接构造没有内环的多边形:

model::polygon<model::d2::point_xy<double>> pol {
    { {0, 0}, {0, 2}, {2, 2}, {2, 0}, {0, 0} }
};