View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.util.geometry;
16  
17  /**
18   * Simple 2D line segment shape.
19   *
20   * @author Ronald Brill
21   * @author Ashutosh Panda
22   */
23  public class Line2D implements Shape2D {
24  
25      private final double startX_;
26      private final double startY_;
27      private final double endX_;
28      private final double endY_;
29      private final boolean isVertical_;
30  
31      private final double slope_;
32      private final double yIntercept_;
33  
34      /**
35       * Creates a new line segment between the two given points.
36       *
37       * @param start the start point
38       * @param end the end point
39       */
40      public Line2D(final Point2D start, final Point2D end) {
41          this(start.getX(), start.getY(), end.getX(), end.getY());
42      }
43  
44      /**
45       * Creates a new line segment between the two given coordinates.
46       *
47       * @param x1 the x coordinate of the start point
48       * @param y1 the y coordinate of the start point
49       * @param x2 the x coordinate of the end point
50       * @param y2 the y coordinate of the end point
51       */
52      public Line2D(final double x1, final double y1, final double x2, final double y2) {
53          startX_ = x1;
54          startY_ = y1;
55          endX_ = x2;
56          endY_ = y2;
57  
58          isVertical_ = Math.abs(startX_ - endX_) < EPSILON;
59          if (isVertical_) {
60              slope_ = Double.NaN;
61              yIntercept_ = Double.NaN;
62          }
63          else {
64              // y = slope * x + b
65              slope_ = (endY_ - startY_) / (endX_ - startX_);
66              yIntercept_ = startY_ - slope_ * startX_;
67          }
68      }
69  
70      /**
71       * Returns the intersection point of this line with the given line,
72       * or {@code null} if the lines are parallel.
73       *
74       * @param line the line to intersect with
75       * @return the intersection point, or {@code null} if the lines are parallel
76       */
77      public Point2D intersect(final Line2D line) {
78          if (isVertical_ && line.isVertical_) {
79              return null;
80          }
81  
82          if (isVertical_ && !line.isVertical_) {
83              final double intersectY = line.slope_ * startX_ + line.yIntercept_;
84              return new Point2D(startX_, intersectY);
85          }
86  
87          if (!isVertical_ && line.isVertical_) {
88              final double intersectY = slope_ * line.startX_ + yIntercept_;
89              return new Point2D(line.startX_, intersectY);
90          }
91  
92          // parallel?
93          if (Math.abs(slope_ - line.slope_) < EPSILON) {
94              return null;
95          }
96  
97          // x = (n2-n1)/(m1-m2)
98          final double intersectX = (line.yIntercept_ - yIntercept_) / (slope_ - line.slope_);
99          // y = m2*x+n2
100         final double intersectY = slope_ * intersectX + yIntercept_;
101         return new Point2D(intersectX, intersectY);
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public boolean contains(final double x, final double y) {
109         if (isVertical_) {
110             if (Math.abs(startX_ - x) > EPSILON) {
111                 return false;
112             }
113         }
114         else {
115             final double testY = slope_ * x + yIntercept_;
116             if (Math.abs(y - testY) > EPSILON) {
117                 return false;
118             }
119 
120             if (x < startX_ && x < endX_
121                     || (x > startX_ && x > endX_)) {
122                 return false;
123             }
124         }
125 
126         return !(y < startY_ && y < endY_
127                 || (y > startY_ && y > endY_));
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public boolean isEmpty() {
135         return Math.abs(startX_ - endX_) < EPSILON && Math.abs(startY_ - endY_) < EPSILON;
136     }
137 
138     @Override
139     public String toString() {
140         return "Line2D [ (" + startX_ + ", " + startY_ + "), (" + endX_ + ", " + endY_
141                 + "), " + (isVertical_ ? "isVertical" : "y = " + slope_ + "*x + " + yIntercept_ + "]");
142     }
143 }