当前位置: 首页 > article >正文

用C++实现点到三角形最小距离的计算

1、全部代码

#include <iostream>
#include <cmath>
#include <array>
#include <algorithm>
 
// 二维点结构体 
struct Point2D 
{
    double x, y;
    Point2D(double x = 0, double y = 0) : x(x), y(y) {}
};
 
// 计算点到线段的最小距离 
double pointToSegmentDistance(const Point2D& p, 
                             const Point2D& a, 
                             const Point2D& b) 
{
    const Point2D ab = {b.x - a.x, b.y - a.y}; // 线段向量 
    const Point2D ap = {p.x - a.x, p.y - a.y}; // 起点到点的向量 
    
    // 计算投影参数 
    const double dot = ap.x * ab.x + ap.y * ab.y;
    const double lenSq = ab.x * ab.x + ab.y * ab.y;
    
    // 处理零长度线段 
    if (lenSq == 0.0) 
        return std::hypot(ap.x, ap.y);
    
    const double t = std::clamp(dot / lenSq, 0.0, 1.0);
    
    // 计算投影点 
    const Point2D projection = 
    {
        a.x + t * ab.x,
        a.y + t * ab.y 
    };
    
    return std::hypot(p.x - projection.x, p.y - projection.y);
}
 
// 计算点到三角形的最小距离 
double pointToTriangleDistance(const Point2D& p,
                              const Point2D& a,
                              const Point2D& b,
                              const Point2D& c) 
{
    // 计算到三条边的最小距离 
    const double d1 = pointToSegmentDistance(p, a, b);
    const double d2 = pointToSegmentDistance(p, b, c);
    const double d3 = pointToSegmentDistance(p, c, a);
    
    return std::min({d1, d2, d3});
}
 
int main() 
{
    // 测试用例 
    Point2D p(1, 1);  // 测试点 
    Point2D a(0, 0);  // 三角形顶点 
    Point2D b(4, 0);
    Point2D c(2, 3);
    
    double distance = pointToTriangleDistance(p, a, b, c);
    std::cout << "Minimum distance: " << distance << std::endl;
    
    return 0;
}

2、核心算法解析(三维度分析)

2.1 几何原理维度

  • 将三角形分解为三条边处理
  • 采用投影法计算点到线段的最近距离
  • 使用向量点积判断投影点是否在线段范围内
  • 运用勾股定理计算欧氏距离

2.2 边界处理维度

// 处理退化线段(起点终点重合)
if (lenSq == 0.0) 
    return std::hypot(ap.x, ap.y);
 
// 使用clamp确保投影参数在[0,1]区间 
const double t = std::clamp(dot / lenSq, 0.0, 1.0);

2.3 性能优化维度

  • 避免使用平方根运算直到最后计算距离
  • 使用hypot函数避免数值溢出
  • 并行计算三条边的距离
  • 最小堆思想选取最小值

2.4 扩展功能接口

// 增强版接口(返回最近点和距离)
struct DistanceResult 
{
    Point2D closest_point;
    double distance;
};
 
DistanceResult enhancedDistance(const Point2D& p, 
                               const Point2D& a,
                               const Point2D& b,
                               const Point2D& c) 
{
    // 实现逻辑:
    // 1. 计算三条边的最近点 
    // 2. 比较三个距离 
    // 3. 返回最小距离及其对应点 
    // (实现代码约20行,此处省略)
}

2.5 使用示例

// 场景1:点在三角形内部 
Point2D p1(2, 1);
assert(pointToTriangleDistance(p1,a,b,c) == 1.0);
 
// 场景2:点在顶点附近 
Point2D p2(0.1, 0.1);
assert(floor(distance*100) == 14); // 实际距离≈0.1414 
 
// 场景3:点在边的延长线上 
Point2D p3(5, 0);
assert(distance == 1.0); // 距离边BC为1 
 
// 场景4:退化三角形处理 
Point2D d(0,0);
assert(pointToTriangleDistance(p,d,d,d) == hypot(1,1));


http://www.kler.cn/a/549057.html

相关文章:

  • 基于Spring Boot的家电销售展示平台设计与实现(LW+源码+讲解)
  • CI/CD(二)docker-compose安装Jenkins
  • C++11 thread
  • AF3 MmcifObject类解读
  • 故地重游:一眼是曾经,一眼是如今
  • spring boot 对接aws 的S3 服务,实现上传和查询
  • 【Black Mesa】黑山起源用服务器开服多人联机教程
  • DOS命令 setx 用法
  • linux常用命令大全(包括抓包、网络检测、路由等,做项目一点点总结而来!)
  • 静默安装OGG for MySQL微服务版本,高效开展数据同步和迁移
  • matlab基于梯度下降和软阈值化的去噪算法
  • Kubernetes-master 组件
  • 多媒体软件安全与授权新范例,用 CodeMeter 实现安全、高效的软件许可管理
  • 鸿蒙项目用的router如何迁移至Navgation
  • 基于SpringBoot+Vue的智慧校园管理系统设计和实现(源码+文档+部署讲解)
  • 几款dxf文件转Gcode的开源软件
  • K8S下载离线安装包所需文件
  • HTML4
  • 权谋场景人物引入
  • 架构——Nginx功能、职责、原理、配置示例、应用场景