-
Jan 22nd, 2011, 04:57 PM
#1
Thread Starter
Junior Member
[RESOLVED] draw parabola (vb 2010 express)
hi,
I'm writing a little program for school to calculate a few parabolic characteristics
but i wanna draw a parabola.. and i don't know how to do it. plz help me =)
-
Jan 23rd, 2011, 03:19 AM
#2
Re: draw parabola (vb 2010 express)
Divide the task into two parts:
1. Use the formula for your parabola to populate an array of points. Work out the x,y values for the points from the formula.
2. Draw the values in the array in the Paint event of a control, e.g. a Form or a PictureBox. Use Graphics.DrawLines or Graphics.DrawCurve, both of which can take an array of points as an argument.
Say if you need more help with either of these parts.
BB
-
Jan 23rd, 2011, 03:44 AM
#3
Fanatic Member
Re: draw parabola (vb 2010 express)
One of my earliest computing experiences was in the 10th grade, 1983. I was in computer lab, learning BASIC on a cutting edge TRS 80 (with a 5 1/4 " Floppy disc drive - Only the netwrok admin had a hard drive - an ENORMOUS 10 MB deal . . . ). Back then, I decided I would get one over on my math teacher. I was struggling with the quadratic formula, and I decided to write a program to do it for me, so that I wouldn't have to do all that fussy algebraic crap.
The joke was on me, but by the time I was done, I understood the qudaratic formula better than anyone in my class.
Boops boops is correct. You can think of the surface of a form as a grid. Kind of like graph paper. You can use the Paint event of the form to catch the PaintEvent Args (that's the "e" in the event signature), which will provide you with a Graphics object. You can then use that to draw on the form.
Since I am all about helping you learn how to do this, but believe you are better served by the struggle of the effort, Here is some farily simple code, which uses said paint event to draw a straight line.
Note how, in defining my points, I have modeled a basic linear function:
y = X + SomeValue.
Think this through, and please do let us know how you progress. Next step might be to play with the code below, and maybe figure out how one might pass a List(Of Point) to a method like that below, which would iterate through the point structures in the list, and plot lines between them.
Then one would have only to fill up that list(of Point) with points calculated using the formula for a parabola.
Again echoing boops boops, do also ask for additional help. If you are a beginner with vb, and/or are only learning the algebra, this will be both challenging and reqarding . . .
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
'Catch the PainteventArge, and pass to the function of your choice:
Call Me.DrawLinearFunction(e)
'This could just as easily be YOUR Parabolic Function.
End Sub
Private Sub DrawLinearFunction(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim Origin As Point
Origin.X = e.ClipRectangle.Left
Origin.Y = e.ClipRectangle.Top
Dim p1 As Point
p1.X = Origin.X + 10
p1.Y = Origin.Y + 10
Dim p2 As Point
p2.X = p1.X + 20
p2.Y = p1.Y + 20
Dim Pen As New System.Drawing.Pen(Color.Black, 1)
e.Graphics.DrawLine(Pen, p1, p2)
End Sub
-
Jan 23rd, 2011, 05:58 AM
#4
Thread Starter
Junior Member
Re: draw parabola (vb 2010 express)
i've got one little question left how can i change this function to something like "y = x²" ? (so, what function do i need to chose instead of "Me.DrawLinearFunction(e)"
-
Jan 23rd, 2011, 11:30 AM
#5
Fanatic Member
Re: draw parabola (vb 2010 express)
Well, that's the part where I said it will benefit you to figure it out. Note that I didn't CHOOSE a pre-existing method called "DrawLinearFunction(e)" - I wrote it.
THIS is a Method ("a SubRoutine" or "Sub" for short.). A Sub executes the code inside the outermost stubs, usually to perform some action. THIS particular peice of code executes in response the a message from the form to "Paint". Note the Handles statement after the Sub declaration?
Code:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
End Sub
When I typed "Me.DrawLinearFunction(e)" in there, I am telling the application to execute another Sub named just that:
Code:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Me.DrawLinearFunction(e)
End Sub
Then I (very hastily and with little forethought, in this case) wrote my own SUB named DrawLinearFuntion, which accepts a parameter, e, of type PainteventArgs. The code between the "Private . . ." and "End" Keywards is what I want the computer to do when some other peice of code calls this method (Ignore my use of the "call" keyword in the previous post - it is not needed in .net. I've been doing a large MS Access Project in VBA, where is is used alot. Ooops):
Code:
Private Sub DrawLinearFunction(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim Origin As Point
Origin.X = e.ClipRectangle.Left
Origin.Y = e.ClipRectangle.Top
Dim p1 As Point
p1.X = Origin.X + 10
p1.Y = Origin.Y + 10
Dim p2 As Point
p2.X = p1.X + 20
p2.Y = p1.Y + 20
Dim Pen As New System.Drawing.Pen(Color.Black, 1)
e.Graphics.DrawLine(Pen, p1, p2)
End Sub
If all of this is still problematic for you, I recommend you review the documentation for VS, and go over the basics. While what you are trying to do is not execptionally difficult, it will be quite challenging for someone who is only just learning things like methods, properties, functions, and Event Handlers.
Introduction to the Visual Basic Programming Languageis a link, available in the "Start Page" of VS, which provides some introduction to basic programming concepts.
Hope that helps. If you have put together any code of your own for this project yet, please post it, and we can be of more assistance . . .
-
Jan 26th, 2011, 12:48 PM
#6
Thread Starter
Junior Member
Re: draw parabola (vb 2010 express)
Public Class Form1
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkboxalfa.CheckedChanged
If checkboxalfa.Checked Then txtboxalfa.Visible = True Else txtboxalfa.Visible = False
End Sub
Private Sub checkboxbeta_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkboxbeta.CheckedChanged
If checkboxbeta.Checked Then txtboxbeta.Visible = True Else txtboxbeta.Visible = False
End Sub
Private Sub checkboxa_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkboxa.CheckedChanged
If checkboxa.Checked = True Then txtboxa.Visible = True Else txtboxa.Visible = False
End Sub
Private Sub checkboxb_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkboxb.CheckedChanged
If checkboxb.Checked Then txtboxb.Visible = True Else txtboxb.Visible = False
End Sub
Private Sub checkboxc_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkboxc.CheckedChanged
If checkboxc.Checked Then txtboxc.Visible = True Else txtboxc.Visible = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a, b, c, d, alfa, beta, snijpunt, xwaarde As Decimal
a = (txtboxa.Text)
b = (txtboxb.Text)
c = (txtboxc.Text)
d = (b ^ 2) - (4 * CInt(a) * CInt(c))
alfa = CInt(txtboxalfa.Text)
beta = CInt(txtboxbeta.Text)
xwaarde = TextBox1.Text
lblD.Text = d
If (checkboxa.Checked And checkboxb.Checked And checkboxc.Checked) Then
x.Text = CStr(Math.Round((-CStr(b) / (2 * CStr(a))), 2))
y.Text = CStr(Math.Round((-CStr(d) / (4 * CStr(a))), 2))
xvgl.Text = ("x = " + CStr((Math.Round((-CStr(b) / (2 * CStr(a))), 2))))
vgl.Text = (txtboxa.Text + ("* x² + ") + txtboxb.Text + ("* x + ") + txtboxc.Text)
Label6.Text = ("(0 ; " + CStr(c) + ")")
x1.Text = CStr(Math.Round((-b - Math.Sqrt(d)) / (2 * a), 3))
x2.Text = CStr(Math.Round((-b + Math.Sqrt(d)) / (2 * a), 3))
lbl2a.Text = CStr(2 * a)
labelD.Text = CStr(d)
lblb.Text = CStr(b)
Panel1.Show()
End If
If (checkboxalfa.Checked And checkboxbeta.Checked) Then
vgl.Text = (CStr(a) + ("(x - ") + CStr(alfa) + (")² +") + CStr(beta))
xvgl.Text = ("x = " + CStr(alfa))
x.Text = txtboxalfa.Text
y.Text = txtboxbeta.Text
snijpunt = a * alfa ^ 2 + beta
Label6.Text = CStr(a) * CStr(alfa ^ 2) + CStr(beta)
x1.Text = (Math.Sqrt((-(txtboxbeta.Text) / (txtboxa.Text))) + (txtboxalfa.Text))
x2.Text = (-Math.Sqrt((-(txtboxbeta.Text) / (txtboxa.Text))) + (txtboxalfa.Text))
Panel1.Hide()
End If
'discriminant
If checkboxalfa.Checked And checkboxbeta.Checked Then
lblD.Hide()
Label4.Hide()
Else
lblD.Show()
Label4.Show()
End If
If (checkboxa.Checked And checkboxb.Checked And checkboxc.Checked And CheckBox1.Checked) Then
Label15.Text = CStr(Math.Round((a * xwaarde ^ 2 + b * xwaarde + c), 2))
End If
If (checkboxalfa.Checked And checkboxbeta.Checked And CheckBox1.Checked) Then
Label15.Text = a * (xwaarde - alfa) ^ 2 + beta
End If
If CheckBox1.Checked = True Then
Panel2.Visible = True
Else : Panel2.Visible = False
End If
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
Form2.Show()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
form3.show()
End Sub
End Class
http://imagetwist.com/je20q8mi1p2s/printscreen.png.html
http://imagetwist.com/9qz1kjuolsr5/p...reen2.png.html
(the form..) I translated the importantst stuff,.. (i'm dutch)
it's just a small program for math (to make my homework) but i wanna add a parabola so i can see it (when i'm needed to draw one in my workbook)
(xwaarde = xvalue, d = the discriminant (b²-4ac), if you want something more that i can explain, just ask me
(i'm really stuck at the part to make the parabola, can some1 just write a little piece of code that i can copy (just a parabola like "y = x²"),.. really appreciate it
-
Jan 27th, 2011, 01:46 PM
#7
Re: draw parabola (vb 2010 express)
Hi tinkii,
Here's an example of how you could draw a parabola for y = x2 in a picture box (PictureBox1). I've assumed you want plot the values at integer intervals from x=-10 to x =10. That should be enough to give a smooth curve.
Code:
Option Strict Off
Public Class Form1
Dim graphMargin As Integer = 5 'some space around the graph
Dim dataPoints(20) As Point 'an array of points (size = 21 since counting starts from 0)
'Build the array:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Get a rectangle for drawing in the picture box:
Dim rect As Rectangle = PictureBox1.ClientRectangle
rect.Inflate(-graphMargin, -graphMargin) 'allow a margin
'fill the array with data:
For i As Integer = -10 To 10
Dim x, y As Integer
'here's where you apply the formula
x = i
y = i ^ 2
'scale and centre the point coordinates to fit the picture box
Dim p As Point
p.X = x * rect.Width / 20 + PictureBox1.Width / 2
p.Y = rect.Bottom - y * rect.Height / 100
'add the data point to the array
dataPoints(i + 10) = p
Next
End Sub
'Draw the parabola on the picture box:
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality 'optional: draws a nicer line.
e.Graphics.DrawCurve(Pens.Blue, dataPoints) 'Draw the parabola -- just one line!
End Sub
End Class
Please look at the comments so you understand how it works and you can apply it to other formulae. The most difficult part is probably figuring out how to fit the data nicely in the picture box. That's something you have to think about with each formula. Try it out on paper, or do it by trial and error.
BB
Last edited by boops boops; Jan 27th, 2011 at 02:21 PM.
-
Jan 27th, 2011, 01:51 PM
#8
Thread Starter
Junior Member
Re: draw parabola (vb 2010 express)
thanks bb =) looking good ^^
-
Jan 28th, 2011, 04:34 AM
#9
Thread Starter
Junior Member
Re: draw parabola (vb 2010 express)
printscreen of the program running. thanks BB for helping ^^
-
Jan 28th, 2011, 09:02 AM
#10
Fanatic Member
Re: [RESOLVED] draw parabola (vb 2010 express)
@tinkii - Nicely Done!!! Rep to you for your effort!
-
Jan 28th, 2011, 09:08 AM
#11
Fanatic Member
Re: [RESOLVED] draw parabola (vb 2010 express)
I would be interested in seeing your final code . . .
-
Jan 28th, 2011, 12:00 PM
#12
Thread Starter
Junior Member
Re: [RESOLVED] draw parabola (vb 2010 express)
-
Nov 11th, 2012, 09:56 AM
#13
New Member
Re: [RESOLVED] draw parabola (vb 2010 express)
Originally Posted by tinkii
Hello
Unfortunately the file: wiskunde3.rar File size: 655.70 KB is no longer available!
Please could put it again?
thank you very much
Tags for this Thread
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
|