Results 1 to 8 of 8

Thread: [2005] draw a line

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    27

    [2005] draw a line

    The following is the code that executes on the click of button1. My problem is that my line is not drawing correctly. I have tested the output of the values that points the line in the correct direction. They are coming out correct. However, when the line draws, at an angle the angle is going in the wrong direction.

    i.e. a 45 degree angle is drawing at an angle of 305

    Any help is greatly appreciated.


    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim ofd As New OpenFileDialog()
    
            Dim MyValue() As String
    
            x1 = 300
            y1 = 300
    
            ofd.InitialDirectory = "c:\"
            ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    
            If ofd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Using sr As New System.IO.StreamReader(ofd.FileName)
                    Do
                        MyValue = sr.ReadLine.Split
                        Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
                        Dim formGraphics As System.Drawing.Graphics
                        formGraphics = Me.CreateGraphics()
                        length = Convert.ToDouble(MyValue(0))
                        angle = Convert.ToDouble(MyValue(1))
                        y = angle * (Math.PI / 180)
                        x2 = x1 + length
                        y2 = y1 + ((length) * (Math.Sin(y)))
                        tmr.Enabled = True
    
                        formGraphics.DrawLine(myPen, Convert.ToInt32(x1), Convert.ToInt32(y1), Convert.ToInt32(x2), Convert.ToInt32(y2))
                        TextBoxX.Text = Convert.ToString(y1)
                        TextBoxY.Text = Convert.ToString(y2)
                        TextBoxAngle.Text = MyValue(1)
                        x1 = x2
                        y1 = y2
                        If sr.EndOfStream Then
                            tmr.Enabled = False
                        End If
                        Try
                        Catch Ex As Exception
                            MessageBox.Show("Cannot read file from disk. Click okay to try another file.")
                        End Try
                    Loop Until sr.EndOfStream
                End Using
            End If
        End Sub

  2. #2
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2005] draw a line

    What values are in MyValue(0) and MyValue(1)?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    27

    Re: [2005] draw a line

    the numbers vary. the first numbers is the distance of a line. the second number is the angle at which the line is to be drawn.

    I have it working so that angles between 0 and 360 work. However anything above and below that does not work.

    i.e.

    10 -45

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

    Re: [2005] draw a line

    I haven't looked at your code but remember this. Traditionally the positive x axis is to the right and the positive y axis is upwards. On a WinForms control the positive y axis is downwards.
    Last edited by jmcilhinney; Dec 5th, 2007 at 12:10 AM.
    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

  5. #5
    Addicted Member
    Join Date
    Sep 2006
    Location
    Surabaya, Indonesia
    Posts
    163

    Re: [2005] draw a line

    jim, X axis always to the right, Y axis is downwards in winform.

    @champ0342: You should draw the line on Paint event, rather than click, because it won't draw again when you minimize or move the form.. And graphic objects consume memory a lot if you can't manage them..
    Check my Blog at VB Corner,Component Crafts

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    27

    Re: [2005] draw a line

    how would i tell the program to clear anything drawn on this form?

  7. #7
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: [2005] draw a line

    isn't there an overloads for the linedraw that takes an angle as a parameter?
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

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

    Re: [2005] draw a line

    Quote Originally Posted by michaelrawi
    jim, X axis always to the right, Y axis is downwards in winform.

    @champ0342: You should draw the line on Paint event, rather than click, because it won't draw again when you minimize or move the form.. And graphic objects consume memory a lot if you can't manage them..
    Oops! That was a typo. I did mean the +ve y axis was down and I've edited my post.

    Follow the Drawing link in my signature to see how to draw and "erase" lines on a control.
    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

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