1
2
3
4 package org.opentrafficsim.road.network.factory.shape;
5
6 import org.locationtech.jts.geom.Coordinate;
7 import org.locationtech.jts.geom.LineString;
8
9
10
11
12 public class LocatePoint
13 {
14
15 public static Coordinate pointAlongSegment(Coordinate p0, Coordinate p1, double distance)
16 {
17 double segLen = p1.distance(p0);
18 double frac = distance / segLen;
19 if (frac <= 0.0)
20 {
21 return p0;
22 }
23 if (frac >= 1.0)
24 {
25 return p1;
26 }
27
28 double x = (p1.x - p0.x) * frac + p0.x;
29 double y = (p1.y - p0.y) * frac + p0.y;
30 return new Coordinate(x, y);
31 }
32
33 public static Coordinate pointAlongLine(LineString line, double distance)
34 {
35 LocatePointnetwork/factory/shape/LocatePoint.html#LocatePoint">LocatePoint loc = new LocatePoint(line, distance);
36 return loc.getPoint();
37 }
38
39 private Coordinate pt;
40
41 private int index;
42
43 public LocatePoint(LineString line, double distance)
44 {
45 compute(line, distance);
46 }
47
48 private void compute(LineString line, double distance)
49 {
50
51 double totalDist = 0.0;
52 Coordinate[] coord = line.getCoordinates();
53 for (int i = 0; i < coord.length - 1; i++)
54 {
55 Coordinate p0 = coord[i];
56 Coordinate p1 = coord[i + 1];
57 double segLen = p1.distance(p0);
58 if (totalDist + segLen > distance)
59 {
60 pt = pointAlongSegment(p0, p1, distance - totalDist);
61 index = i;
62 return;
63 }
64 totalDist += segLen;
65 }
66
67 pt = new Coordinate(coord[coord.length - 1]);
68 index = coord.length;
69 }
70
71 public Coordinate getPoint()
72 {
73 return pt;
74 }
75
76
77
78
79
80 public int getIndex()
81 {
82 return index;
83 }
84
85 }