Calculates the distance between two points - Android Graphics

Android examples for Graphics:Path Point

Description

Calculates the distance between two points

Demo Code


//package com.java2s;

import android.graphics.PointF;

public class Main {
    /**/*w w  w.  ja v  a 2 s .  c o m*/
     * Calculates the distance between two points
     * @param p1 First point
     * @param p2 Second point
     * @return The distance between p1 and p2
     */
    public static double distance(PointF p1, PointF p2) {
        PointF vector = subtract(p1, p2);
        return Math.sqrt(vector.x * vector.x + vector.y * vector.y);
    }

    /**
     * Subtracts one point from another
     * @param p1 First point
     * @param p2 Second point
     * @return The difference between p1 and p2 (i.e., p1 - p2)
     */
    public static PointF subtract(PointF p1, PointF p2) {
        return new PointF(p1.x - p2.x, p1.y - p2.y);
    }
}

Related Tutorials