Calculating angle between two lines (using C++)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Calculating angle between two lines (using C++)

  1. #1
    Join Date
    Jun 2006
    Posts
    17

    Calculating angle between two lines (using C++)

    I have a total of four x and y coordinates.
    Two of which will be joined as one line and the other two as another line. The problem now is how to find the angle between these two lines?
    Is there any pre defined function which allows me to calculate the angle?

    [:confused:]
    help! thanks in advance

  2. #2
    Join Date
    Oct 2005
    Posts
    173
    since you are in 2-d, shift a line so that it shares a common end point with the other.
    or find the point where the two lines intersect.
    Once you end up with 2 lines and 3 points, it's going to be pretty easy to figure out.

  3. #3
    Join Date
    Jun 2006
    Posts
    17
    Erm yeah sound good but is there any pre defined function in c++ language which allow me to calculate the angle between the two line or any idea which you think i could use to calculate it?

    cheers mate

  4. #4
    Join Date
    Oct 2005
    Posts
    173
    of course.....sine, cosine are both predefined and allows you to calculate the angle.
    as for idea, I gave you two options already.

  5. #5
    Join Date
    Apr 2006
    Location
    Zagreb, Croatia
    Posts
    152
    I think the easisest way is to calculate the angle of each line with respect to the X axis. Then simply subtract the calculated angles. To calculate the angles with respect to the x axis use alpha = tan(dy/dx) so if your line's start point is (20,20) and the last point is (80,30) the tan equals 10/60 which is 9,4 degrees. Use the same for another line and then subtract the angles.
    Note that you will have to add 90 degrees for obtuse angles

  6. #6
    Join Date
    Jun 2006
    Posts
    17
    Great mate... thanks everyone for your input.. ivan guess i get your idea..
    If there are easier option do post... thanks alot

    cheers

  7. #7
    Join Date
    Dec 2003
    Posts
    38

    Try this.

    You may like this solution using dot products between two vectors. Suppose you have two lines where the first line passes through the points (x1,y1), (x2,y2), and the second line passes through the points (x3,y3) and (x4,y4). Take the differences between the x and y coordinates for both lines. a = x2 - x1, b = y2 - y1, c = x4 - x3, and d = y4 - y3. The so called dot product yields the equation

    Cosine( Theta ) = ( (a x c) + (b x d) )/(Sqrt(a^2 + b^2) x Sqrt(c^2 + d^2) ) = Z.

    The angle you'd be interested in is Theta or the angle between the two lines and that can be found using the inverse cosine or Theta = ArcCos( Z ) where of course Z is the somewhat complicated expression involving a,b,c,d. Probably be difficult for you to find a class that contains an interface that readily accomplish this for you. You could just code it up in alot less time. Hope you find this useful.

    EVAC
    Last edited by evac-q8r; 07-02-2006 at 09:51 AM.

  8. #8
    Join Date
    Jun 2006
    Posts
    17
    Hi Evac,

    With your idea can i apply the formula regardless of where the two lines is? Even without any contact point at all? Sorry my calculus skill abit poor.

    And also i can directly get the angle by inverse cosine Z?
    where z = ( (a x c) + (b x d) )/(Sqrt(a^2 + b^2) x Sqrt(b^2 + d^2) ) ?

    Thanks alot.

    cheers

  9. #9
    Join Date
    Dec 2003
    Posts
    38

    Yes

    That is correct (for all three questions). Even if the point of intersection between the lines are unknown given they are in the same plane in 2d (guarantees intersection unless parallel) and not 3d (may skew instead)
    space. As long as your points for each line are determined by the same coordinate system or axes then the formula is valid. This equation unlike the prior one suggested you don't need to worry about adding or subtracting any angles relative to some axes. If interested look up Scalar Products of Vectors and you should see some pages describing this concept.

    EVAC

  10. #10
    Join Date
    Jun 2006
    Posts
    17
    Cool... guess i know how to go about tackling this problem thanks alot everyone

    cheers :)

  11. #11
    Join Date
    Dec 2003
    Posts
    3,366
    You might look at atan2 to get the corrected angle if you find that you need this.

  12. #12
    Join Date
    Jun 2006
    Posts
    17
    erm hi jonnin care to elaborate? I don't quite understand. thanks

  13. #13
    Join Date
    Dec 2003
    Posts
    3,366
    Just read the help file on atan2 for a complete description. In a nutshell you can lose what quadrant the angle was in by using inverse trig functions (for some types of problems). You can correct for this manually of course, but the built in atan2 function corrects it for you. Depending on what you are doing, it may or may not be important to use this function, but at least be aware that it exists.

  14. #14
    Join Date
    Jun 2006
    Posts
    17
    roger that thanks

  15. #15
    Join Date
    Jun 2006
    Posts
    17
    Hi everyone just to clarify again the formula

    Cosine( Theta ) = ( (a x c) + (b x d) )/(Sqrt(a^2 + b^2) x Sqrt(b^2 + d^2) ) = Z.

    is correct?

    Or is the second part of the sqrt should be (c^2 + d^2) instead of Sqrt(b^2 + d^2)?

    please advise... thanks

Similar Threads

  1. Replies: 4
    Last Post: 02-20-2010, 03:17 AM
  2. Replies: 0
    Last Post: 07-18-2005, 09:46 AM
  3. How long before the next version??
    By _CAG in forum .NET
    Replies: 146
    Last Post: 08-12-2002, 10:40 PM
  4. Re: App Object (fixes)
    By Rob Teixeira in forum .NET
    Replies: 129
    Last Post: 06-06-2002, 05:23 AM
  5. Viewable lines in a rich text box?
    By Sean Woods in forum VB Classic
    Replies: 0
    Last Post: 02-04-2002, 07:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center