Changeset 18590 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2022-11-08T21:50:55+01:00 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Geometry.java
r18589 r18590 492 492 double pdx = point.getX() - p1.getX(); 493 493 double pdy = point.getY() - p1.getY(); 494 495 double offset = (pdx * ldx + pdy * ldy) / (ldx * ldx + ldy * ldy);496 497 if (segmentOnly && offset <= 0)498 return p1;499 else if (segmentOnly && offset >= 1)500 return p2;501 else502 return p1.interpolate(p2, offset);503 }504 505 /**506 * Get the closest point to a segment507 * @param p1 Point 1 of the segment508 * @param p2 Point 2 of the segment509 * @param point The point to use to get the closest point on the segment510 * @param segmentOnly {@code true} if the point <i>must</i> be on the segment511 * @return The closest point on the segment if {@code segmentOnly = true}, otherwise the closest point on the infinite line.512 */513 private static ILatLon closestPointTo(ILatLon p1, ILatLon p2, ILatLon point, boolean segmentOnly) {514 CheckParameterUtil.ensureParameterNotNull(p1, "p1");515 CheckParameterUtil.ensureParameterNotNull(p2, "p2");516 CheckParameterUtil.ensureParameterNotNull(point, "point");517 518 double ldx = p2.lon() - p1.lon();519 double ldy = p2.lat() - p1.lat();520 521 //segment zero length522 if (ldx == 0 && ldy == 0)523 return p1;524 525 double pdx = point.lon() - p1.lon();526 double pdy = point.lat() - p1.lat();527 494 528 495 double offset = (pdx * ldx + pdy * ldy) / (ldx * ldx + ldy * ldy); … … 1534 1501 1535 1502 double smallest = Double.MAX_VALUE; 1503 EastNorth en0 = node.getEastNorth(); 1536 1504 // go through the nodes as if they were paired 1537 1505 Iterator<Node> iter = way.getNodes().iterator(); 1538 Node en1 = iter.next();1506 EastNorth en1 = iter.next().getEastNorth(); 1539 1507 while (iter.hasNext()) { 1540 Node en2 = iter.next();1541 double distance = getSegmentNodeDistSq(en1, en2, node);1508 EastNorth en2 = iter.next().getEastNorth(); 1509 double distance = getSegmentNodeDistSq(en1, en2, en0); 1542 1510 if (distance < smallest) 1543 1511 smallest = distance; … … 1592 1560 double rValue = Double.MAX_VALUE; 1593 1561 Iterator<Node> iter1 = w1.getNodes().iterator(); 1562 List<Node> w2Nodes = w2.getNodes(); 1594 1563 Node w1N1 = iter1.next(); 1595 List<Node> w2Nodes = w2.getNodes();1596 1564 while (iter1.hasNext()) { 1597 1565 Node w1N2 = iter1.next(); … … 1638 1606 return Double.NaN; 1639 1607 } 1640 if (getSegmentSegmentIntersection(ws1Node1, ws1Node2, ws2Node1, ws2Node2) != null) 1608 EastNorth enWs1Node1 = ws1Node1.getEastNorth(); 1609 EastNorth enWs1Node2 = ws1Node2.getEastNorth(); 1610 EastNorth enWs2Node1 = ws2Node1.getEastNorth(); 1611 EastNorth enWs2Node2 = ws2Node2.getEastNorth(); 1612 if (getSegmentSegmentIntersection(enWs1Node1, enWs1Node2, enWs2Node1, enWs2Node2) != null) 1641 1613 return 0; 1642 1614 1643 double dist1sq = getSegmentNodeDistSq( ws1Node1, ws1Node2, ws2Node1);1644 double dist2sq = getSegmentNodeDistSq( ws1Node1, ws1Node2, ws2Node2);1645 double dist3sq = getSegmentNodeDistSq( ws2Node1, ws2Node2, ws1Node1);1646 double dist4sq = getSegmentNodeDistSq( ws2Node1, ws2Node2, ws1Node2);1615 double dist1sq = getSegmentNodeDistSq(enWs1Node1, enWs1Node2, enWs2Node1); 1616 double dist2sq = getSegmentNodeDistSq(enWs1Node1, enWs1Node2, enWs2Node2); 1617 double dist3sq = getSegmentNodeDistSq(enWs2Node1, enWs2Node2, enWs1Node1); 1618 double dist4sq = getSegmentNodeDistSq(enWs2Node1, enWs2Node2, enWs1Node2); 1647 1619 double smallest = Math.min(Math.min(dist1sq, dist2sq), Math.min(dist3sq, dist4sq)); 1648 1620 return smallest != Double.MAX_VALUE ? Math.sqrt(smallest) : Double.NaN; … … 1679 1651 * @return the square of the euclidean distance from p to the closest point on the segment 1680 1652 */ 1681 private static double getSegmentNodeDistSq( ILatLon s1, ILatLon s2, ILatLonp) {1682 ILatLonc1 = closestPointTo(s1, s2, p, true);1683 return c1.distanceSq(p .lon(), p.lat());1653 private static double getSegmentNodeDistSq(EastNorth s1, EastNorth s2, EastNorth p) { 1654 EastNorth c1 = closestPointTo(s1, s2, p, true); 1655 return c1.distanceSq(p); 1684 1656 } 1685 1657 }
Note:
See TracChangeset
for help on using the changeset viewer.