Asaf Nevo
Asaf Nevo

Reputation: 11688

android how many pixels between 2 points

i want to calculate how many pixels there are between 2 points on my screen.

I've seen that i can draw a straight line between 2 points using Path class, but i don't really want to draw that line, i only want to know how long is it in pixels..

i really need it for my MapView clusters implementation..

i can get each marker position on screen with no problem, but don't know how to calculate pixel's "distance" between them ... i know that there are cluster's implementations available but i want to try and create one of my own

help will be appreciated :)

Upvotes: 1

Views: 2219

Answers (1)

ScouseChris
ScouseChris

Reputation: 4397

This is very straightforward using a bit of algebra :)

Take the co-ordinates of both points and calculate the difference between their x and y values e.g.

dx = p1.x - p2.x;
dy = p1.y - p2.y;

distance = Math.sqrt( (dx * dx) + (dy * dy) );

Where p1 and p2 are the points you want get find the distance between, and distance is the result. It will be a double but you can round it to the nearest int if you wish

Upvotes: 9

Related Questions