DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26

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

  1. #16
    Join Date
    Dec 2003

    Oops... Sorry

    Affirmative. I checked the expression several times for accuracy before I responded and I'm extremely disappointed that I failed to catch that mistake. :(


  2. #17
    Join Date
    Jun 2006
    Hi Evac, nah don't be disappointed.. everyone make mistake right? And im extremely grateful that you response to my thread which lead to me solving my problem. Once more thanks for your help and everyone else too!


  3. #18
    Join Date
    Dec 2003
    Is it possible you could post the code behind this calculation? I'd be interested in seeing how it looks, that is, if you have already completed it.


  4. #19
    Join Date
    Jun 2006
    pt1 = x2 - x1;
    pt2 = y2 - y1;
    pt3 = x4 - x3;
    pt4 = y4 - y3;

    angle = ( (pt1 * pt3) + (pt2 * pt4) )/((sqrt(pt1*pt1 + pt2*pt2)) * (sqrt(pt3*pt3 + pt4*pt4)) );

    result = acos(angle) *180 /PI;

    EVAC, here the code.

    One more question, since there are a total of 4 points which is 2 lines. So the angle will be calculated from which line to which line? And what is the calculation order of the angle (clockwise, anti-clockwise)? Also if i were to swap the points around, will the result still be consistent?

    Help help! thanks
    Last edited by larree; 07-03-2006 at 09:38 AM.

  5. #20
    Join Date
    Dec 2003
    Which way the angle is calculated shouldn't really matter. You can only get the same angle but either positive or negative (same absolute value). Just a matter of which line is used for which point... basically just a matter of convention. Here is website explaining a dot product of vectors and the angle in between Website . Keep in mind we are using a vector representation for the lines which is OK since all we really care about is the orientation of the lines.

  6. #21
    Join Date
    Jul 2006
    hey laree,
    what about finding the slopes with respect to the x-axis using
    . find the slope of each line.
    now slope m1=tan @1. similarly find tan@2=m2.
    now find @1 and @2 and add them
    or simply use
    angle between lines @=tan-1( (m1+m2)/1-m1m2)
    hope ur problems r over!!!!!
    Last edited by ardentmanufan; 07-13-2006 at 09:13 AM. Reason: error

  7. #22
    Join Date
    Jun 2006
    cool thanks for your feedback i get what you mean. By using this formula will i be able narrow down to calculate the angle by clockwise direction only?

  8. #23
    Join Date
    Jun 2006
    Hi everyone, having problem again.. Anyone knows how to calculate the angle in only either clockwise or anti clockwise direction only? Cause now when i calculate the angle two different line facing different direction pop out with the same angle! Help Help help!

  9. #24
    Join Date
    Jun 2006
    evac how should i do in order to get angle in a fixed direction?
    Example will be 270 and 90 instead of both 90.. cheers

  10. #25
    Join Date
    Apr 2012

    Easy angle check of a Vector

    Quote Originally Posted by larree View Post
    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
    I don't believe your system will work about 360 degrees, however if you are suggesting something like this, where a & b are two vectors & you are looking for the angle between them;

    float theta = (float)( (a.x* b.x) + (a.y* b.y) );
    float aScale = Mathf.Pow( (Mathf.Sqrt(a.x) + Mathf.Sqrt(a.y)) , 0.5f );
    float bScale = Mathf.Pow( (Mathf.Sqrt(b.x) + Mathf.Sqrt(b.y)) , 0.5f );
    float angle = Mathf.Cos( theta / (aScale * bScale) );
    return angle;

    then this won't work because getting the power of 0.5 on a negative number will be an imaginary number (as will getting the square root of a negative number)

    I've found the best thing to do is work out the angle of each vector seperatly & then check them against each other as bellow;

    //do this for each vector
    float radians = Mathf.Atan2(testVector.y, testVector.x);
    float degrees = radians * Mathf.Rad2Deg;

    You could also rotate the second vector by the first and then do this, the result is the same but most of the time knowing the angle acording to the x-axis is useful

    Hope this helps =]

  11. #26
    Join Date
    Apr 2012

    keep it simple

    looking at this a little bit simpler, conceptually:
    1. determine which point on each line is closer to the axes origin by finding minimum distance (vector length) [ d = sqrt (x^2 + y^2) ] -- refer to the Pythagorean theorem; furthest point will be the (x2,y2) in steps 2-4, closest point becomes (x1,y1) in next step.
    2. determine slope of the line as mentioned above [ m = (y2-y1)/(x2-x1) ] for each line.
    3. use the slope (m) and (x2,y2) for each line to calculate new (x1,y1), AKA y-intercept, where x=0 [ y1 = y2 - ( m*x2 ) ].
    4. Each line now has points of [(0,y1),(x2,y2)] that reflect original angles.
    5. Translate each line "down" the Y axis so that the first point sits atop the axis origin (0,0), yielding (x3 = x2; y3 = y2-y1 where y1 is >= 0 and y3 = y2 + y1 where y1 < 0), giving points [(0,0), (x3,y3)].
    6. Angle for each line in degrees is [ result = atan2 (y3,x3) * 180 / PI; ] as mentioned previously.
    7. Use sign of each (x3,y3) to determine quadrant for each line and adjust the sign of each [ result ] to calculate the difference correctly.

    Yes, this can be optimized, but I find it easier to optimize once I guarantee I am getting the correct answer for all conditions. HTH!

Similar Threads

  1. Replies: 4
    Last Post: 02-20-2010, 04: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, 08: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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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