Hi all.
Sorry to be brief, but have to leave.
If I have the coordinates of two points (on the computer screen), how do I calculate the angle of the line which joins them?
I know, my terms are vague and in some cases completely wrong (for instance it would be an "interval", not a line) but hopefully you get the idea.
Oct 17 '07
19 36463
Ok, thanks for that.
One thing, though. This is something which keeps cropping up here lately. The word is "questions" , not "doubts".
Oh, no it was doubts,
Because he was good at geometry and trignometry, and he would come and pile everything up on me, and explaining the humungus geometry, seemed that he would almost recreate the entire earth in detail.
And expected me to follow him, and call out when the error is occouring !!
so doubts...not questions !!! (questions would have been easier than doubts)
bartonc 6,596
Recognized Expert Expert
Thanks for that, people.
Since I need to cover the full circle (in other words, the angle could be up to 360 degrees), I've had to do some real kludges. Hope someone can point out a better way.  Calculate (absolute) DistanceX & DistanceY
 Calculate Ratio: DistanceX / DistanceY
 Calculate Degrees = Atn(Ratio) * RadsToDegs
This provides a value between 0 and 90, which is not good enough. So...  Determine which "quadrant" we're in (lowerright = 0, upperright = 1, upper left = 2, lower left = 3) by checking signs of horizontal & vertical distances.
 Adjust the angle based on the quadrant.
 Quadrant 0: 360  Degrees
 Quadrant 1: 90  Degrees
 Quadrant 2: 90 + Degrees
 Quadrant 3: 270  Degrees
 And that's it. Simple, huh. :)
This does produce what looks like the right answer, or close enough. But as you can see, it's rather messy so far.
Note, using the absolute values of the distances dates back to before you people provided the Atan solution, so I'll be revisitng that to see what I can do better.
I developed these for my Garmin GPS interface: 

def QuadHeading(east, north):

"""Given the vector, convert to degrees in a quadrant.

N is zero, S is 180, E is positive, W is negitive."""

return degrees(atan2(east, north))


def QuadToCirc(heading):

"""Convert quadrant to degrees of a circle."""

return (heading, abs(heading + 360))[heading < 0] # index the list on bool
For future reference, here's the function I ended up putting together in VB6. You'll note that I've defined named constants for everything rather than hardcoding any values. This is based on the fact that constants have traditionally been faster to reference than variables. I don't actually know whether this is still the case in VB6.  Option Explicit

DefLng AZ


Private DistanceX As Single, DistanceY As Single

Private Const Zero As Long = 0

Private Const NotQuiteZero As Single = 0.00001

'Private Const One As Long = 1

'Private Const Two As Long = 2

'Private Const Three As Long = 3

Private Const Ninety As Long = 90

Private Const TwoSeventy As Long = 270

Private Const Pi As Single = 3.14159265358979

Private Const RadsToDegs As Single = 180 / Pi


Public Function DirectionOfLine(ByVal From_X As Single, ByVal From_Y As Single, ByVal To_X As Single, ByVal To_Y As Single) As Single

' Given two points, return the angle of the line from P1 to P2.

DistanceX = To_X  From_X

DistanceY = To_Y  From_Y

If DistanceY = Zero Then DistanceY = NotQuiteZero ' Prevent divbyzero error.

If DistanceY < Zero Then

DirectionOfLine = Ninety + Atn(DistanceX / DistanceY) * RadsToDegs

Else

DirectionOfLine = TwoSeventy + Atn(DistanceX / DistanceY) * RadsToDegs

End If

End Function
Actually, here's a shorter (and probably very slightly faster) version...  Option Explicit

DefLng AZ


Private DistanceX As Single, DistanceY As Single

Private Const NotQuiteZero As Single = 0.00001

Private Const Ninety As Long = 90

Private Const TwoSeventy As Long = 270

Private Const RadsToDegs As Single = 57.29578



Public Function DirectionOfLine2(ByVal From_X As Single, ByVal From_Y As Single, ByVal To_X As Single, ByVal To_Y As Single) As Single

' Given two points, return the angle of the line from P1 to P2.

If From_Y = To_Y Then To_Y = From_Y  NotQuiteZero ' Prevent divbyzero error.

If To_Y < From_Y Then

DirectionOfLine2 = Ninety + Atn((To_X  From_X) / (To_Y  From_Y)) * RadsToDegs

Else

DirectionOfLine2 = TwoSeventy + Atn((To_X  From_X) / (To_Y  From_Y)) * RadsToDegs

End If

End Function
Hi all.
Sorry to be brief, but have to leave.
If I have the coordinates of two points (on the computer screen), how do I calculate the angle of the line which joins them?
I know, my terms are vague and in some cases completely wrong (for instance it would be an "interval", not a line) but hopefully you get the idea.
Try arctan((y2y1)/(x2x1))
By the way, I read all of the replies and every one of them is wrong. The tangent of an angle is the change in Y divided by the change in X. The change in X divided by the change in Y is the cotangent!!!
Try arctan((y2y1)/(x2x1))
Um... isn't that what I've got?
By the way, I read all of the replies and every one of them is wrong. The tangent of an angle is the change in Y divided by the change in X. The change in X divided by the change in Y is the cotangent!!!
Thanks for the correction. I have to admit though, all I'm really concerned about is whether the code works. Which it does.
Since this is the first link that pops up on Google when I search how to do this, I thought I'd include a much easier method.  Double radians = Math.Atan2((y2y1), (x2x1));

Double degrees = radians * 180 / Math.PI;


if(degrees < 0) {

degrees += 360; // degrees are now 0360

}
recently today my friend and I worked this out originally I thought of using the four quadrants of a graph to get the angle, but the way I had done it ended with me having to build a large table to get values back.
so after about an hour we came up with this:
cos^1(adjacent/hypotenuse) this translates to being: let one point be your origin and the other point be the x and y values you get the hypotenuse by obviously using the Pythagorean theorem. So lets say the coordinates are (0,0) and (4,4) using the formula above it would look like this.
cos^1(4/sqrt(4^2+4^2)) = 45 degrees which is correct since the slope of the line is 1/1.
hope this helps,
Verrazano
Cowpie, what language is that? VB.Net?
I am/was working in VB6. By the way, this question is from 4 years ago. :)
Sign in to post your reply or Sign up for a free account.
Similar topics 
by: Darren Grant 
last post by:
Hi there,
I've attempted to implement an Angle class. An Angle is a subset of an
integer, where the range is [0,360). All other operations should be
permitted.
The code works, I think... except (for example) a = b + 10;
needs to be a = b + (Angle) 10;
Could some kind soul comment on my code and show me how it could be

by: Dennis Myrén 
last post by:
Hi.
Sorry if this post might be a little off the topic,
but just in case anyone knows...
Given a size, a position, a start angle and a sweep angle,
i want to draw a bezier curve.
I will need to compute the bezier control points
(start point, point A, point B, point C).

by: Joe 
last post by:
I need to add a trend line to a scatter plot (not automatically supported by
the charting package) but I don't know how to calculate it. Is there any
formulas around for calculating what the trend line should look like?

by: The Mess 
last post by:
I am using VB5.
I wish I had a function to draw a line, giving the angle and length but I do
not have the math skills involved to do this. Does anyone have a function
that they care to share that does this?
I was thinking something like:
Public Sub DrawLine(X1 as single, Y1 as single, Len as single, Angle as
single, byref X2 as single, byref Y2 as single)
and have X2 and Y2 assigned in the Sub (rather than just a line drawn 
because I...

by: cwsullivan 
last post by:
Hi gang,
I have a grid full of particles in my program, and I want to find an
angle between two particles. I'm having trouble because it seems like
the answer depends on whether or not the target particle is above or
below, in front or behind the refernce particle.
If I have a reference particle at (10,10), and another particle at
(20,20), i'm currently finding the angle by:
angle = atan((1020)/(1020)) = 45 degrees. When I draw this...
 
by: giloosh 
last post by:
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

by: Jem 
last post by:
Hi all
I have the following code which draws a vertical line on a form...
Dim VertCrossHairX1 As Integer = 75
Dim VertCrossHairX2 As Integer = 75
Dim VertCrossHairY1 As Integer = 70
Dim VertCrossHairY2 As Integer = 80

by: royrana 
last post by:
Hello,
I am working on a problem in which a particle hits a wall and gets reflected.
I need to calculate the tangential velocity and the impact angle with the wall.
I wrote this:
/*for tangential velocity*/
real vi = sqrt(p>state.V* p>state.V
+ p>state.V* p>state.V
+ p>state.V* p>state.V);

by: Keith Hughitt 
last post by:
Hi all,
I am using someone else's script which expects input in the form of:
./script.py <arg1arg2
I was wondering if the anglebrackets here have a special meaning? It
seems like
they specify an input and output stream to use in place of the
console. I could not

by: marktang 
last post by:
ONU (Optical Network Unit) is one of the key components for providing highspeed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...

by: Hystou 
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
 
by: Oralloy 
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bitfields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++12 std=c++20 Wnarrowing bit_field.cpp
Here is the code in...

by: tracyyun 
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, ZWave, WiFi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...

by: agi2029 
last post by:
Let's talk about the concept of autonomous AI software engineers and nocode agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...

by: conductexam 
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...

by: TSSRALBI 
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LANtoLAN VPNs.
The last exercise I practiced was to create a LANtoLAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

by: 6302768590 
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
 
by: muto222 
last post by:
How can i add a mobile payment intergratation into php mysql website.
 