CGAL 计算两点间距离:使用 squared_distance() 函数
CGAL 库提供了 CGAL::squared_distance() 函数来计算两点之间的距离。该函数接收两个点作为参数,并返回这两个点之间的平方距离。
以下 C++ 代码示例演示了如何使用 CGAL::squared_distance() 函数计算两点之间的距离:
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/point_generators_3.h>
#include <CGAL/distance.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_3 Point_3;
int main()
{
Point_3 p1(0, 0, 0);
Point_3 p2(1, 1, 1);
double distance_squared = CGAL::squared_distance(p1, p2);
double distance = std::sqrt(distance_squared);
std::cout << 'Distance: ' << distance << std::endl;
}
在这个例子中,我们使用 CGAL::Exact_predicates_inexact_constructions_kernel 来定义点和计算距离。我们创建了两个点 p1 和 p2,并计算它们之间的距离。我们首先计算平方距离,然后使用 std::sqrt() 函数计算实际距离。最后,我们将距离输出到控制台。
原文地址: https://www.cveoy.top/t/topic/nvt8 著作权归作者所有。请勿转载和采集!