簡體   English   中英

Boost :: Geometry如何獲取多邊形內點的坐標?

[英]Boost::Geometry how to get the coordinates of a point inside a polygon?

我正在使用boost :: geometry庫對一個給出先前定義的多邊形直徑的函數進行編碼。

該直徑定義為其兩個點之間的最大距離。 因此,我正在編碼一個雙循環,計算每對點的每一個距離,但是我不知道如何訪問多邊形內部甚至點對象內的點坐標,然后使用兩個點之間的距離函數通過庫(順便說一下,哪個應該更快?)。

在查看了關於多邊形的Boost文檔之后,我嘗試了my_polygon.PointList,但沒有成功...

最后,我的解決方案是使用Barend提案的修改版本:

for(auto it1 = boost::begin(boost::geometry::exterior_ring(poly)); 
    it1 != boost::end(boost::geometry::exterior_ring(poly));
    ++it1)
{
    for(auto it2 = it1+1; 
        it2 != boost::end(boost::geometry::exterior_ring(poly));
        ++it2)
   {
       double distance = boost::geometry::distance(*it1, *it2);
       if(my_diameter < distance){my_diameter = distance;}
   }
}

我只是抑制了計算兩次相同距離的重復性;

多邊形由環組成。 您要有外圈(外圈)。 可以使用exterior_ring(aPolygon);訪問。

因此,您可以使用類似以下代碼的代碼來迭代多邊形的點(為簡單起見,我使用auto,否則聲明一個迭代器):

for(auto it1 = boost::begin(boost::geometry::exterior_ring(poly)); 
    it1 != boost::end(boost::geometry::exterior_ring(poly));
    ++it1)
{
    for(auto it2 = boost::begin(boost::geometry::exterior_ring(poly)); 
        it2 != boost::end(boost::geometry::exterior_ring(poly));
        ++it2)
   {
       // You might skip cases where it1==it2, distance is zero anyway
       double distance = boost::geometry::distance(*it1, *it2);
       // Compare with a max distance, if larger, assign, etc.
   }
}

(循環通常還會在內圈上循環,但是對於直徑,如果多邊形定義明確,則不必這樣做)。

順便說一句,關於您的問題:PointList是模板參數的名稱(請參閱doc)。 成員函數是external(),用於外圈。 上面的代碼使用一個免費函數“ exterior_ring”來使用Polygon Concept,該概念應適用於Boost.Geometry中的任何多邊形類型。

您只能檢查彼此之間的“角”距離,因為最大距離將位於兩個“角”之間。

僅用一點“ A”和一個分段來考慮它。 您會看到,無論如何放置線段,與A的最大距離的線段的點都是兩端之一。

順便說一句,每個點? 以什么粒度? 無限多!

暫無
暫無

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

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