c++ 点到多边形的距离

目录

判断点是否在多边形内

点到多边形距离


判断点是否在多边形内

#include <vector>

struct Point {
    double x, y;
};

bool isPointInPolygon(const Point &p, const std::vector<Point> &polygon) {
    bool result = false;
    int j = polygon.size() - 1;
    for (int i = 0; i < polygon.size(); i++) {
        if ((polygon[i].y > p.y) != (polygon[j].y > p.y) &&
            (p.x < (polygon[j].x - polygon[i].x) * (p.y - polygon[i].y) / (polygon[j].y - polygon[i].y) + polygon[i].x)) {
            result = !result;
        }
        j = i;
    }
    return result;
}

int main() {
    // 定义多边形顶点,多边形顶点需按顺序(顺时针或逆时针)定义
    std::vector<Point> polygon = {{1, 1}, {1, 4}, {4, 4}, {4, 1}};
    Point p = {2, 3};  // 待判断的点

    if (isPointInPolygon(p, polygon)) {
        std::cout << "点在多边形内" << std::endl;
    } else {
        std::cout << "点不在多边形内" << std::endl;
    }

    return 0;
}

点到多边形距离

点在外部,是负数,点在内部,是正数。

#include <iostream>
#include <vector>
#include <cmath>
#include <limits>

struct Point {
    double x, y;
};

double distance(const Point& p1, const Point& p2) {
    return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}

bool isPointInPolygon(const Point& p, const std::vector<Point>& polygon) {
    bool inside = false;
    for (int i = 0, j = polygon.size() - 1; i < polygon.size(); j = i++) {
        if ((polygon[i].y > p.y) != (polygon[j].y > p.y) &&
            (p.x < (polygon[j].x - polygon[i].x) * (p.y - polygon[i].y) / (polygon[j].y - polygon[i].y) + polygon[i].x)) {
            inside = !inside;
        }
    }
    return inside;
}

double pointToSegmentDistance(const Point& p, const Point& a, const Point& b) {
    Point AB{ b.x - a.x, b.y - a.y };
    Point BP{ p.x - b.x, p.y - b.y };
    Point AP{ p.x - a.x, p.y - a.y };
    double abSquare = AB.x * AB.x + AB.y * AB.y;
    double abapProduct = AB.x * AP.x + AB.y * AP.y;
    double t = abapProduct / abSquare;

    if (t < 0.0) {
        return distance(p, a);
    }
    else if (t > 1.0) {
        return distance(p, b);
    }
    else {
        Point closest{ a.x + t * AB.x, a.y + t * AB.y };
        return distance(p, closest);
    }
}

double pointToPolygonDistance(const Point& p, const std::vector<Point>& polygon) {
    double minDist = std::numeric_limits<double>::max();
    for (int i = 0, n = polygon.size(); i < n; i++) {
        int j = (i + 1) % n;
        double dist = pointToSegmentDistance(p, polygon[i], polygon[j]);
        minDist = std::min(minDist, dist);
    }

    // 如果点在多边形外,返回负的距离
    if (!isPointInPolygon(p, polygon)) {
        minDist = -minDist;
    }

    return minDist;
}

int main() {
    std::vector<Point> polygon = { {50, 300}, {200, 300}, {300, 250}, {200, 100}, {100, 150} };
    Point p = { 150, 150 };  // 待计算的点

    double distance = pointToPolygonDistance(p, polygon);
    std::cout << "点到多边形的最短距离是: " << distance << std::endl;

    return 0;
}

相关推荐

  1. c++ 多边距离

    2024-05-09 10:16:07       13 阅读
  2. 平面上直线距离

    2024-05-09 10:16:07       41 阅读
  3. PCL 圆柱距离(3D)

    2024-05-09 10:16:07       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-09 10:16:07       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-09 10:16:07       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-09 10:16:07       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-09 10:16:07       18 阅读

热门阅读

  1. AI学习指南高数篇-微分方程

    2024-05-09 10:16:07       12 阅读
  2. 前端每日基础day1

    2024-05-09 10:16:07       11 阅读
  3. vue3配置基础路径

    2024-05-09 10:16:07       11 阅读
  4. MySQL数据库失效:潜在场景、影响与应对策略

    2024-05-09 10:16:07       9 阅读
  5. js之dom学习

    2024-05-09 10:16:07       6 阅读
  6. 【代码随想录37期】Day01 二分查找 + 移除元素

    2024-05-09 10:16:07       9 阅读
  7. 离线安装dokcer&离线获取docker镜像

    2024-05-09 10:16:07       11 阅读
  8. docker-compose-itd和d

    2024-05-09 10:16:07       10 阅读