Segment.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.geometry.euclidean.twod;

  22. import org.hipparchus.util.FastMath;

  23. /** Simple container for a two-points segment.
  24.  */
  25. public class Segment {

  26.     /** Start point of the segment. */
  27.     private final Vector2D start;

  28.     /** End point of the segment. */
  29.     private final Vector2D end;

  30.     /** Line containing the segment. */
  31.     private final Line     line;

  32.     /** Build a segment.
  33.      * @param start start point of the segment
  34.      * @param end end point of the segment
  35.      * @param tolerance of the line.
  36.      */
  37.     public Segment(final Vector2D start, final Vector2D end, final double tolerance) {
  38.         this(start, end, new Line(start, end, tolerance));
  39.     }

  40.     /** Build a segment.
  41.      * @param start start point of the segment
  42.      * @param end end point of the segment
  43.      * @param line line containing the segment
  44.      */
  45.     public Segment(final Vector2D start, final Vector2D end, final Line line) {
  46.         this.start  = start;
  47.         this.end    = end;
  48.         this.line   = line;
  49.     }

  50.     /** Get the start point of the segment.
  51.      * @return start point of the segment
  52.      */
  53.     public Vector2D getStart() {
  54.         return start;
  55.     }

  56.     /** Get the end point of the segment.
  57.      * @return end point of the segment
  58.      */
  59.     public Vector2D getEnd() {
  60.         return end;
  61.     }

  62.     /** Get the line containing the segment.
  63.      * @return line containing the segment
  64.      */
  65.     public Line getLine() {
  66.         return line;
  67.     }

  68.     /**
  69.      * Get the length of the line segment.
  70.      *
  71.      * @return line segment length.
  72.      */
  73.     public double getLength() {
  74.         return getEnd().distance(getStart());
  75.     }

  76.     /** Calculates the shortest distance from a point to this line segment.
  77.      * <p>
  78.      * If the perpendicular extension from the point to the line does not
  79.      * cross in the bounds of the line segment, the shortest distance to
  80.      * the two end points will be returned.
  81.      * </p>
  82.      *
  83.      * Algorithm adapted from:
  84.      * <a href="http://www.codeguru.com/forum/printthread.php?s=cc8cf0596231f9a7dba4da6e77c29db3&amp;t=194400&amp;pp=15&amp;page=1">
  85.      * Thread @ Codeguru</a>
  86.      *
  87.      * @param p to check
  88.      * @return distance between the instance and the point
  89.      */
  90.     public double distance(final Vector2D p) {
  91.         final double deltaX = end.getX() - start.getX();
  92.         final double deltaY = end.getY() - start.getY();

  93.         final double r = ((p.getX() - start.getX()) * deltaX + (p.getY() - start.getY()) * deltaY) /
  94.                          (deltaX * deltaX + deltaY * deltaY);

  95.         // r == 0 => P = startPt
  96.         // r == 1 => P = endPt
  97.         // r < 0 => P is on the backward extension of the segment
  98.         // r > 1 => P is on the forward extension of the segment
  99.         // 0 < r < 1 => P is on the segment

  100.         // if point isn't on the line segment, just return the shortest distance to the end points
  101.         if (r < 0 || r > 1) {
  102.             final double dist1 = getStart().distance(p);
  103.             final double dist2 = getEnd().distance(p);

  104.             return FastMath.min(dist1, dist2);
  105.         }
  106.         else {
  107.             // find point on the line and see if it is in the line segment
  108.             final double px = start.getX() + r * deltaX;
  109.             final double py = start.getY() + r * deltaY;

  110.             final Vector2D interPt = new Vector2D(px, py);
  111.             return interPt.distance(p);
  112.         }
  113.     }
  114. }