get Distance To Center - Java java.lang

Java examples for java.lang:Math Geometry Distance

Description

get Distance To Center

Demo Code

/*/*from ww  w  .  j ava  2 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 double getDistanceToCenter(Point2D p, Line2D line) {

        Point2D midPoint = new Point2D.Double((line.getP1().getX() + line
                .getP2().getX()) / 2, (line.getP1().getY() + line.getP2()
                .getY()) / 2);

        return Math.hypot(midPoint.getX() - p.getX(),
                midPoint.getY() - p.getY());
    }
}

Related Tutorials