1 package org.opentrafficsim.core.geometry;
2
3 import org.djutils.draw.line.PolyLine2d;
4 import org.djutils.draw.point.OrientedPoint2d;
5 import org.djutils.draw.point.Point2d;
6 import org.djutils.exceptions.Throw;
7 import org.opentrafficsim.base.geometry.OtsGeometryUtil;
8
9
10
11
12
13
14
15
16
17 public class ContinuousStraight implements ContinuousLine
18 {
19
20
21 private final OrientedPoint2d startPoint;
22
23
24 private final OrientedPoint2d endPoint;
25
26
27 private final double length;
28
29
30
31
32
33
34 public ContinuousStraight(final OrientedPoint2d startPoint, final double length)
35 {
36 Throw.whenNull(startPoint, "Start point may not be null.");
37 Throw.when(length <= 0.0, IllegalArgumentException.class, "Length must be above 0.");
38 this.startPoint = startPoint;
39 this.endPoint = new OrientedPoint2d(startPoint.x + length * Math.cos(startPoint.dirZ),
40 startPoint.y + length * Math.sin(startPoint.dirZ), startPoint.dirZ);
41 this.length = length;
42 }
43
44 @Override
45 public OrientedPoint2d getStartPoint()
46 {
47 return this.startPoint;
48 }
49
50 @Override
51 public OrientedPoint2d getEndPoint()
52 {
53 return this.endPoint;
54 }
55
56 @Override
57 public double getStartCurvature()
58 {
59 return 0.0;
60 }
61
62 @Override
63 public double getEndCurvature()
64 {
65 return 0.0;
66 }
67
68
69
70
71
72 public PolyLine2d flatten()
73 {
74 return new PolyLine2d(new Point2d(this.startPoint.x, this.startPoint.y), new Point2d(this.endPoint.x, this.endPoint.y));
75 }
76
77
78
79
80
81
82 @Override
83 public PolyLine2d flatten(final Flattener flattener)
84 {
85 return flatten();
86 }
87
88
89
90
91
92
93 public PolyLine2d offset(final ContinuousDoubleFunction offset)
94 {
95 Throw.whenNull(offset, "Offsets may not be null.");
96 double[] knots = offset.getKnots();
97 double[] knotOffset = new double[knots.length];
98 for (int i = 0; i < knots.length; i++)
99 {
100 knotOffset[i] = offset.apply(knots[i]);
101 }
102 return OtsGeometryUtil.offsetLine(flatten(), knots, knotOffset);
103 }
104
105
106
107
108
109
110
111 @Override
112 public PolyLine2d flattenOffset(final ContinuousDoubleFunction offsets, final Flattener flattener)
113 {
114 return offset(offsets);
115 }
116
117 @Override
118 public double getLength()
119 {
120 return this.length;
121 }
122
123 @Override
124 public String toString()
125 {
126 return "ContinuousStraight [startPoint=" + this.startPoint + ", endPoint=" + this.endPoint + ", length=" + this.length
127 + "]";
128 }
129
130 }