Drawing radial lines in Visual Basic PDF Print E-mail
Written by Joris Bijvoets   
Tuesday, 05 January 2010 18:35

How to draw radial lines in VB.net

Drawing radial lines in Visual Basic is quite easy using simple maths. The following code offers an tutorial example for drawing a sun:

drawing radial lines in visual basic

 

I use the form's canvas to draw upon. Just for the picture, I changed the background of the form.


Dim Graph As Graphics = Me.CreateGraphics()

'Define some variables to draw the radial lines
Dim x1 As Integer, x2 As Integer, y1 As Integer, y2 As Integer

'Define some more variables

Dim innerRadius As Integer = 50
Dim outerRadius As Integer = 150

Dim xCenter As Integer = Me.Width / 2
Dim yCenter As Integer = Me.Height / 2

Dim rad As Single


'Draw the filled ellipse

Graph.FillEllipse(Brushes.Yellow, xCenter - 40, yCenter - 40, 80, 80)

'Define a Pen to draw with

Dim MyPen As New Pen(Color.Yellow, 5)

MyPen.DashStyle = DashStyle.Dash

'Draw the radial lines in a loop
For a = 0 To 360 Step 20 'Angle in degrees

rad = a / 180 * Math.PI 'Sinus and Cosinus use radians, so convert the degrees to radians


'Now calculate begin and end positions of the radial lines

x1 = xCenter + Math.Sin(rad) * innerRadius
x2 = xCenter + Math.Sin(rad) * outerRadius

y1 = yCenter + Math.Cos(rad) * innerRadius
y2 = yCenter + Math.Cos(rad) * outerRadius

'At last, draw the radial lines

Graph.DrawLine(MyPen, x1, y1, x2, y2)

Next


 

Last Updated on Friday, 26 November 2010 19:03