Returns a square distance of two points. - Java java.lang

Java examples for java.lang:Math Geometry Distance

Description

Returns a square distance of two points.

Demo Code

/**/*from w  ww  .  j a  v  a2s . co m*/
 * Copyright [2014] Gaurav Gupta
 *
 * Licensed 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.
 */
//package com.java2s;
import java.awt.Point;

public class Main {
    /**
     * Returns a square distance of two points.
     *
     * @param p1 the first point
     * @param p2 the second point
     * @return the square distance
     */
    public static double distanceSq(Point p1, Point p2) {
        int w = p2.x - p1.x;
        int h = p2.y - p1.y;
        return Math.sqrt(w * w + h * h);
    }
}

Related Tutorials