#include "electronicFence.h" //----------------param // 定义多边形的经纬度坐标集合 double polygonLat[REC_COORDINATE_DEPTH] = {0}; double polygonLng[REC_COORDINATE_DEPTH] = {0}; /** * 是否有 横断
参数为四个点的坐标 * * @param px1 * @param py1 * @param px2 * @param py2 * @param px3 * @param py3 * @param px4 * @param py4 * @return */ static bool isIntersect(double px1, double py1, double px2, double py2, double px3, double py3, double px4, double py4) { bool flag = false; double d = (px2 - px1) * (py4 - py3) - (py2 - py1) * (px4 - px3); if (d != 0) { double r = ((py1 - py3) * (px4 - px3) - (px1 - px3) * (py4 - py3)) / d; double s = ((py1 - py3) * (px2 - px1) - (px1 - px3) * (py2 - py1)) / d; if ((r >= 0) && (r <= 1) && (s >= 0) && (s <= 1)) { flag = true; } } return flag; } static double Multiply(double px0, double py0, double px1, double py1, double px2, double py2) { return ((px1 - px0) * (py2 - py0) - (px2 - px0) * (py1 - py0)); } /** * 目标点是否在目标边上边上
* * @param px0 * 目标点的经度坐标 * @param py0 * 目标点的纬度坐标 * @param px1 * 目标线的起点(终点)经度坐标 * @param py1 * 目标线的起点(终点)纬度坐标 * @param px2 * 目标线的终点(起点)经度坐标 * @param py2 * 目标线的终点(起点)纬度坐标 * @return */ static bool isPointOnLine(double px0, double py0, double px1, double py1, double px2, double py2) { bool flag = false; double ESP = 1e-9; if ((fabs(Multiply(px0, py0, px1, py1, px2, py2)) < ESP) && ((px0 - px1) * (px0 - px2) <= 0) && ((py0 - py1) * (py0 - py2) <= 0)) { flag = true; } return flag; } /** * 判断目标点是否在多边形内(由多个点组成)
* * @param px * 目标点的经度坐标 * @param py * 目标点的纬度坐标 * @param polygonXA * 多边形的经度坐标集合 * @param polygonYA * 多边形的纬度坐标集合 * @param num * 多边形的坐标集个数 * @return */ bool isPointInPolygon(double px, double py, double polygonXA[], double polygonYA[], int num) { bool isInside = false; double ESP = 1e-9; int count = 0; double linePoint1x; double linePoint1y; double linePoint2x = 180; double linePoint2y; linePoint1x = px; linePoint1y = py; linePoint2y = py; for (int i = 0; i < num; i++) { double cx1 = polygonXA[i]; double cy1 = polygonYA[i]; double cx2 = polygonXA[i + 1]; double cy2 = polygonYA[i + 1]; if (isPointOnLine(px, py, cx1, cy1, cx2, cy2)) { return true; } if (fabs(cy2 - cy1) < ESP) { continue; } if (isPointOnLine(cx1, cy1, linePoint1x, linePoint1y, linePoint2x, linePoint2y)) { if (cy1 > cy2) count++; } else if (isPointOnLine(cx2, cy2, linePoint1x, linePoint1y, linePoint2x, linePoint2y)) { if (cy2 > cy1) count++; } else if (isIntersect(cx1, cy1, cx2, cy2, linePoint1x, linePoint1y, linePoint2x, linePoint2y)) { count++; } } if (count % 2 == 1) { isInside = true; } return isInside; }