是否有可能获得与boost
的矩形x圆交叉点?据我所知,boost
有交叉功能:
template<typename Geometry1, typename Geometry2, typename GeometryOut>
bool intersection(Geometry1 const & geometry1, Geometry2 const & geometry2, GeometryOut & geometry_out)
但我无法找到如何构建圆形几何体。我的意思是,我可以创建一个包含许多顶点的多边形,但它不是我正在寻找的那种表示
如果你想使用boost获得圆,你可以使用boost::geometry::buffer
算法。详情here。
您需要将点作为输入几何(圆的中心)和半径作为distance_strategy传递。完整的测试代码如下
#include <boost/geometry.hpp>
#include <boost/geometry/io/io.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <iostream>
int main()
{
typedef boost::geometry::model::d2::point_xy<double> point;
typedef boost::geometry::model::polygon<point> polygon;
const double buffer_distance = 1.0; // radius of circle
const int points_per_circle = 36;
boost::geometry::strategy::buffer::distance_symmetric<double> distance_strategy(buffer_distance);
boost::geometry::strategy::buffer::join_round join_strategy(points_per_circle);
boost::geometry::strategy::buffer::end_round end_strategy(points_per_circle);
boost::geometry::strategy::buffer::point_circle circle_strategy(points_per_circle);
boost::geometry::strategy::buffer::side_straight side_strategy;
boost::geometry::model::multi_polygon<polygon> result;
point pt;
boost::geometry::read_wkt("POINT(5 5)", pt); // center of circle
boost::geometry::buffer(pt, result,
distance_strategy, side_strategy,
join_strategy, end_strategy, circle_strategy);
// first item of result is circle with 1 radius and (5,5) point as center
// result should have 1 polygon
polygon rect; // your rectangle
boost::geometry::read_wkt("POLYGON((3 3,3 7,5 7,5 3,3 3))",rect);
std::deque<polygon> intersectionGeometry;
boost::geometry::intersection(rect,result.front(),intersectionGeometry);
if (intersectionGeometry.size() == 1)
std::cout << boost::geometry::wkt(intersectionGeometry.front()) << std::endl; // intersection
std::cout << boost::geometry::wkt(result) << "\n";
}
将交叉点打印为
POLYGON((5 4,4.82635 4.01519,4.65798 4.06031,4.5 4.13397,4.35721 4.23396,4.23396 4.35721,4.13397 4.5,4.06031 4.65798,4.01519 4.82635,4 5,4.01519 5.17365,4.06031 5.34202,4.13397 5.5,4.23396 5.64279,4.35721 5.76604,4.5 5.86603,4.65798 5.93969,4.82635 5.98481,5 6,5 4))
你可以看到缓冲区的result
是:
MULTIPOLYGON(((6 5,5.98481 4.82635,5.93969 4.65798,5.86603 4.5,5.76604 4.35721,5.64279 4.23396,5.5 4.13397,5.34202 4.06031,5.17365 4.01519,5 4,4.82635 4.01519,4.65798 4.06031,4.5 4.13397,4.35721 4.23396,4.23396 4.35721,4.13397 4.5,4.06031 4.65798,4.01519 4.82635,4 5,4.01519 5.17365,4.06031 5.34202,4.13397 5.5,4.23396 5.64279,4.35721 5.76604,4.5 5.86603,4.65798 5.93969,4.82635 5.98481,5 6,5.17365 5.98481,5.34202 5.93969,5.5 5.86603,5.64279 5.76604,5.76604 5.64279,5.86603 5.5,5.93969 5.34202,5.98481 5.17365,6 5)))
多边形近似是通常的方法。
构建一个的简单方法是使用buffer
与point_circle
策略: