Hugh said:
I am trying to draw a compass on a form but my maths is not up to
scratch! I have the disc with the bearing markers but have no idea how
to calculate the needle line based on the desired degree.
Does anyone know what the formula is to create it? If my circle is 50
by 50 I need to plot a line starting in the centre and going out to the
outer circle and touching it at the x degrees mark. Ideas??!
Here is a function that takes a center point, a radius, and an angle in
degrees with North being 0, West being 90, South being 180 and East
being 270. It returns the point to draw the line to.
So if your Center is 50,50, and you want to draw a 50 unit line NW at
40 degrees, you would call it like this:
Dim center As New PointF(50.0F, 50.0F)
Dim endPoint As PointF = PointOnCircle(center, 50, 40)
g.DrawLine(Pens.Black, center, endPoint)
Private Function PointOnCircle(ByVal center As PointF, ByVal radius
As Double, ByVal angle As Double) As PointF
'NOTE: The center of the circle is passed in terms of GDI+
coordinates
'The angle passed in assumes North is 0 degrees. For the polar
coordinate calculations, we need to translate
'this so that East is zero:
Dim translatedAngle As Double = angle + 90.0
If translatedAngle > 360.0 Then
translatedAngle -= 360.0
End If
'Next convert degrees to radians
translatedAngle *= (Math.PI / 180.0)
'Next we need to calculate the polar coordinates:
Dim x As Double = radius * Math.Cos(translatedAngle)
Dim y As Double = radius * Math.Sin(translatedAngle)
'Convert doubles to Singles
Dim x2 As Single = Convert.ToSingle(x)
Dim y2 As Single = Convert.ToSingle(y)
'Finally create the PointF structure and adjust for the center
of the circle:
Return New PointF(x2 + center.X, -y2 + center.Y)
End Function
Hope this helps