Results 1 to 4 of 4

Thread: Line in a angle in gdi+

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    49

    Line in a angle in gdi+

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Line in a angle in gdi+

    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:
    vb Code:
    1. 'The start point of the line.
    2. Private start As New Point(100, 100)
    3. 'The end point of the line.
    4. Private [end] As Point
    5. 'The angle of the line, in degrees, with respect to the horizontal X axis.
    6. Private angle As Integer = 270
    7. 'The distance between the start and end points of the line.
    8. Private distance As Integer = 100
    9.  
    10. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    11.     'Rotate the line six degrees, equivalent to one second on a clock.
    12.     Me.angle += 6
    13.  
    14.     If Me.angle >= 360 Then
    15.         'Reset the angle.
    16.         Me.angle = 0
    17.     End If
    18.  
    19.     'Force the form to repaint.
    20.     Me.Refresh()
    21. End Sub
    22.  
    23. Private Sub Form1_Paint(ByVal sender As Object, _
    24.                         ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    25.     'Set the end point for the line.
    26.     Me.SetEndPoint()
    27.  
    28.     'Draw the line.
    29.     e.Graphics.DrawLine(Pens.Black, Me.start, Me.end)
    30. End Sub
    31.  
    32. Private Sub SetEndPoint()
    33.     'Convert the angle to radians.
    34.     Dim angle As Double = Me.DegreesToRadians(Me.angle)
    35.     'Calculate the distance between the start and end points in the X direction.
    36.     Dim xDistance As Double = Math.Cos(angle) * distance
    37.     'Calculate the distance between the start and end points in the Y direction.
    38.     Dim yDistance As Double = Math.Sin(angle) * distance
    39.  
    40.     'Set the coordinates of the end point based on the start point and the distance in each direction.
    41.     Me.end = New Point(Convert.ToInt32(Math.Round(xDistance) + Me.start.X), _
    42.                        Convert.ToInt32(Math.Round(yDistance) + Me.start.Y))
    43. End Sub
    44.  
    45. Private Function DegreesToRadians(ByVal angle As Integer) As Double
    46.     Return angle * Math.PI / 180
    47. End Function
    Now run your project and watch a line behave like the second hand on a clock.
    Last edited by jmcilhinney; Mar 8th, 2007 at 09:31 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    49

    Re: Line in a angle in gdi+

    thx

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    49

    Re: Line in a angle in gdi+

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width