cos波表示について マクローリン展開 vb
下記が、sin波を表示させるためのプログラムです。
これを改良し(mysin→mycosにし、mysin2→mycos2にする。)cos波を表示させたいです。
今、作ってみましたが、変な形です。
どこをどのように改良すればよいか、教えていただけませんでしょうか。
ちなみに、cos波は、picturebox2に作り、button2をクリックしたときに表示させるようにする。
Imports System.Math
Public Class Form1
Dim b As Bitmap
Dim g As Graphics
Dim p As Pen
Dim w, h, mw, mmw, mh As Integer
Dim b2 As Bitmap
Dim g2 As Graphics
Dim p2 As Pen
Dim w2, h2, mw2, mmw2, mh2 As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "Sin波 Cos波"
w = PictureBox1.Width
h = PictureBox1.Height
w2 = PictureBox2.Width
h2 = PictureBox2.Height
mw = w \ 2
mmw = mw \ 2
mh = h \ 2
mw2 = w2 \ 2
mmw2 = mw2 \ 2
mh2 = h2 \ 2
Button1.Text = "Sin Curve"
Button2.Text = "Cos Curve"
b = New Bitmap(w, h)
g = Graphics.FromImage(b)
p = New Pen(Color.Black, 1)
b2 = New Bitmap(w2, h2)
g2 = Graphics.FromImage(b2)
p2 = New Pen(Color.Black, 1)
g.DrawLine(p, 0, mh, w, mh)
g.DrawLine(p, mw \ 2, 0, mw \ 2, h)
g2.DrawLine(p2, 0, mh2, w2, mh2)
g2.DrawLine(p2, mw2 \ 2, 0, mw2 \ 2, h2)
p.Dispose()
p2.Dispose()
PictureBox1.Image = b
PictureBox2.Image = b2
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, k, x0, y0 As Integer
p = New Pen(Color.Blue, 2)
For i = mw + mmw - w To w - mmw
k = mysin(i * PI / 180) * mh * 0.9
x0 = mmw + i
y0 = mh - k
g.DrawLine(p, x0, y0, x0 + 1, y0 + 1)
Next
PictureBox1.Image = b
End Sub
Function mysin(ByVal arg) As Double
Dim s = Sign(arg)
Dim mp = Int(arg / (2 * PI))
arg = arg - 2 * PI * mp
If s < 0 Then arg = 2 * PI - arg
If arg > 3 * PI / 2 Then
Return -s * mysin2(2 * PI - arg)
ElseIf arg > PI Then
Return -s * mysin2(arg - PI)
ElseIf arg > PI / 2 Then
Return s * mysin2(PI - arg)
Else
Return s * mysin2(arg)
End If
End Function
Function mysin2(ByVal arg) As Double
Return arg - Pow(arg, 3) / 6 + Pow(arg, 5) / 120 - Pow(arg, 7) / 5040
End Function
Function mycos(ByVal arg2) As Double
Dim c = Sign(arg2)
Dim mp = Int(arg2 / (2 * PI))
arg2 = arg2 - 2 * PI * mp
If c < 0 Then arg2 = -2 * PI - arg2
If arg2 > 3 * PI / 2 Then
Return c * mycos2(-2 * PI - arg2)
ElseIf arg2 > PI Then
Return c * mycos2(arg2 - PI)
ElseIf arg2 > PI / 2 Then
Return -c * mycos2(PI - arg2)
Else
Return -c * mycos2(arg2)
End If
End Function
Function mycos2(ByVal arg2) As Double
Return 1 - Pow(arg2, 2) / 4 + Pow(arg2, 4) / 24 - Pow(arg2, 6) / 720
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim j, l, x1, y1 As Integer
p2 = New Pen(Color.Red, 2)
For j = mw2 - mmw2 - w2 To w2 - mmw2
l = mycos(j * PI / 180) * mh2
x1 = mmw2 + j
y1 = mh2 - l
g2.DrawLine(p2, x1, y1, x1 + 1, y1 + 1)
Next
PictureBox2.Image = b2
End Sub
End Class
お礼
そうですね。ご指摘に感謝。