473,883 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to calculate angle of a line

8,435 Recognized Expert Expert
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 #1
19 36463
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Trignometry :)
you know the cordinates of the points,
Get the height and the width of the that line and use tan

angle = tan inverse ( X / Y)
Attached Images
File Type: jpg tan.jpg (5.4 KB, 7479 views)
Oct 17 '07 #2
Killer42
8,435 Recognized Expert Expert
Trignometry :)
you know the cordinates of the points,
Get the height and the width of the that line and use tan

angle = tan inverse ( X / Y)
Thanks. Haven't done a lot of trig for a few years.

I had already worked out the horizontal and vertical distance (X and Y), and I routinely use these to calculate the straight-line distance (good old Pythagorus), but didn't know how to work out the angle.

I'm still not sure. I need to be able to go the full circle. That is, angles all the way around to 360 degrees (or just under, since that would be the same as zero of course). Will this work? Not sure what you mean by "inverse", in this context. (Be gentle, it has been a long time...)
Oct 17 '07 #3
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
I'm still not sure. I need to be able to go the full circle. That is, angles all the way around to 360 degrees (or just under, since that would be the same as zero of course). Will this work?
Depends on your frame of reference (my frame of reference is the x axis)

Not sure what you mean by "inverse", in this context. (Be gentle, it has been a long time...)
Well... lets assume that we now see @ as theta (the general angle variable)
tan @ = X/Y;
so @ = tan inverse (X/Y)
i have to type in inverse, because i cant type in -1 in superscript :D

Hope the math comes along well for you :D

cheers
Oct 17 '07 #4
bartonc
6,596 Recognized Expert Expert
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.
Here's how I'd do it:
Expand|Select|Wrap|Line Numbers
  1. >>> from math import atan, degrees
  2. >>> bottom, left = 0, 0
  3. >>> top, right = 10.0, 20.0
  4. >>> degs = degrees(atan((top - bottom)/(right - left)))
  5. >>> print degs
  6. 26.5650511771
  7. >>> 
Oct 17 '07 #5
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. Double radians = Math.Atan((x2-x1)/(y2-y1));
  2. Double degrees = radians *  180 / Math.PI;
Thats c# :D
Oct 18 '07 #6
Killer42
8,435 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 (lower-right = 0, upper-right = 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.
Oct 18 '07 #7
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Well...I would say calculate the degrees,
the values of X=(x2-x1) and Y=(y2-y1) will indicate which quadrant the angle is being measured to.


Assume that the quadrants are being split as in picture.
X(sign)__Y(sign ) ___ Quadrant
__+_______+____ ____ 0
__+_______- ________1
__-_______-_________2
__-_______+_______ __3

(Excuse the undescores, as that gets trimmed off in the posts)

The angle is measured always to the X axis, so then apply the required arithmetic to get the desired angle from your frame of ref, and angle of ref (ie, whether clockwise or anti-clockwise).
Attached Images
File Type: jpg quadrants.jpg (2.6 KB, 1279 views)
Oct 18 '07 #8
Killer42
8,435 Recognized Expert Expert
Thanks Shashi. That's pretty much what I'm doing. I've just complicated it a little by using the absolute values of X and Y to begin with. I plan to change that.
Oct 18 '07 #9
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
I would suggest to keep the values of X and Y as they are.
the angles yould come as positive or negative depending which quadrant there are in (0 and 2 will be positive and 1,3 negative)

Soall you then need to find which qadrant they are in
and then
(90 * (quadrant number +1)) + angle = resulting required angle

the resulting angle starts from the X axis at quadrant 0, and works it was anti clock wise from there.

cheers :) (woo hoo,, i never knew i could start liking trignometry.... no...wait, i started avoiding it when my games designer friend came to ask me doubts :P)
Oct 18 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3135
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
2
7242
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).
2
11239
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?
2
7142
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...
15
46497
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((10-20)/(10-20)) = 45 degrees. When I draw this...
8
17831
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
4
6396
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
6
2470
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);
4
7469
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 angle-brackets 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
0
9934
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, 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...
0
9789
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,...
0
11137
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, 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...
1
10840
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10409
tracyyun
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, Z-Wave, Wi-Fi, 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...
1
7969
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4608
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
2
4214
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.