地图上判断两条线段是否相交等
/** * 1 相交 2 平行不共线 3 平行共线有重合 4 平行共线不重合 5 不相交不平行 * @param a * @param c * @param range * @return */ public static int isCoordinatesCross(LatLonReq a, LatLonReq c, double range){ LatLonReq b = getDegreesCoordinates(a, range); LatLonReq d = getDegreesCoordinates(c, range); double radians1 = a.getRadians(); double radians2 = c.getRadians(); double abX = b.getLatitude() - a.getLatitude(); double abY = b.getLongitude() - a.getLongitude(); double acX = c.getLatitude() - a.getLatitude(); double acY = c.getLongitude() - a.getLongitude(); double adX = d.getLatitude() - a.getLatitude(); double adY = d.getLongitude() - a.getLongitude(); double ab_acCross = multiCross(abX, abY, acX, acY); double ab_adCross = multiCross(abX, abY, adX, adY); boolean ab_cdCross = false; if(ab_acCross < 0 && ab_adCross > 0){ ab_cdCross = true; } if(ab_acCross > 0 && ab_adCross < 0){ ab_cdCross = true; } if(ab_acCross == 0 && ab_adCross != 0){ ab_cdCross = true; } if(ab_acCross != 0 && ab_adCross == 0){ ab_cdCross = true; } double cdX = d.getLatitude() - c.getLatitude(); double cdY = d.getLongitude() - c.getLongitude(); double caX = a.getLatitude() - c.getLatitude(); double caY = a.getLongitude() - c.getLongitude(); double cbX = b.getLatitude() - c.getLatitude(); double cbY = b.getLongitude() - c.getLongitude(); double cd_caCross = multiCross(cdX, cdY, caX, caY); double cd_cbCross = multiCross(cdX, cdY, cbX, cbY); boolean cd_abCross = false; if(cd_caCross < 0 && cd_cbCross > 0){ cd_abCross = true; } if(cd_caCross > 0 && cd_cbCross < 0){ cd_abCross = true; } if(cd_caCross == 0 && cd_cbCross != 0){ cd_abCross = true; } if(cd_caCross != 0 && cd_cbCross == 0){ cd_abCross = true; } if(ab_cdCross && cd_abCross){ return 1; } //平行 if(radians1 == radians2 || Math.abs(radians1 - radians2) == 180){ if(ab_acCross == 0){ double v = compareLatLon(a, b); double v1 = compareLatLon(c, d); LatLonReq abMin = v < 0 ? a : b; LatLonReq abMax = v > 0 ? a : b; LatLonReq cdMin = v1 < 0 ? c : d; if(compareLatLon(abMin, cdMin) < 0 && compareLatLon(abMax, cdMin) > 0){ return 3; } return 4; } else { return 2; } } return 5; }
/** * 获取向量的另一个坐标 * @param latLonReq * @param range * @return */ public static LatLonReq getDegreesCoordinates(LatLonReq latLonReq, double range){ double distance = Optional.ofNullable(range).orElse(1000.0); double latitude = latLonReq.getLatitude(); double longitude = latLonReq.getLongitude(); double radians = latLonReq.getRadians(); double cosLine = Math.cos(Math.toRadians(radians)) * distance; double sinLine = Math.sin(Math.toRadians(radians)) * distance; double diffLat = cosLine / oneLatitudeDistance(); double diffLon = sinLine / oneLongitudeDistance(latitude, longitude); double reLat = diffLat + latitude; double reLon = diffLon + longitude; return new LatLonReq(reLat, reLon); }
/** * 计算叉乘的结果 * @param x1 * @param y1 * @param x2 * @param y2 * @return */ public static double multiCross(double x1,double y1,double x2,double y2) { return x1*y2 - y1*x2; }
/** * 坐标排序 纬度大的点更大,纬度相同,经度大的点更大 * @param a * @param b * @return */ public static double compareLatLon(LatLonReq a,LatLonReq b) { if(a.getLatitude() != b.getLatitude()){ return a.getLatitude() - b.getLatitude(); } else { return a.getLongitude() - b.getLongitude(); } }
参考:https://www.cnblogs.com/tuyang1129/p/9390376.html