7 Replies - 5692 Views - Last Post: 22 March 2011 - 12:33 PM Rate Topic: -----

#1 hugronaphor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-March 11

How to write a simple sine graph ?

Posted 21 March 2011 - 06:55 AM

How to write a simple sin graph for my calculator?
I need , when I'll press the button, to show this graphic.


 Private Sub btnsin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsin.Click
        For i = 0 To 100
            PictureBox.DrawLine(Pens.Black, i, 100 + CInt(Math.Sin(i)), i + 1, 100 + CInt(Math.Sin(i + 1)))
        Next i


    End Sub




error is on PictureBox.DrawLine -> "is not a member 'System.Windows.Form.PictureBox'" so what word I have to use?
thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: How to write a simple sine graph ?

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,178
  • Joined: 02-July 08

Re: How to write a simple sine graph ?

Posted 21 March 2011 - 08:55 AM

Painting is typically done in a paint event and using the e.Graphic object, where you will fine the drawline method.

If you do not paint in the paint event your drawings will not persist thru the next refresh.
Was This Post Helpful? 1
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,178
  • Joined: 02-July 08

Re: How to write a simple sine graph ?

Posted 21 March 2011 - 09:08 AM

Also a sine wave is drawn with a curve object.
Was This Post Helpful? 1
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: How to write a simple sine graph ?

Posted 21 March 2011 - 09:19 AM

You could use something like this:
        Dim myGraphics As Graphics = PictureBox1.CreateGraphics
        For i As Integer = 0 To 1000
            myGraphics.DrawLine(Pens.Black, i, 100 + CInt(Math.Sin(i)), i + 1, 100 + CInt(Math.Sin(i + 1)))
        Next i

Which would draw the sine when the button is clicked

BUT... As pointed out. ANY refresh will lose the image.
Was This Post Helpful? 1
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,178
  • Joined: 02-July 08

Re: How to write a simple sine graph ?

Posted 21 March 2011 - 09:38 AM

Always dispose any graphics object that you create(so e.Graphics is excluded in this). This includes Pens, Brushes, fonts, graphicspaths, etc...

The best way to dispose them is the Using/End Using block.
Was This Post Helpful? 0
  • +
  • -

#6 hugronaphor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-March 11

Re: How to write a simple sine graph ?

Posted 21 March 2011 - 09:53 AM

View PostCharlieMay, on 21 March 2011 - 09:19 AM, said:



Woow, finaly it's working (little bit).Thanks.

If you want to help me to the end, so:
I need that this sine grpaphic will show me a certain value. I mean whwn I write 90 or 60 in digital calc. to show me this graph. some ideas? I know that you know :D

I add code and photo here:

Attached Image

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: How to write a simple sine graph ?

Posted 21 March 2011 - 01:09 PM

For those of us who don't like to (or can't, for security reasons) open attachments, here's the relevant code:
Private Sub btnsin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsin.Click
     Dim myGraphics As Graphics = PictureBox1.CreateGraphics
     For i As Integer = 0 To 1000
          myGraphics.DrawLine(Pens.Black, i, 100 + CInt(Math.Sin(i)), i + 1, 100 + CInt(Math.Sin(i + 1)))
     Next i
End Sub


A Sine Wave has Frequency and Amplitude. For each x coordinate on the graph, y in terms of x is Sin(x * Frequency) * Amplitude. So perhaps make your amplitude the height of your picture box, and allow frequency to be a variable input.

In effect, yours always has a frequency of 1 and an amplitude of 1, and that cute little wiggly line seems to be an accurate representation of that.

This post has been edited by BobRodes: 21 March 2011 - 01:10 PM

Was This Post Helpful? 2
  • +
  • -

#8 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: How to write a simple sine graph ?

Posted 22 March 2011 - 12:33 PM

More sine wave fun, for extra credit. A sine wave is a representation of a simple oscillation of an object between two points. As such, it is the simplest of all sounds that can be made, fairly close to the sound made when running a finger around a wine glass. Perhaps when running your program you could also create a sine wave sound signal with the frequency and amplitude represented on your graph.

For some background on the subject: http://www.tek-tips....cfm?qid=1165975
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1