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

问题描述 投票:1回答:1

我正在尝试使用Boost.Geometry库来查找square和line的交集,

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}

但它只返回一个点,虽然实际上有两个交点

enter image description here

我怎样才能找到所有的交点?

c++ boost geometry boost-geometry
1个回答
2
投票

关闭你的戒指并按预期顺序(默认顺时针,see default template parameters):

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

你的戒指无效,i。不满足指定模板参数的要求。

使用无效几何作为输入的As per documentation (see under rules)可能会给出错误的结果,并且不检查有效性,也不会通过算法进行校正。

戒指在施工时或首次使用前也不会自动关闭(如何知道你不会追加更多积分?)。 Here是一个具有重复关闭点的示例构造。

然而,is_validcorrect可以解决这个问题。

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

model::polygon<model::d2::point_xy<double>> pol {
    { {0, 0}, {0, 2}, {2, 2}, {2, 0}, {0, 0} }
};
© www.soinside.com 2019 - 2024. All rights reserved.