|
-
Dec 4th, 2007, 11:10 PM
#1
Thread Starter
Junior Member
[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
-
Dec 4th, 2007, 11:17 PM
#2
Frenzied Member
Re: [2005] draw a line
What values are in MyValue(0) and MyValue(1)?
-
Dec 4th, 2007, 11:30 PM
#3
Thread Starter
Junior Member
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
-
Dec 4th, 2007, 11:43 PM
#4
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.
-
Dec 4th, 2007, 11:58 PM
#5
Addicted Member
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..
-
Dec 5th, 2007, 12:06 AM
#6
Thread Starter
Junior Member
Re: [2005] draw a line
how would i tell the program to clear anything drawn on this form?
-
Dec 5th, 2007, 12:08 AM
#7
Re: [2005] draw a line
isn't there an overloads for the linedraw that takes an angle as a parameter?
-
Dec 5th, 2007, 12:10 AM
#8
Re: [2005] draw a line
 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.
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
|