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