簡體   English   中英

點和序列的boost :: geometry :: within()行為

[英]boost::geometry::within() behavior for point and seqment

我當前使用的是Boost 1.67,並且發現如果我使用boost::geometry::within()來檢查某個點是否在線段內,則不會得到我期望的答案。 例如,我可以構造幾個相交的線段,並使用boost::geometry::intersection()獲取相交點。 我希望該點在每個邊緣內。 但是,這並不總是我所看到的。 這是一些演示我的問題的代碼:

#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

int main() {
    using point_t = boost::geometry::model::d2::point_xy<double>;
    using segment_t = boost::geometry::model::segment<point_t>;
    segment_t e1{point_t{100, 350}, point_t{-100, 400}};
    segment_t e2{point_t{0, 0}, point_t{90, 600}};
    std::vector<point_t> iv;
    boost::geometry::intersection(e1, e2, iv);
    assert(iv.size() == 1);
    const auto& v = iv[0];
    bool is_within = boost::geometry::within(v, e1);
    std::cout << "is within? " << is_within << std::endl;
    is_within = boost::geometry::within(v, e2);
    std::cout << "is within? " << is_within << std::endl;

    return 0;
}

在這種情況下, within()對於兩個邊都返回false。

這個問題幾乎“浮點數學運算符是否已損壞?”重復 ,盡管足夠鮮明,以至於我認為不應將其標記為此類。

您總共有兩個問題:

  • 對於正好位於幾何對象邊界上的點, boost::geometry::within將返回false,這對於一行中的所有點都是如此。 您需要更改策略以允許邊界within的點返回true。
  • 通常,在浮點數學運算過程中,計算兩個點的交點會導致一定程度的誤差。 您不能保證確定為兩條線的交點的點實際上在任一條線上。 您需要在距離上允許一定程度的公差,直到達到增量值(由您定義,我不會超過千分之一),才能將其視為“相交”線。

我將其發布為答案,希望它對將來的其他人有用。 如果您需要檢查一個給定的point位於一個segment ,不使用within() (或covered_by()對於這個問題)。 在對原始問題的評論中,@ rafix07建議了一種解決此問題的更有用的方法:使用boost::geometry::distance(point, segment) 您可以根據自己的情況設置適當的公差,如果測得的距離落在公差之內,則宣告勝利並繼續前進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM