1
2
3
4 package org.opentrafficsim.road.network.factory.vissim;
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
46 public static Coordinate pointAlongSegment(Coordinate p0, Coordinate p1, double distance)
47 {
48 double segLen = p1.distance(p0);
49 double frac = distance / segLen;
50 if (frac <= 0.0)
51 {
52 return p0;
53 }
54 if (frac >= 1.0)
55 {
56 return p1;
57 }
58
59 double x = (p1.x - p0.x) * frac + p0.x;
60 double y = (p1.y - p0.y) * frac + p0.y;
61 return new Coordinate(x, y);
62 }
63
64 public static Coordinate pointAlongLine(LineString line, double distance)
65 {
66 LocatePoint loc = new LocatePoint(line, distance);
67 return loc.getPoint();
68 }
69
70 private Coordinate pt;
71
72 private int index;
73
74 public LocatePoint(LineString line, double distance)
75 {
76 compute(line, distance);
77 }
78
79 private void compute(LineString line, double distance)
80 {
81
82 double totalDist = 0.0;
83 Coordinate[] coord = line.getCoordinates();
84 for (int i = 0; i < coord.length - 1; i++)
85 {
86 Coordinate p0 = coord[i];
87 Coordinate p1 = coord[i + 1];
88 double segLen = p1.distance(p0);
89 if (totalDist + segLen > distance)
90 {
91 pt = pointAlongSegment(p0, p1, distance - totalDist);
92 index = i;
93 return;
94 }
95 totalDist += segLen;
96 }
97
98 pt = new Coordinate(coord[coord.length - 1]);
99 index = coord.length;
100 }
101
102 public Coordinate getPoint()
103 {
104 return pt;
105 }
106
107
108
109
110
111 public int getIndex()
112 {
113 return index;
114 }
115
116 }