# How to write a simple sine graph ?

Page 1 of 1

## 7 Replies - 7206 Views - Last Post: 22 March 2011 - 12:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=223374&amp;s=8c2a38c13de5d1f7e57c695cf4ef7bab&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hugronaphor

Reputation: 0
• 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_

• Master(Of Foo)

Reputation: 1153
• Posts: 4,426
• 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.

### #3 _HAWK_

• Master(Of Foo)

Reputation: 1153
• Posts: 4,426
• 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.

### #4 CharlieMay

• This space intentionally left blank

Reputation: 1720
• Posts: 5,636
• 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.

### #5 _HAWK_

• Master(Of Foo)

Reputation: 1153
• Posts: 4,426
• 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.

### #6 hugronaphor

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

## Re: How to write a simple sine graph ?

Posted 21 March 2011 - 09:53 AM

CharlieMay, 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

I add code and photo here:

### #7 BobRodes

• Lovable Curmudgeon

Reputation: 590
• Posts: 3,041
• 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

### #8 BobRodes

• Lovable Curmudgeon

Reputation: 590
• Posts: 3,041
• 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