diff --git a/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/Line.java b/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/Line.java index b40f0faee..d6fd48715 100644 --- a/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/Line.java +++ b/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/Line.java @@ -80,6 +80,9 @@ public class Line implements Hyperplane, EmbeddingThe line is oriented from p1 to p2

* @param p1 first point @@ -118,6 +121,7 @@ public class Line implements Hyperplane, Embedding, Embedding, Embedding, Embedding, Embedding, EmbeddingGet a line with reversed orientation with respect to the - * instance. A new object is built, the instance is untouched.

+ * instance.

+ *

+ * As long as neither the instance nor its reverse are modified + * (i.e. as long as none of the {@link #reset(Vector2D, Vector2D)}, + * {@link #reset(Vector2D, double)}, {@link #revertSelf()}, + * {@link #setAngle(double)} or {@link #setOriginOffset(double)} + * methods are called), then the line and its reverse remain linked + * together so that {@code line.getReverse().getReverse() == line}. + * When one of the line is modified, the link is deleted as both + * instance becomes independent. + *

* @return a new line, with orientation opposite to the instance orientation */ public Line getReverse() { - return new Line((angle < FastMath.PI) ? (angle + FastMath.PI) : (angle - FastMath.PI), - -cos, -sin, -originOffset, tolerance); + if (reverse == null) { + reverse = new Line((angle < FastMath.PI) ? (angle + FastMath.PI) : (angle - FastMath.PI), + -cos, -sin, -originOffset, tolerance); + reverse.reverse = this; + } + return reverse; } /** Transform a space point into a sub-space point. @@ -383,6 +414,7 @@ public class Line implements Hyperplane, Embedding, Embedding