469,270 Members | 1,151 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

distance and angle between 2 xy coordinates

how would i get the distance and angle between 2 points on the screen.
For example:
what is the distance between (100,50) and (250,70)
what i really wanna do is see how far the current mouse position is
from a given object and what the angle is between them.
Any help please?
Thanks,
Gil

Jun 15 '06 #1
8 17511

giloosh wrote:
how would i get the distance and angle between 2 points on the screen.
For example:
what is the distance between (100,50) and (250,70)
what i really wanna do is see how far the current mouse position is
from a given object and what the angle is between them.
Any help please?
Thanks,
Gil


Ok, i know how to find the distance using pythagoras theory.
How do i find the angle once i have all 3 lengths of a triangle?

Jun 15 '06 #2
giloosh wrote on 15 jun 2006 in comp.lang.javascript:
how would i get the distance and angle between 2 points on the screen.
For example:
what is the distance between (100,50) and (250,70)
what i really wanna do is see how far the current mouse position is
from a given object and what the angle is between them.


If we stipulate pixels as being square,
[that is the same distance horizontally as vertically]:
<script type='text/javascript'>

r = Math.atan2(50-70,250-100)

alert(r + ' radians')

d = r *180 / Math.PI

alert(d + ' degrees')

</script>

I hope this is right!

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 15 '06 #3
giloosh wrote on 15 jun 2006 in comp.lang.javascript:

giloosh wrote:
how would i get the distance and angle between 2 points on the screen.
For example:
what is the distance between (100,50) and (250,70)
what i really wanna do is see how far the current mouse position is
from a given object and what the angle is between them.
Any help please?
Thanks,
Gil


Ok, i know how to find the distance using pythagoras theory.
How do i find the angle once i have all 3 lengths of a triangle?


You only need to know tWo, if the triangle has one 90 degree corner.

See my other posting.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 15 '06 #4
Evertjan. wrote:
giloosh wrote on 15 jun 2006 in comp.lang.javascript:
how would i get the distance and angle between 2 points on the screen.
For example:
what is the distance between (100,50) and (250,70)
what i really wanna do is see how far the current mouse position is
from a given object and what the angle is between them.

There can't be an 'angle' between points, there can be between vectors
or you can calculate a direction or bearing from one to the other.

If we stipulate pixels as being square,
[that is the same distance horizontally as vertically]:
<script type='text/javascript'>

r = Math.atan2(50-70,250-100)
Depending ... if the OP wants the direction from the first point to the
second, then:

r = Math.atan2(70-50, 250-100)

is required. More thought may be needed - see below.

alert(r + ' radians')

d = r *180 / Math.PI

alert(d + ' degrees')

</script>

I hope this is right!


Guessing that the OP really wanted the direction from the first point
to the second, and in a mathematic system (zero extends to the right,
90 degrees is straight up) then it's nearly "right".

The general solution using atan2 for the direction from A to B is:

dx = Bx - Ax
dy = By - Ay
r = atan2(dy, dx)

The OPs example is A=(100,50) and B=(250,70) so the solution should be:

r = atan2( 70-50, 250-100)

Which is about 0.13 radians or 7.6 degrees.

Left at that, some results will be negative (e.g. from (0,0) to (1,-1)
gives -45 degrees). Depending on the OPs requirements, that might be
OK but it likely will confuse humans. If a system is needed where
directions are always positive, then add:

if (r < 0) r += Math.PI * 2;

Now -45 degrees will be 315 degrees and directions continuously
increase from zero to 360.

The above is based on a mathematic Cartesian system, what about if a
normal mapping system is required, where zero and north are straight
up, 90 degrees and east are to the right and bearings increase
clockwise? The sense is opposite and the origin is rotated 90 degrees
- but the fix is easy: just swap dx and dy in the atan2 expression:

r = atan2(dx, dy)

And you're done. The example points above give a bearing from A to B
of 82.4 degrees, which is 90-7.6. :-)
--
Rob

Jun 16 '06 #5
RobG wrote on 16 jun 2006 in comp.lang.javascript:
if (r < 0) r += Math.PI * 2;


Generalizing I use:

while (r < 0) r += Math.PI * 2;

or:

while (d < 0) d += 360;

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 16 '06 #6
Evertjan. wrote:
RobG wrote on 16 jun 2006 in comp.lang.javascript:
if (r < 0) r += Math.PI * 2;


Generalizing I use:

while (r < 0) r += Math.PI * 2;

or:

while (d < 0) d += 360;


Is there any chance that the result of atan2() will ever be greater than
2π? The ECMAScript spec 15.8.2.5 says:

"The result is expressed in radians and ranges from −π to +π"
--
Rob
Jun 16 '06 #7
RobG wrote on 16 jun 2006 in comp.lang.javascript:
Evertjan. wrote:
RobG wrote on 16 jun 2006 in comp.lang.javascript:
if (r < 0) r += Math.PI * 2;


Generalizing I use:

while (r < 0) r += Math.PI * 2;

or:

while (d < 0) d += 360;


Is there any chance that the result of atan2() will ever be greater than
2π? The ECMAScript spec 15.8.2.5 says:

"The result is expressed in radians and ranges from −π to +π"


Could be, I cannot tell, since the string from −π to +π" isn ot
meaningfull on my reader.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 16 '06 #8
Evertjan. wrote:
RobG wrote on 16 jun 2006 in comp.lang.javascript:
Evertjan. wrote:
RobG wrote on 16 jun 2006 in comp.lang.javascript:

if (r < 0) r += Math.PI * 2;
Generalizing I use:

while (r < 0) r += Math.PI * 2;

or:

while (d < 0) d += 360;

Is there any chance that the result of atan2() will ever be greater than
2π? The ECMAScript spec 15.8.2.5 says:

"The result is expressed in radians and ranges from −π to +π"


Could be, I cannot tell, since the string from −π to +π" isn ot
meaningfull on my reader.

From -pi to +pi.
--
Rob
Jun 17 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Gilles Lenfant | last post: by
4 posts views Thread by DellaCroce | last post: by
2 posts views Thread by laredotornado | last post: by
11 posts views Thread by devnew | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.