get Closer Point - Java java.lang

Java examples for java.lang:Math Geometry

Description

get Closer Point

Demo Code

/*//from ww w  .j a  v  a2  s . c o m
 * Copyright (c) 2014 tabletoptool.com team.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     rptools.com team - initial implementation
 *     tabletoptool.com team - further development
 */
//package com.java2s;

import java.awt.geom.Line2D;

import java.awt.geom.Point2D;

public class Main {
    public static Point2D getCloserPoint(Point2D origin, Line2D line) {

        double dist1 = Math.hypot(origin.getX() - line.getP1().getX(),
                origin.getY() - line.getP1().getY());
        double dist2 = Math.hypot(origin.getX() - line.getP2().getX(),
                origin.getY() - line.getP2().getY());

        return dist1 < dist2 ? line.getP1() : line.getP2();
    }
}

Related Tutorials