c++ 判断点和折线 距离

目录

c++ 一个点a ,一条折线(n个点)b,求折线b上 与a x相同时 y方向的差异

判断点是否在折线下方:


c++ 一个点a ,一条折线(n个点)b,求折线b上 与a x相同时 y方向的差异

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

struct Point {
    double x, y;
};

double calculateYDifferenceToPolyline(const std::vector<Point>& polyline, const Point& a) {
    double minDifference = std::numeric_limits<double>::infinity();

    for (size_t i = 0; i < polyline.size() - 1; ++i) {
        const Point& P1 = polyline[i];
        const Point& P2 = polyline[i + 1];

        // 检查点 a 是否在 P1 和 P2 之间的 x 范围内
        if ((a.x >= P1.x && a.x <= P2.x) || (a.x >= P2.x && a.x <= P1.x)) {
            // 计算 a 在 P1 -> P2 线段上的对应 y 坐标
            double yLine = P1.y + (P2.y - P1.y) * (a.x - P1.x) / (P2.x - P1.x);

            // 计算 y 方向上的差异
            double difference = a.y - yLine;

            // 更新最小差异
            minDifference = std::min(minDifference, std::abs(difference));
        }
    }

    return minDifference;
}

int main() {
    // 定义折线
    std::vector<Point> polyline = {
        {1.0, 1.0}, {2.0, 3.0}, {3.0, 3.0}, {4.0, 5.0}, {5.0, 5.0}, {6.0, 7.0}
    };

    // 定义需要判断的点
    Point a = { 3.5, 3.5 };

    // 计算 a 与折线段的 y 方向差异
    double difference = calculateYDifferenceToPolyline(polyline, a);

    std::cout << "dis: " << difference << " units." << std::endl;

    return 0;
}

判断点是否在折线下方:

#include <iostream>
#include <vector>

struct Point {
    double x, y;
};

int checkPointRelativeToPolyline(const std::vector<Point>& polyline, const Point& P3) {
    int isBelow = 0;

    for (size_t i = 0; i < polyline.size() - 1; ++i) {
        const Point& P1 = polyline[i];
        const Point& P2 = polyline[i + 1];

        // 计算向量叉积
        double crossProduct = (P2.x - P1.x) * (P3.y - P1.y) - (P2.y - P1.y) * (P3.x - P1.x);


        if (crossProduct < 0) {
            isBelow = 1;
        }

    }

    return isBelow;

}

int main() {
    // 定义折线
    std::vector<Point> polyline = {
        {1.0, 1.0}, {2.0, 3.0}, {3.0, 3.0}, {4.0, 5.0}, {5.0, 5.0}, {6.0, 7.0}
    };

    // 定义需要判断的点
    Point P3 = { 3.5, 4.0 };

    // 检查 P3 相对于折线的每个线段的位置
    checkPointRelativeToPolyline(polyline, P3);

    return 0;
}

相关推荐

  1. c++ 判断折线 距离

    2024-05-03 04:36:02       33 阅读
  2. 距离(C++)

    2024-05-03 04:36:02       22 阅读
  3. c++ 到多边形的距离

    2024-05-03 04:36:02       34 阅读
  4. C# 计算两个坐标直接的距离

    2024-05-03 04:36:02       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-03 04:36:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-03 04:36:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-03 04:36:02       87 阅读
  4. Python语言-面向对象

    2024-05-03 04:36:02       96 阅读

热门阅读

  1. Redis 实现分布式Session 登录相关细节

    2024-05-03 04:36:02       30 阅读
  2. VSCode 配置 Qt 开发环境

    2024-05-03 04:36:02       32 阅读
  3. 模拟实现将字符串转化为整形

    2024-05-03 04:36:02       36 阅读
  4. Matlab基本语法

    2024-05-03 04:36:02       32 阅读
  5. js如何把json列表转成数组

    2024-05-03 04:36:02       34 阅读
  6. 第八周学习笔记DAY.5-实用类介绍

    2024-05-03 04:36:02       38 阅读
  7. [C++基础学习]----03-程序流程结构之跳转语句详解

    2024-05-03 04:36:02       32 阅读