Java Geometry Algorithm nearestColinearPoint(final double x1, final double y1, final double x2, final double y2, double x, double y)

Here you can find the source of nearestColinearPoint(final double x1, final double y1, final double x2, final double y2, double x, double y)

Description

Returns the point on the given line segment which is closest to the given point .

License

Apache License

Parameter

Parameter Description
x1 <var>x</var> value of the first point on the line.
y1 <var>y</var> value of the first point on the line.
x2 <var>x</var> value of the last point on the line.
y2 <var>y</var> value of the last point on the line.
x <var>x</var> value of a point close to the given line.
y <var>y</var> value of a point close to the given line.

Return

The nearest point on the given line.

Declaration

public static Point2D.Double nearestColinearPoint(final double x1, final double y1, final double x2,
        final double y2, double x, double y) 

Method Source Code

//package com.java2s;
/*/*from   w w w. j  a  va2  s .com*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.geom.Point2D;

public class Main {
    /**
     * Returns the point on the given {@code line} segment which is closest to the given {@code point}.
     * Let {@code result} be the returned point. This method guarantees (except for rounding errors) that:
     *
     * <ul>
     *   <li>{@code result} is a point on the {@code line} segment. It is located between the
     *       {@linkplain Line2D#getP1() P1} and {@linkplain Line2D#getP2() P2} ending points
     *       of that line segment.</li>
     *   <li>The distance between the {@code result} point and the given {@code point} is
     *       the shortest distance among the set of points meeting the previous condition.
     *       This distance can be obtained with {@code point.distance(result)}.</li>
     * </ul>
     *
     * @param  x1 <var>x</var> value of the first point on the line.
     * @param  y1 <var>y</var> value of the first point on the line.
     * @param  x2 <var>x</var> value of the last  point on the line.
     * @param  y2 <var>y</var> value of the last  point on the line.
     * @param  x  <var>x</var> value of a point close to the given line.
     * @param  y  <var>y</var> value of a point close to the given line.
     * @return The nearest point on the given line.
     *
     * @see #colinearPoint(double,double , double,double , double,double , double)
     *
     * @todo This method is used by Geotk (a sandbox for code that may migrate to SIS), but not yet by SIS.
     *       We temporarily keep this code here, but may delete or move it elsewhere in a future SIS version
     *       depending whether we port to SIS the sandbox code.
     */
    public static Point2D.Double nearestColinearPoint(final double x1, final double y1, final double x2,
            final double y2, double x, double y) {
        final double slope = (y2 - y1) / (x2 - x1);
        if (!Double.isInfinite(slope)) {
            final double y0 = (y2 - slope * x2);
            x = ((y - y0) * slope + x) / (slope * slope + 1);
            y = x * slope + y0;
        } else {
            x = x2;
        }
        if (x1 <= x2) {
            if (x < x1)
                x = x1;
            if (x > x2)
                x = x2;
        } else {
            if (x > x1)
                x = x1;
            if (x < x2)
                x = x2;
        }
        if (y1 <= y2) {
            if (y < y1)
                y = y1;
            if (y > y2)
                y = y2;
        } else {
            if (y > y1)
                y = y1;
            if (y < y2)
                y = y2;
        }
        return new Point2D.Double(x, y);
    }
}

Related

  1. makeCornerTo(GeneralPath gp, Point2D cornerPoint, Point2D nextCornerPoint, float radius)
  2. makeLine(Point2D.Double center, Point2D.Double north, Point2D.Double east)
  3. makeNeighbor(Point theHex, int direction)
  4. manhattanDistance(Point a, Point b)
  5. mirrorMoveVertically(Point move, int size)
  6. nearestColinearPoint(final Line2D segment, final Point2D point)
  7. nearestPointOnLine(Line2D l, Point2D p, boolean clampToSegment, Point2D dest)
  8. newZeroPoint()
  9. oneThirdPoint(@Nonnull Point2D pA, @Nonnull Point2D pB)