Hi,

Can someone tell me the code to calculate the distance between two pixel points.

If i enter Point(x1,y1) and Point(x2,y2) then i get length of the line between them (in pixels).

Thanks.

Dani AI

Generated

As pointed out and clarified, the straight-line (Euclidean) distance is the right idea for "length in pixels." The replies already show the basic approach; below are practical alternatives, gotchas, and small optimizations that help when this is used in real code.

For a concise and robust single-call solution use Java’s built-in hypot function:

double dist = Math.hypot(x1 - x2, y1 - y2);

When you only need to compare distances (for example: “is point A within radius R of B?”), avoid the cost of a square root by comparing squared distances instead:

int dx = x1 - x2;
int dy = y1 - y2;
long dist2 = (long) dx * dx + (long) dy * dy;
if (dist2 <= (long) r * r) {
    // inside radius
}

Notes and cautions:

  • If coordinates can be large (or negative), cast before squaring to avoid integer overflow — use long or double.
  • Java GUI/image coordinates typically use the top-left as (0,0) and y grows down; the math is unchanged, but be mindful if you convert between device and world coordinates.
  • If coordinates are fractional (transforms, sub-pixel positions), keep them as double. If you only care about grid steps, Manhattan distance (abs(dx) + abs(dy)) can be faster and sometimes more appropriate.
  • For exact pixel counts convert with Math.round when you need an integer result. For tight loops, precompute any constants (e.g., r*r) and profile if performance matters.

OP confirmed the simple approach worked; these notes are intended to make that solution safer and more flexible in production code.

Recommended Answers

All 5 Replies

ever heard of Pythagoras' Theorem " a on power of two plus b on power of two equals to c on power of two"?
just example

(x1 - x2 ) = x           // your a
(y1 - y2) = y            // your b

if ( x < 0) then x * (-1)
if ( y < 0) then y * (-1)

x^2 + y^2 = z^2       // z is your c distance between pixels    this ^2 on power of two

then get square root

ever heard of Pythagoras' Theorem " a on power of two plus b on power of two equals to c on power of two"?
just example

(x1 - x2 ) = x           // your a
(y1 - y2) = y            // your b

if ( x < 0) then x * (-1)
if ( y < 0) then y * (-1)

x^2 + y^2 = z^2       // z is your c distance between pixels    this ^2 on power of two

then get square root

lleave out the multiplying with -1 : x^2 is eual to (-x)^2

lleave out the multiplying with -1 : x^2 is eual to (-x)^2

in other words :

double a = P.x - Q.x;
double b = P.y - Q.y;
double distance = Math.sqrt(a * a + b * b);

and on you go

yeah that's correct forgot about it :mrgreen:

Hi,

Thanks for your help. It worked.

Regards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.