|
-
Jun 18th, 2012, 05:02 PM
#1
Plotting points in a circle
I'm trying to create a watch. Which is a pain in my (3 letter word for rear). I've all ready sucessfuly drawn the numbers on the watches face with Boops help:
Code:
Public Class Form1
Dim P As New Pen(Brushes.Red, 1)
Dim a As Integer = 12
Dim b As Integer = 2
Dim Angle As Single = CSng(b / a * Math.PI)
Dim drawingMargin As Integer = CInt(Me.Width / 6)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SetClientSizeCore(400, 400)
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.SmoothingMode = SmoothingMode.HighQuality
Dim Rect As Rectangle = Me.ClientRectangle
Rect.Inflate(-drawingMargin, -drawingMargin)
Using mypen As New Pen(Color.Blue, 1)
For i As Integer = 1 To a Step 1
Dim Centre As New Point(Me.ClientSize.Width \ 2, Me.ClientSize.Height \ 2)
Dim outerRadius As Single = Centre.X - drawingMargin / 2.0F
Dim outerX, outerY As Single
outerX = Centre.X + CSng(outerRadius * Math.Sin(Angle * i))
outerY = Centre.Y - CSng(outerRadius * Math.Cos(Angle * i))
Dim number As String = i.ToString
Dim numberSize As SizeF = e.Graphics.MeasureString(number, Me.Font)
Dim textLocation As New PointF(outerX - numberSize.Width / 2.0F, outerY - numberSize.Height / 2.0F)
e.Graphics.DrawString(i.ToString, Me.Font, Brushes.Black, textLocation)
Next i
End Using
e.Graphics.DrawEllipse(Pens.Black, Me.ClientRectangle)
End Sub
problem is, I don't know how this math works inside the loop, and well outside it too. I know that the circumfrance of a circle is pi times radius squared,the radius is half the distance, and that the center point is 1/2 of the width and height. Also, I found out that the angle of a triangle from the center to 2 numbers is 30 degrees(360/12). Could y'all kinda help me out on this one? I did terrible in highschool math, and I just wanna understand it :P
-
Jun 19th, 2012, 12:02 PM
#2
Re: Plotting points in a circle
This is basic basic geometry, barely touching into elementary trig. The only thing that's not particularly intuitive is their calculation Angle field. The Sin() and Cos() functions apparently take input in Radians, not degrees (I don't code in VB.net, so I can't tell you for sure). The Angle then is a calculation of the radians between tho hour points on a standard clock, which is one twelfth of the full circle. Since a full circle is 2*PI radians, one twelfth of that is 2*PI * 1 / 12, which equals (2 / 12) * PI = 2 / 12 * PI. Whoever wrote this code simply wrote the algorithm to compute this value in a manner which seems to obfuscate the purpose. It computes the correct value, but maybe only by accident.
Now then, inside of the loop, the basic calculation is to find the (X,Y) values of the point on the perimeter of the circle, relative to the center of the circle, which is located at (numberSize.Width / 2.0F, numberSize.Height / 2.0F). To calculate these points, you use Sin() and Cos(). Remember the SOHCAHTOA acronym from middle or high school? This is where that comes in handly. Normally, to calculate the Y value, you use Sin(), and to calculate the X value you use Cos(). However, you'll note that this code is using Sin() to calculate X and Cos() to calculate Y, which is backwards. This is because the 'zero-angle' in trigonometry normally refers to what on a clock would be three o'clock. Compounding that problem, the Y-axis in Cartesian space normally counts up from bottom to top, but in most computer charting utilities, it counts up from top to bottom. To rotate the zero-angle counterclockwise a quarter turn and then turn the clock up-side down, the code swaps the use of Sin and Cosine. Again, this isn't intuitive or explained in the code, but that is what's going on.
-
Jun 19th, 2012, 12:42 PM
#3
Re: Plotting points in a circle
Wow, thank you so much. I failed math, and just wanted to understand the algo going on inside the loop. You've helped me out so much.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|