当前位置:   article > 正文

c++ 点到多边形的距离

c++ 点到多边形的距离

目录

判断点是否在多边形内

点到多边形距离


判断点是否在多边形内

  1. #include <vector>
  2. struct Point {
  3. double x, y;
  4. };
  5. bool isPointInPolygon(const Point &p, const std::vector<Point> &polygon) {
  6. bool result = false;
  7. int j = polygon.size() - 1;
  8. for (int i = 0; i < polygon.size(); i++) {
  9. if ((polygon[i].y > p.y) != (polygon[j].y > p.y) &&
  10. (p.x < (polygon[j].x - polygon[i].x) * (p.y - polygon[i].y) / (polygon[j].y - polygon[i].y) + polygon[i].x)) {
  11. result = !result;
  12. }
  13. j = i;
  14. }
  15. return result;
  16. }
  17. int main() {
  18. // 定义多边形顶点,多边形顶点需按顺序(顺时针或逆时针)定义
  19. std::vector<Point> polygon = {{1, 1}, {1, 4}, {4, 4}, {4, 1}};
  20. Point p = {2, 3}; // 待判断的点
  21. if (isPointInPolygon(p, polygon)) {
  22. std::cout << "点在多边形内" << std::endl;
  23. } else {
  24. std::cout << "点不在多边形内" << std::endl;
  25. }
  26. return 0;
  27. }

点到多边形距离

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

  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <limits>
  5. struct Point {
  6. double x, y;
  7. };
  8. double distance(const Point& p1, const Point& p2) {
  9. return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
  10. }
  11. bool isPointInPolygon(const Point& p, const std::vector<Point>& polygon) {
  12. bool inside = false;
  13. for (int i = 0, j = polygon.size() - 1; i < polygon.size(); j = i++) {
  14. if ((polygon[i].y > p.y) != (polygon[j].y > p.y) &&
  15. (p.x < (polygon[j].x - polygon[i].x) * (p.y - polygon[i].y) / (polygon[j].y - polygon[i].y) + polygon[i].x)) {
  16. inside = !inside;
  17. }
  18. }
  19. return inside;
  20. }
  21. double pointToSegmentDistance(const Point& p, const Point& a, const Point& b) {
  22. Point AB{ b.x - a.x, b.y - a.y };
  23. Point BP{ p.x - b.x, p.y - b.y };
  24. Point AP{ p.x - a.x, p.y - a.y };
  25. double abSquare = AB.x * AB.x + AB.y * AB.y;
  26. double abapProduct = AB.x * AP.x + AB.y * AP.y;
  27. double t = abapProduct / abSquare;
  28. if (t < 0.0) {
  29. return distance(p, a);
  30. }
  31. else if (t > 1.0) {
  32. return distance(p, b);
  33. }
  34. else {
  35. Point closest{ a.x + t * AB.x, a.y + t * AB.y };
  36. return distance(p, closest);
  37. }
  38. }
  39. double pointToPolygonDistance(const Point& p, const std::vector<Point>& polygon) {
  40. double minDist = std::numeric_limits<double>::max();
  41. for (int i = 0, n = polygon.size(); i < n; i++) {
  42. int j = (i + 1) % n;
  43. double dist = pointToSegmentDistance(p, polygon[i], polygon[j]);
  44. minDist = std::min(minDist, dist);
  45. }
  46. // 如果点在多边形外,返回负的距离
  47. if (!isPointInPolygon(p, polygon)) {
  48. minDist = -minDist;
  49. }
  50. return minDist;
  51. }
  52. int main() {
  53. std::vector<Point> polygon = { {50, 300}, {200, 300}, {300, 250}, {200, 100}, {100, 150} };
  54. Point p = { 150, 150 }; // 待计算的点
  55. double distance = pointToPolygonDistance(p, polygon);
  56. std::cout << "点到多边形的最短距离是: " << distance << std::endl;
  57. return 0;
  58. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/562727
推荐阅读
相关标签
  

闽ICP备14008679号