How to calculate radial angles in a circle? 
Author Message
 How to calculate radial angles in a circle?

Dear All,
VB6.
I have Shape1 with Shape property of "Circle" drawn in the Picture1 control.
I want to move the Label1.Caption="X" over the Shape1 and I want to display
in the Text1.Text the angle that the point "X" (Label1) is making in relation
to the centre of the Shape1 (circle).
Any suggestion on how to calculate the angle?
Thank you very much for your comments in advance.
DORI


Mon, 13 Jun 2011 15:11:00 GMT  
 How to calculate radial angles in a circle?
If you can get the X and Y difference (d) between the shape centre and the
point on the radius, the angle can be calculated using ATAN2(dY, dX).

See the definition of ATAN2 at
http://www.vb-helper.com/howto_atan2.html

and how to use it at
http://en.wikipedia.org/wiki/Atan2


Quote:
> Dear All,
> VB6.
> I have Shape1 with Shape property of "Circle" drawn in the Picture1
> control.
> I want to move the Label1.Caption="X" over the Shape1 and I want to
> display
> in the Text1.Text the angle that the point "X" (Label1) is making in
> relation
> to the centre of the Shape1 (circle).
> Any suggestion on how to calculate the angle?
> Thank you very much for your comments in advance.
> DORI



Mon, 13 Jun 2011 16:09:50 GMT  
 How to calculate radial angles in a circle?


Quote:
> I have Shape1 with Shape property of "Circle" drawn in the Picture1 control.
> I want to move the Label1.Caption="X" over the Shape1 and I want to display
> in the Text1.Text the angle that the point "X" (Label1) is making in relation
> to the centre of the Shape1 (circle).
> Any suggestion on how to calculate the angle?
> Thank you very much for your comments in advance.
> DORI

If you want 0 at 12 o'clock, advancing clockwise, you can use this:

LFS

Function FindAngle(ByVal CX As Single, _
                   ByVal CY As Single, _
                   ByVal PX As Single, _
                   ByVal PY As Single, _
                   Optional Degrees As Boolean = True) As Single
' INPUT
' CX, CY    Center of a circle
' PX, PY    Point for desired angle
' Degrees   Units in Radians or Degrees

' OUTPUT
' Angle     Oriented clockwise with 0 at 12 o'clock

Dim DX As Single, DY As Single, Ang As Single, Atn1 As Single
   ' Init variables
   Atn1 = Atn(1)
   DX = PX - CX
   DY = PY - CY
   ' Avoid divide by 0 error
   If DX = 0 Then DX = 1E-20
   ' Find arctangent + (orient to 12 o'clock)
   Ang = Atn(DY / DX) + (Atn1 * 2)
   ' Adjust for quadrant
   If PX < CX Then Ang = Ang + Atn1 * 4
   ' Return value
   If Degrees Then
      FindAngle = Ang * 45 / Atn1
   Else
      FindAngle = Ang
   End If
End Function



Mon, 13 Jun 2011 18:04:28 GMT  
 How to calculate radial angles in a circle?
Sometimes the answers to Qs like this can be very complex for a novice to
follow, so to answer your question in the very simplist programing language
there is some code below as a demo to give you what you want.
On a form put a shape control as a circle, a label and a text box then paste
the code. The function in this demo returns the correct angle between to
sets of X-Y co-ords passed to it, In this case the args passed to the
function are:
CenterX of shape
CenterY of shape
CenterX of label
CenterY of label

To convert the textbox text to what you want uncomment the If-Then statement
in the Form_MouseMove event.
Hope this helps

Ivar

The Code:

Option Explicit

Private Sub Form_Load()
Shape1.Move Me.ScaleWidth / 4, Me.ScaleHeight / 4, _
Me.ScaleWidth / 2, Me.ScaleWidth / 2
Label1.Caption = "X"
Label1.AutoSize = True
End Sub

Private Sub Form_MouseMove(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Dim TheAngle As Single

Label1.Move X - (Label1.Width / 2), Y - (Label1.Height / 2)

TheAngle = GetAngle(Shape1.Left + (Shape1.Width / 2), _
            Shape1.Top + (Shape1.Height / 2), _
            Label1.Left + (Label1.Width / 2), _
            Label1.Top + (Label1.Width / 2))
'At this point the Val of TheAngle is correct
'To Convert to what you want uncomment next 5 lines
'If TheAngle < 90 Then
'TheAngle = 90 - TheAngle
'Else:
'TheAngle = 450 - TheAngle
'End If

Text1.Text = TheAngle
End Sub

Private Function GetAngle(P1X As Single, P1Y As Single, _
P2X As Single, P2Y As Single) As Single
Dim RatioX As Single
Dim RatioY As Single
Dim QuadrantNum As Integer
RatioX = (P1X - P2X)
RatioY = (P1Y - P2Y)
'Stop Division by Zero Error
If RatioY = 0 Then
    If RatioX > 0 Then
    GetAngle = 180: Exit Function
    Else:
    GetAngle = 0: Exit Function
    End If
ElseIf RatioX = 0 Then
    If RatioY > 0 Then
    GetAngle = 90: Exit Function
    Else:
    GetAngle = 270: Exit Function
    End If
End If

If RatioX > 0 Then
    If RatioY > 0 Then
    QuadrantNum = 2 'Top Left Corner
    Else:
    QuadrantNum = 3 'Bottom Left Corner
    End If
Else:
    If RatioY > 0 Then
    QuadrantNum = 1 'Top Right Corner
    Else:
    QuadrantNum = 4 'Bottom Right Corner
    End If
End If

GetAngle = Atn(RatioX / RatioY)

Select Case QuadrantNum
Case 1, 2: GetAngle = 90 + (GetAngle * 180 / 3.1415)
Case 3, 4: GetAngle = 270 + (GetAngle * 180 / 3.1415)
End Select
End Function



Mon, 13 Jun 2011 19:08:52 GMT  
 How to calculate radial angles in a circle?


Quote:
> I have Shape1 with Shape property of "Circle" drawn in the Picture1 control.
> I want to move the Label1.Caption="X" over the Shape1 and I want to display
> in the Text1.Text the angle that the point "X" (Label1) is making in relation
> to the centre of the Shape1 (circle).
> Any suggestion on how to calculate the angle?

FYI, Both solutions from Ivar and myself assume VB's normal coordinate
system, where (0,0) indicates the upper left corner of the surface.

If you are working in a cartesian system where (0, 0) is the center of the
circle, the values from both solutions will not be accurate.

To convert my routine to work in the cartesian system (including
the orientation you wanted), change this line:

   Ang = Atn(DY / DX) + (Atn1 * 2)

To:

   Ang = (Atn1 * 2) - Atn(DY / DX)

I'll leave Ivar to convert his routine if he wishes....  ;-)

HTH
LFS



Mon, 13 Jun 2011 20:25:31 GMT  
 How to calculate radial angles in a circle?
Hi Larry and Ivar,
Thank you very much for the solutions and the code. It worked great.
One more question: from each angle I want to know the distance of the Label1
from the centre of the circle, how can I do that?
Thanks again very much for your help.
DORI


Wed, 15 Jun 2011 11:05:01 GMT  
 How to calculate radial angles in a circle?


Quote:
> Hi Larry and Ivar,
> Thank you very much for the solutions and the code. It worked great.
> One more question: from each angle I want to know the distance of the Label1
> from the centre of the circle, how can I do that?

Pythagorean theorem.

A^2 + B^2 = C^2

That would equate to:

DX ^ 2 + DY ^ 2  =  XY ^ 2

Or:

XY = Sqr(DX * DX + DY * DY)

LFS



Wed, 15 Jun 2011 14:10:57 GMT  
 How to calculate radial angles in a circle?


Quote:
> Hi Larry and Ivar,
> Thank you very much for the solutions and the code. It worked great.
> One more question: from each angle I want to know the distance of the
> Label1
> from the centre of the circle, how can I do that?
> Thanks again very much for your help.
> DORI

Here are two functions, Both do the same thing and return the same result.
One is easy to understand and the other is a head scratcher.

Ivar

Private Function GetDistance1(P1X As Single, P1Y As Single, _
P2X As Single, P2Y As Single) As Single
Dim DistanceX As Single
Dim DistanceY As Single
Dim DistanceZ As Single
'Calc Distance between 2 X points
DistanceX = P1X - P2X
'Calc Distance between 2 X points
DistanceY = P1Y - P2Y
'Convert both nums to square of themselves
DistanceX = DistanceX * DistanceX
DistanceY = DistanceY * DistanceY
'Add the two together
DistanceZ = DistanceX + DistanceY
'Squareroot of distanceZ is answer
GetDistance1 = Sqr(DistanceZ)
End Function

Private Function GetDistance2(P1X As Single, P1Y As Single, _
P2X As Single, P2Y As Single) As Single
GetDistance2 = Sqr(((P1X - P2X) ^ 2) + ((P1Y - P2Y) ^ 2))
End Function



Wed, 15 Jun 2011 19:26:33 GMT  
 How to calculate radial angles in a circle?
Dear Ivar and Larry,
Thanks again very much for the solutions to my questions. You have been very
helpful.
Happy New Year!
DORI
Quote:



> > Hi Larry and Ivar,
> > Thank you very much for the solutions and the code. It worked great.
> > One more question: from each angle I want to know the distance of the
> > Label1
> > from the centre of the circle, how can I do that?
> > Thanks again very much for your help.
> > DORI

> Here are two functions, Both do the same thing and return the same result.
> One is easy to understand and the other is a head scratcher.

> Ivar

> Private Function GetDistance1(P1X As Single, P1Y As Single, _
> P2X As Single, P2Y As Single) As Single
> Dim DistanceX As Single
> Dim DistanceY As Single
> Dim DistanceZ As Single
> 'Calc Distance between 2 X points
> DistanceX = P1X - P2X
> 'Calc Distance between 2 X points
> DistanceY = P1Y - P2Y
> 'Convert both nums to square of themselves
> DistanceX = DistanceX * DistanceX
> DistanceY = DistanceY * DistanceY
> 'Add the two together
> DistanceZ = DistanceX + DistanceY
> 'Squareroot of distanceZ is answer
> GetDistance1 = Sqr(DistanceZ)
> End Function

> Private Function GetDistance2(P1X As Single, P1Y As Single, _
> P2X As Single, P2Y As Single) As Single
> GetDistance2 = Sqr(((P1X - P2X) ^ 2) + ((P1Y - P2Y) ^ 2))
> End Function



Wed, 22 Jun 2011 08:11:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. How to calculate the radial angle in a circle shape?

2. Calculating angles of triangles

3. need help from a mathematic-calculate angles and coordinates

4. Calculate Angle

5. Testing for Radial Button status

6. Radial buttons

7. Radial declare ?

8. calculate dates calculate time

9. HELP! How to calculate angles?

10. How do you calculate angle from its Tangent in VB (Tan-1)?

11. Draw Text on an angle

12. change the angle of a control in the form

 

 
Powered by phpBB® Forum Software