473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 17811

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gilles Lenfant | last post by:
Hi, I got an element ( say <div id="foo">... ) at its natural position in a page. I need to get the distance (in pixels) between the top of that element and the top of the page/frame. Of...
4
by: DellaCroce | last post by:
Does anyone here have the formula for calculating distance give two pairs of Longitude/Latitude coordinates? Please share this with me if you would. -- Greg
9
by: Sandy | last post by:
Hello - I need either a cheap tool or code & DB that calculates, eg. within 50-mile radius of a zip code. Anyone have any suggestions? -- Sandy
1
by: radskate360 | last post by:
Hi I am newer to programming and need a bit of help with this program. OK, heres the directions. The distance between two places on earth can be calculated by using their latitudes and...
1
by: tiffrobe | last post by:
I'm a little lost on my program. Everything works fine except function 3. It gives out garbage numbers. Its suppose to give the distance between two points and then the area of 2 circles. ...
2
by: laredotornado | last post by:
Hi, I'm using php 4.4.4. Maybe I'm misreading the docs, but in the imagettfbbox manual (http://us2.php.net/imagettfbbox), it says that text coordinates are the same regardless of what the angle...
11
by: devnew | last post by:
hello while trying to write a function that processes some numpy arrays and calculate euclidean distance ,i ended up with this code (though i used numpy ,i believe my problem has more to do with...
6
by: Tom P. | last post by:
I am writing a drawing program but I want to keep the scale down (there could end up being several hundred objects on the screen). I want to limit the points collected to a certain distance from...
8
by: Sunny | last post by:
Hi, do someone know, How we can find the smallest distance between a bunch of lat 7 long? Like I have 10 Latitude & Longitude. -73.924598,40.879010 -73.924506,40.878978 -73.924506,40.878978...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.