C++ 当linestring工作时,使用boost段在可比的_距离内失败

C++ 当linestring工作时,使用boost段在可比的_距离内失败,c++,boost,boost-geometry,C++,Boost,Boost Geometry,我试图计算点和线段之间的距离。但以下代码在运行时突然终止(@comparable_distance) 使用名称空间std; 名称空间bg=boost::geometry; 类顶点{ 公众: 顶点(intpx,intpy):x(px),y(py){ int x; int-y; }; 增强几何体、寄存器、点、2D(顶点、双顶点、cs::笛卡尔坐标、x、y) typedef顶点*vertref; typedef bg::model::segment_type; std::向量段; int main()

我试图计算点和线段之间的距离。但以下代码在运行时突然终止(@comparable_distance)

使用名称空间std;
名称空间bg=boost::geometry;
类顶点{
公众:
顶点(intpx,intpy):x(px),y(py){
int x;
int-y;
};
增强几何体、寄存器、点、2D(顶点、双顶点、cs::笛卡尔坐标、x、y)
typedef顶点*vertref;
typedef bg::model::segment_type;
std::向量段;
int main()
{
段_型l(新顶点(1,2),新顶点(2,2));
顶点p(4,5);
双comp_dist=0;
比较距离=bg::可比距离(p,l);
cout的建模方式是,它采用一个模板参数来建模

现在您使用:

typedef Vertex *vertref;

typedef bg::model::segment<vertref> segment_type;
印刷品:

13
实际上,13是实际距离的平方:

奖金 作为对该评论的回应,是的,甚至还有一个现成的模型,用于引用点(来自其他几何体)的线段。下面是如何使用
引用段

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <iostream>
namespace bg = boost::geometry;

struct Vertex {
    double x;
    double y;
};

BOOST_GEOMETRY_REGISTER_POINT_2D(Vertex, double, bg::cs::cartesian, x, y)

typedef bg::model::segment<Vertex> segment_type;

int main() {
    segment_type l({1, 2}, {2, 2});
    Vertex p {4, 5};

    auto comp_dist = bg::comparable_distance(p, l);

    std::cout << comp_dist << "\n";
}
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <iostream>
namespace bg = boost::geometry;

struct Vertex {
    double x;
    double y;
};

BOOST_GEOMETRY_REGISTER_POINT_2D(Vertex, double, bg::cs::cartesian, x, y)

typedef bg::model::referring_segment<Vertex> segment_type;

int main() {
    Vertex A{1,2},
           B{2,2};
    segment_type l(A, B);
    Vertex p {4, 5};

    std::cout << bg::distance(p, l) << "\n";

    bg::multiply_value(A, -1);
    bg::multiply_value(B, -1);
    std::cout << bg::distance(p, l) << "\n";
}
同样,这与您期望看到的情况相符:

笔记:
  • 在C++中使用<代码>新< /COD>几乎总是错误的。这个错误最常见的是来自垃圾收集语言的初学者,如Java或C.java()
  • 类型不一致(成员
    x
    y
    int
    s)

  • 您可能希望从中提供调用堆栈dump@SergeyA附加调用堆栈。感谢回复@sehe。我之所以需要引用是为了将点列表保存在其他容器中。因此,当我更新点时,距离也会更新。我可以使用linestring来实现这一点。我可以在这里使用引用吗?当然可以使用
    refe我链接的[concept doc]()中列出的模型。查看。将更新答案。完成。加入(计入Geogebra)再次感谢!我就像一个使用攻击步枪的家伙,他不具备使用锤子的能力。我需要从我的脑袋中删除java/python编程,重新学习C++。好的,最后一个愚蠢的问题:D……这些点是动态地在程序中生成的(用户点击屏幕)……我怎么做而不使用动态内存?
    
    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/register/point.hpp>
    #include <iostream>
    namespace bg = boost::geometry;
    
    struct Vertex {
        double x;
        double y;
    };
    
    BOOST_GEOMETRY_REGISTER_POINT_2D(Vertex, double, bg::cs::cartesian, x, y)
    
    typedef bg::model::segment<Vertex> segment_type;
    
    int main() {
        segment_type l({1, 2}, {2, 2});
        Vertex p {4, 5};
    
        auto comp_dist = bg::comparable_distance(p, l);
    
        std::cout << comp_dist << "\n";
    }
    
    13
    
    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/register/point.hpp>
    #include <iostream>
    namespace bg = boost::geometry;
    
    struct Vertex {
        double x;
        double y;
    };
    
    BOOST_GEOMETRY_REGISTER_POINT_2D(Vertex, double, bg::cs::cartesian, x, y)
    
    typedef bg::model::referring_segment<Vertex> segment_type;
    
    int main() {
        Vertex A{1,2},
               B{2,2};
        segment_type l(A, B);
        Vertex p {4, 5};
    
        std::cout << bg::distance(p, l) << "\n";
    
        bg::multiply_value(A, -1);
        bg::multiply_value(B, -1);
        std::cout << bg::distance(p, l) << "\n";
    }
    
    3.60555
    8.60233