473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Drawing a compass

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??!

Thanks

Aug 17 '06 #1
8 5886
Try Graphics.DrawPie. Just pass a really small value for the sweepAngle
parameter (something like 0.0001 should do the trick)

/claes

"Hugh Janus" <my*************@hotmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>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??!

Thanks

Aug 17 '06 #2
Hugh Janus wrote:
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

Aug 17 '06 #3
Hugh Janus wrote:
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
Thanks, this seems to work (sort of!). I have one question. In your
post, you say that West is 90 degrees, but it should be 270. Why is
that?

Hugh

Aug 18 '06 #4
Hugh Janus wrote:
Thanks, this seems to work (sort of!). I have one question. In your
post, you say that West is 90 degrees, but it should be 270. Why is
that?
No reason, that's just the way I did it. If you need to have it work a
different way, you need to change the way the translated angle is
computed. The formula for polar coordinates that is used assumes that
0 degrees is to the East.

So If you want 270 degrees to be West, then the translated angle should
be 180. I think the formula for the translated angle would be this:

Dim translatedAngle As Double
If angle <= 90 Then
translatedAngle = 90.0F - angle
Else
translatedAngle = (360.0F - angle) + 90.0F
End If

Aug 18 '06 #5
Hugh,
For a "speedometer" that I draw in one of my applications I use is
Graphics.TranslateTransform, followed by a couple of
Graphics.RotateTransform.

The TranslateTransform moves the "center" point to the middle of my control.

The first RotateTransform rotates orients the needle to the starting angle.

The second RotateTransform sets the position of the needle:

Something like (untested, extracted code):

gr.TranslateTransform(CSng(Me.ClientSize.Width) / 2,
CSng(Me.ClientSize.Height) / 2)
gr.RotateTransform(180)

Dim length As Integer = Math.Max(Me.ClientSize.Width \ 2,
Me.ClientSize.Height \ 2)

gr.RotateTransform(Value)

gr.DrawLine(pen, 0, 0, length, 0)

Instead of DrawLine, you could use DrawPath or something fancy if you wanted
a fanciful needle.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Hugh Janus" <my*************@hotmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
|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??!
|
| Thanks
|
Aug 20 '06 #6
Chris Dunaway wrote:
>
So If you want 270 degrees to be West, then the translated angle should
be 180. I think the formula for the translated angle would be this:

Dim translatedAngle As Double
If angle <= 90 Then
translatedAngle = 90.0F - angle
Else
translatedAngle = (360.0F - angle) + 90.0F
End If
Thanks this is working great now. 1 question, I've not seen it before
where you have that "F" next to a number. What does it mean?

Thanks, Jay, i'll keep your code in mind as well for a different part
of my code.

Hugh

Aug 21 '06 #7
Hugh Janus wrote:
Chris Dunaway wrote:

So If you want 270 degrees to be West, then the translated angle should
be 180. I think the formula for the translated angle would be this:

Dim translatedAngle As Double
If angle <= 90 Then
translatedAngle = 90.0F - angle
Else
translatedAngle = (360.0F - angle) + 90.0F
End If

Thanks this is working great now. 1 question, I've not seen it before
where you have that "F" next to a number. What does it mean?
I believe by default, numeric literals such as 90.0 would be a double
type. The F specifies the literal as type Single. I'm not sure if
they are necessary here.

Chris

Aug 21 '06 #8
Hugh,
| Thanks this is working great now. 1 question, I've not seen it before
| where you have that "F" next to a number. What does it mean?
As Chris suggests the "F" indicates a Single literal. I understand that the
"F" stands for Float, which traditionally has been the same size as
Single...

The list of literal type characters is available here:
http://msdn2.microsoft.com/en-us/library/s9cz43ek.aspx

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Hugh Janus" <my*************@hotmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
| Chris Dunaway wrote:
| >
| So If you want 270 degrees to be West, then the translated angle should
| be 180. I think the formula for the translated angle would be this:
| >
| Dim translatedAngle As Double
| If angle <= 90 Then
| translatedAngle = 90.0F - angle
| Else
| translatedAngle = (360.0F - angle) + 90.0F
| End If
|
| Thanks this is working great now. 1 question, I've not seen it before
| where you have that "F" next to a number. What does it mean?
|
| Thanks, Jay, i'll keep your code in mind as well for a different part
| of my code.
|
| Hugh
|
Aug 21 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tomi Holger Engdahl | last post by:
I am looking for a solution to add on-line drawing tool to a phpBB discussion board. The idea would be that the users can draw their own simple drawings with the tool and attach them easily as...
2
by: Champika Nirosh | last post by:
Hi, I want to create drawing board application that can draw Line, rectagle, circle and free hand drawing. Each drawing need to be transparent, moveable (draggable), have bring to front and...
1
by: Hadar | last post by:
Hi, I'm getting "object is currently in use elsewhere" when I use System.Drawing.Graphics.MesureString. This is what I do: My controls use a utility class the helps it to mesure strings. To...
7
by: Marc Pelletier | last post by:
Hello all, I have a class which includes a method to create a chart. I want to be able to call this method from asp.net code as well as windows application code, so I have sketched it out as...
5
by: Jerry J | last post by:
I want to use the System.Drawing.Image class. According to the help file, this is an abstract base class. Because it is supposedly abstract, I created another class that inherits from it. However,...
0
by: _mubashir | last post by:
Hello All, Merlin Data Compass version 2 is available now. It is a Web based OLAP data access, analysis and interactive reporting tool. It provides an organization with the ability to create a...
1
by: YouPoP | last post by:
I am doing an app (C# 2.0) where you can draw in a panel with your mouse in "real time". I actually have 2 problems; 1- it does not really is "real time", if your mouse move fast or very fast the...
2
by: ThatsIT.net.au | last post by:
I have this code that writes a pie chart in a asp.net page, but I want to use it in a server control. When I try I get a error on the last line "Response.OutputStream" Obviously there is no...
7
by: raylopez99 | last post by:
I have a logical drawing space much bigger than the viewport (the screen) and I'd like to center the viewport (the screen) to be at the center of the logical drawing space. After following the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.