123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #include "electronicFence.h"
- //----------------param
- // 定义多边形的经纬度坐标集合
- double polygonLat[REC_COORDINATE_DEPTH] = {0};
- double polygonLng[REC_COORDINATE_DEPTH] = {0};
-
- ///**
- //* 是否有 横断<br/> 参数为四个点的坐标
- //*
- //* @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));
- //}
- //
- ///**
- //* 目标点是否在目标边上边上<br/>
- //*
- //* @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;
- //}
- //
- //
- ///**
- //* 判断目标点是否在多边形内(由多个点组成)<br/>
- //*
- //* @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;
- //}
- // 判断两条线段是否相交
- int segmentsIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
- double d1, d2, d3, d4;
- d1 = (x1 - x2) * (y3 - y1) - (y1 - y2) * (x3 - x1);
- d2 = (x1 - x2) * (y4 - y1) - (y1 - y2) * (x4 - x1);
- d3 = (x3 - x4) * (y1 - y3) - (y3 - y4) * (x1 - x3);
- d4 = (x3 - x4) * (y2 - y3) - (y3 - y4) * (x2 - x3);
- return ((d1 * d2 <= 0.0) && (d3 * d4 <= 0.0));
- }
- // 判断点是否在多边形内部
- int pointInPolygon(double pointLat, double pointLng, double polygonLat[], double polygonLng[], int numPoints) {
- int i, j, c = 0;
- for (i = 0, j = numPoints - 1; i < numPoints; j = i++) {
- if ((polygonLat[i] > pointLat) != (polygonLat[j] > pointLat) &&
- pointLng < (polygonLng[j] - polygonLng[i]) * (pointLat - polygonLat[i]) / (polygonLat[j] - polygonLat[i]) + polygonLng[i]) {
- c = !c;
- }
- }
- return c;
- }
|