Given a line formed by points Point1 and Point2, returns the length of the line. - Java 2D Graphics

Java examples for 2D Graphics:Line

Description

Given a line formed by points Point1 and Point2, returns the length of the line.

Demo Code

/*//w ww. j  a va  2 s. c o m
 * Copyright (c) 2001-2002 Regents of the University of California.
 * All rights reserved.
 *
 * This software was developed at the University of California, Irvine.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by the University of California, Irvine.  The name of the
 * University may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */
//package com.java2s;

import java.awt.Point;

public class Main {
    /**
     * Given a line formed by points Point1 and Point2, returns
     * the length of the line.
     */
    public static double lineLen(Point Point1, Point Point2) {
        return Math.sqrt(sq((Point2.getX() - Point1.getX()))
                + sq((Point2.getY() - Point1.getY())));

    }

    public static double lineLen(double x1, double y1, double x2, double y2) {
        return Math.sqrt(sq(x2 - x1) + sq(y2 - y1));

    }

    /**
     * Calculates a square root
     */
    public static double sq(double val) {
        return val * val;
    }
}

Related Tutorials