944,168 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7521
  • Java RSS
Nov 17th, 2006
0

Calculating distance between pixels

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
anti_genius is offline Offline
27 posts
since Nov 2006
Nov 17th, 2006
1

Re: Calculating distance between pixels

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
Last edited by peter_budo; Nov 17th, 2006 at 7:39 am.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Nov 17th, 2006
0

Re: Calculating distance between pixels

Click to Expand / Collapse  Quote originally posted by peter_budo ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
p.bondam is offline Offline
17 posts
since Nov 2006
Nov 17th, 2006
0

Re: Calculating distance between pixels

Click to Expand / Collapse  Quote originally posted by p.bondam ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
p.bondam is offline Offline
17 posts
since Nov 2006
Nov 17th, 2006
0

Re: Calculating distance between pixels

yeah that's correct forgot about it :mrgreen:
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Nov 24th, 2006
0

Re: Calculating distance between pixels

Hi,

Thanks for your help. It worked.

Regards.
Reputation Points: 10
Solved Threads: 0
Light Poster
anti_genius is offline Offline
27 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Convert an image to *.ico format
Next Thread in Java Forum Timeline: Adding mouse listener to a frame





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC