Can someone give me a code example or direct me to a good website how to rotate a line in gdi+?
I'm using visual basic 2005 express edition
Printable View
Can someone give me a code example or direct me to a good website how to rotate a line in gdi+?
I'm using visual basic 2005 express edition
Create a new project and add a Timer to your form. Set the Timer's Interval property to 1000 and its Enabled property to True. Add the following code:Now run your project and watch a line behave like the second hand on a clock.vb Code:
'The start point of the line. Private start As New Point(100, 100) 'The end point of the line. Private [end] As Point 'The angle of the line, in degrees, with respect to the horizontal X axis. Private angle As Integer = 270 'The distance between the start and end points of the line. Private distance As Integer = 100 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 'Rotate the line six degrees, equivalent to one second on a clock. Me.angle += 6 If Me.angle >= 360 Then 'Reset the angle. Me.angle = 0 End If 'Force the form to repaint. Me.Refresh() End Sub Private Sub Form1_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint 'Set the end point for the line. Me.SetEndPoint() 'Draw the line. e.Graphics.DrawLine(Pens.Black, Me.start, Me.end) End Sub Private Sub SetEndPoint() 'Convert the angle to radians. Dim angle As Double = Me.DegreesToRadians(Me.angle) 'Calculate the distance between the start and end points in the X direction. Dim xDistance As Double = Math.Cos(angle) * distance 'Calculate the distance between the start and end points in the Y direction. Dim yDistance As Double = Math.Sin(angle) * distance 'Set the coordinates of the end point based on the start point and the distance in each direction. Me.end = New Point(Convert.ToInt32(Math.Round(xDistance) + Me.start.X), _ Convert.ToInt32(Math.Round(yDistance) + Me.start.Y)) End Sub Private Function DegreesToRadians(ByVal angle As Integer) As Double Return angle * Math.PI / 180 End Function
thx:)
ths again for that code example it works verry good, but i tried to adjust it so there should be an other line of a lenght of lets say 300 that goes from
the end of the seconds pen to the Y-coordinate of the beginning of the secondspen but the x coordinate changes all the time so that the lenght of 300 stays the same. But now my problem is that the second line changes size automatically(so it doesn't stay 300) could you help me? this is my code
Code:Public Class Form1
Private start As New Point(300, 300)
Private end2 As Point
'The end point of the line.
Private [end] As Point
'The angle of the line, in degrees, with respect to the horizontal X axis.
Private angle As Integer = 270
'The distance between the start and end points of the line.
Private distance As Integer = 70
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Rotate the line six degrees, equivalent to one second on a clock.
Me.angle += 15
If Me.angle >= 360 Then
'Reset the angle.
Me.angle = 0
End If
'Force the form to repaint.
Me.Refresh()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
'Set the end point for the line.
Me.SetEndPoint()
Me.setendpoint2()
'Draw the line.
e.Graphics.DrawLine(Pens.Black, Me.start, Me.end)
e.Graphics.DrawLine(Pens.Red, Me.end, Me.end2)
End Sub
Private Sub SetEndPoint()
'Convert the angle to radians.
Dim angle As Double = Me.DegreesToRadians(Me.angle)
'Calculate the distance between the start and end points in the X direction.
Dim xDistance As Double = Math.Cos(angle) * distance
'Calculate the distance between the start and end points in the Y direction.
Dim yDistance As Double = Math.Sin(angle) * distance
'Set the coordinates of the end point based on the start point and the distance in each direction.
Me.end = New Point(Convert.ToInt32(Math.Round(xDistance) + Me.start.X), _
Convert.ToInt32(Math.Round(yDistance) + Me.start.Y))
End Sub
Private Sub SetEndPoint2()
'Calculate the distance between the start and end points in the X direction.
Dim angle As Double = Me.DegreesToRadians(Me.angle)
Dim xDistance As Double = (250 ^ 2 - (Math.Sin(angle) * distance) ^ 2) ^ (1 / 2)
'Calculate the distance between the start and end points in the Y direction.
Dim yDistance As Double = 300
'Set the coordinates of the end point based on the start point and the distance in each direction.
Me.end2 = New Point(Convert.ToInt32(Math.Round(xDistance) + 300), _
Convert.ToInt32(Math.Round(yDistance)))
End Sub
Private Function DegreesToRadians(ByVal angle As Integer) As Double
Return (angle * Math.PI / 180)
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class