How to get distance between two points in Android?

Math.hypot(x1 - x2, y1 - y2)

should perform a bit faster.

Math.hypot


Finding distance between 2 points is not android specific:

    double x1 = event.getX(1);
    double y1 = event.getY(1);
    double x2 = event.getX(0);
    double y2 = event.getY(0);
    double x = Math.sqrt(Math.pow(x2 - x1, 2) - Math.pow(y2 - y1, 2));

Its not just Android..

We remember from trigonometry class that distance between two points can be calculated using Pythagorean Theorem as demonstrated here

But in code basically what you want is as follows:

double d = Math.sqrt(Math.pow(event.getX(1) - event.getX(0), 2) + Math.pow(event.getY(1) - event.getY(0), 2));

Where d is distance between two points

Tags:

Android

Point