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.djutils.exceptions.Try;
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
45 @Override
46 public OrientedPoint2d getStartPoint()
47 {
48 return this.startPoint;
49 }
50
51
52 @Override
53 public OrientedPoint2d getEndPoint()
54 {
55 return this.endPoint;
56 }
57
58
59 @Override
60 public double getStartCurvature()
61 {
62 return 0.0;
63 }
64
65
66 @Override
67 public double getEndCurvature()
68 {
69 return 0.0;
70 }
71
72
73
74
75
76 public PolyLine2d flatten()
77 {
78 return new PolyLine2d(new Point2d(this.startPoint.x, this.startPoint.y), new Point2d(this.endPoint.x, this.endPoint.y));
79 }
80
81
82
83
84
85
86 @Override
87 public PolyLine2d flatten(final Flattener flattener)
88 {
89 return flatten();
90 }
91
92
93
94
95
96
97 public PolyLine2d offset(final FractionalLengthData offsets)
98 {
99 Throw.whenNull(offsets, "Offsets may not be null.");
100 return Try.assign(
101 () -> OtsGeometryUtil.offsetLine(flatten(), offsets.getFractionalLengthsAsArray(), offsets.getValuesAsArray()),
102 "Unexpected exception while creating straigh OtsLine2d.");
103 }
104
105
106
107
108
109
110
111 @Override
112 public PolyLine2d flattenOffset(final FractionalLengthData offsets, final Flattener flattener)
113 {
114 return offset(offsets);
115 }
116
117
118 @Override
119 public double getLength()
120 {
121 return this.length;
122 }
123
124
125 @Override
126 public String toString()
127 {
128 return "ContinuousStraight [startPoint=" + this.startPoint + ", endPoint=" + this.endPoint + ", length=" + this.length
129 + "]";
130 }
131
132 }