使用升压几何查找线段的交点
Using boost geometry to find intersection of line segments
我正在尝试将 boost 几何方法 intersects
与我自己的点 class 一起使用,已成功注册到 boost 几何库。
boost 文档 (https://www.boost.org/doc/libs/1_73_0/libs/geometry/doc/html/geometry/reference/algorithms/intersection/intersection_3.html) 说我可以使用点向量作为输出参数。所以我写了这个:
#include <iostream>
#include <deque>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
namespace bg = boost::geometry;
using namespace std;
class cxy
{
public:
double x;
double y;
cxy( double X, double Y )
: x( X )
, y( Y )
{
}
/// boost geometry insists on a default constructor
cxy()
: cxy(0,0)
{
}
};
BOOST_GEOMETRY_REGISTER_POINT_2D( cxy, double, bg::cs::cartesian, x, y )
typedef bg::model::segment<cxy> segment_t;
int main()
{
cxy a(0,0);
cxy b(1,1);
cxy c(1,0);
cxy d(0,1) ;
std::vector<cxy> out;
//////////////// this compiles
bg::intersection(segment_t{a, b}, segment_t{c, d}, out);
//////////////// this does not!!!
segment_t ab( a, b );
segment_t cd( c, d );
bg::intersects( ab, cd, out );
return 0;
}
明确一点:我的问题是我在 intersection
和 intersects
之间感到困惑
以下代码编译并产生预期结果:
#include <iostream>
#include <deque>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
namespace bg = boost::geometry;
using namespace std;
class cxy
{
public:
double x;
double y;
cxy( double X, double Y )
: x( X )
, y( Y )
{
}
/// boost geometry insists on a default constructor
cxy()
: cxy(0,0)
{
}
};
BOOST_GEOMETRY_REGISTER_POINT_2D( cxy, double, bg::cs::cartesian, x, y )
typedef bg::model::segment<cxy> segment_t;
int main()
{
cxy a(0,0);
cxy b(1,1);
cxy c(1,0);
cxy d(0,1) ;
segment_t ab( a, b );
segment_t cd( c, d );
std::vector<cxy> out;
if( ! bg::intersection( ab, cd, out ) ) {
std::cout << "no intersection\n";
return 1;
}
std::cout << "intersection at " << out[0].x <<" " << out[0].y << "\n";
return 0;
}
你问的是“a intersects b 是否”。
但是,您的第三个论点表明您想要的是“intersection”:
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <iostream>
namespace bg = boost::geometry;
using namespace std;
struct cxy {
double x = 0;
double y = 0;
cxy(double X, double Y) : x(X), y(Y) {}
cxy() = default;
};
BOOST_GEOMETRY_REGISTER_POINT_2D(cxy, double, bg::cs::cartesian, x, y)
using segment_t = bg::model::segment<cxy>;
using points_t = bg::model::multi_point<cxy>;
int main() {
cxy a{0, 0}, b{1, 1}, c{1, 0}, d{0, 1};
std::vector<cxy> out;
bg::intersection(segment_t{a, b}, segment_t{c, d}, out);
// for output, use a multipoint model or register your vector as one
points_t pts;
bg::intersection(segment_t{a, b}, segment_t{c, d}, pts);
std::cout << bg::wkt(pts) << "\n";
}
版画
MULTIPOINT((0.5 0.5))
我正在尝试将 boost 几何方法 intersects
与我自己的点 class 一起使用,已成功注册到 boost 几何库。
boost 文档 (https://www.boost.org/doc/libs/1_73_0/libs/geometry/doc/html/geometry/reference/algorithms/intersection/intersection_3.html) 说我可以使用点向量作为输出参数。所以我写了这个:
#include <iostream>
#include <deque>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
namespace bg = boost::geometry;
using namespace std;
class cxy
{
public:
double x;
double y;
cxy( double X, double Y )
: x( X )
, y( Y )
{
}
/// boost geometry insists on a default constructor
cxy()
: cxy(0,0)
{
}
};
BOOST_GEOMETRY_REGISTER_POINT_2D( cxy, double, bg::cs::cartesian, x, y )
typedef bg::model::segment<cxy> segment_t;
int main()
{
cxy a(0,0);
cxy b(1,1);
cxy c(1,0);
cxy d(0,1) ;
std::vector<cxy> out;
//////////////// this compiles
bg::intersection(segment_t{a, b}, segment_t{c, d}, out);
//////////////// this does not!!!
segment_t ab( a, b );
segment_t cd( c, d );
bg::intersects( ab, cd, out );
return 0;
}
明确一点:我的问题是我在 intersection
和 intersects
以下代码编译并产生预期结果:
#include <iostream>
#include <deque>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
namespace bg = boost::geometry;
using namespace std;
class cxy
{
public:
double x;
double y;
cxy( double X, double Y )
: x( X )
, y( Y )
{
}
/// boost geometry insists on a default constructor
cxy()
: cxy(0,0)
{
}
};
BOOST_GEOMETRY_REGISTER_POINT_2D( cxy, double, bg::cs::cartesian, x, y )
typedef bg::model::segment<cxy> segment_t;
int main()
{
cxy a(0,0);
cxy b(1,1);
cxy c(1,0);
cxy d(0,1) ;
segment_t ab( a, b );
segment_t cd( c, d );
std::vector<cxy> out;
if( ! bg::intersection( ab, cd, out ) ) {
std::cout << "no intersection\n";
return 1;
}
std::cout << "intersection at " << out[0].x <<" " << out[0].y << "\n";
return 0;
}
你问的是“a intersects b 是否”。
但是,您的第三个论点表明您想要的是“intersection”:
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <iostream>
namespace bg = boost::geometry;
using namespace std;
struct cxy {
double x = 0;
double y = 0;
cxy(double X, double Y) : x(X), y(Y) {}
cxy() = default;
};
BOOST_GEOMETRY_REGISTER_POINT_2D(cxy, double, bg::cs::cartesian, x, y)
using segment_t = bg::model::segment<cxy>;
using points_t = bg::model::multi_point<cxy>;
int main() {
cxy a{0, 0}, b{1, 1}, c{1, 0}, d{0, 1};
std::vector<cxy> out;
bg::intersection(segment_t{a, b}, segment_t{c, d}, out);
// for output, use a multipoint model or register your vector as one
points_t pts;
bg::intersection(segment_t{a, b}, segment_t{c, d}, pts);
std::cout << bg::wkt(pts) << "\n";
}
版画
MULTIPOINT((0.5 0.5))