Results 1 to 11 of 11

Thread: [RESOLVED] Finding an Angle

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2006
    Location
    Sheffield, UK
    Posts
    35

    Resolved [RESOLVED] Finding an Angle

    Hey, i was hoping to get some Help with Finding Angles between two Points, i have got the numbers moving a little with math.atan etc..

    Like this:

    (SEE ATTACHMENT)


    The Grey Line from the Point in the Center to the Mouse Pointer is what i need to calculate. Numbers are as you see in the picture

    Someone can help me out?..Endless and Pointless hours trying so far

    - Cheers in Advanced.. L3mmy
    Attached Images Attached Images  
    uhm, it wasn't me..

    Prove it..

    Prove it was me........

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Finding an Angle

    Well, first you need the lengths of the sides of the triangle:

    diffX = abs(X1-X2)
    diffY = abs(Y1-Y2)

    Now, the Tangent of the angle is equal to diffY (opposite side) / diffX (adjacent side). So if you take the ArcTan of (diffY/diffX), you will get a number that will be the angle in radians. To turn that into degrees, multiply by 180 and divide by pi. Thus, the whole equation looks like:

    angle in degrees = (180 * Math.ATan(math.abs((Y1-Y2)/(X1-X2))))/3.1415

    That will get you pretty close. A more accurate value for pi would give you more significant figures, but this will get you accurate to about three decimal places.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2006
    Location
    Sheffield, UK
    Posts
    35

    Re: Finding an Angle

    I don't seem to beable to get it working Could you show me a little Example..
    uhm, it wasn't me..

    Prove it..

    Prove it was me........

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Finding an Angle

    How about you show us your attempt?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2006
    Location
    Sheffield, UK
    Posts
    35

    Re: Finding an Angle

    Don't Know what your Trying to Imply on me there but I'm not too Good at maths..or Angles..and here's what i got..It doesn't Return the Right Value..From the Center of the Form, to the Cursor Position.

    Code:
    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
            
            Dim Angle As Double
    
            Angle = (180 * Math.Atan(Math.Abs((Cursor.Position.Y - Me.Height / 2) / (Cursor.Position.X - Me.Width / 2)))) / 3.1415
    
            Label2.Text = Angle
    
    End Sub
    uhm, it wasn't me..

    Prove it..

    Prove it was me........

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Finding an Angle

    You need to be aware that the positive Y direction on your form is downwards, where your calculations are assuming that it's upwards.

    You should also be aware that the middle of the form's client area, which excludes the border and title bar, would be found using the ClientSize, not the Height and Width.

    You should also be using the Math.PI constant to convert radians to degrees.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Finding an Angle

    Another point to note, which I forgot to mention, is that Cursor.Position is in screen coordinates, not form coordinates.

    I would suggest that you start off by making sure you can calculate the correct opposite and adjacent values before you worry about anything else. ALWAYS break your problem down and solve the first bits first. The PointToClient method of your form will be useful.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Finding an Angle

    You should also note that you're never going to get an angle outside the of 0-90 the way you're doing it. You'd have to use the signs of the opposite and adjacent sides specifically to determine which quadrant you're in.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Finding an Angle

    Hmmm... I see that this thread is now resolved. It's customary to provide the final solution for those who may have the same question in future. It's also considered polite to tell people that the problem is solved and thanks for the help.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [RESOLVED] Finding an Angle

    I would guess that a combination of your post #6 and #8 solved the issue.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Member
    Join Date
    Sep 2006
    Location
    Sheffield, UK
    Posts
    35

    Re: [RESOLVED] Finding an Angle

    Sorry i didn't reply..I ended up asking a Friend on MSN..He let me use some Code of his..
    uhm, it wasn't me..

    Prove it..

    Prove it was me........

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width