''' <summary>
''' 関数(y=f(x))のデリゲート
''' </summary>
''' <param name="x">入力</param>
''' <returns>出力</returns>
''' <remarks></remarks>
Public Delegate Function FuncHandler(ByVal x As Integer) As Integer
''' <summary>
''' 関数(y=f(x))のグラフをイメージに描画する。
''' </summary>
''' <param name="targetImage">描画対象のイメージ</param>
''' <param name="fnc">関数(y=f(x))</param>
''' <param name="xFrom">X座標の最小値</param>
''' <param name="xTo">X座標の最大値</param>
''' <remarks></remarks>
Public Shared Sub DrawFunction( _
ByVal targetImage As Image, ByVal fnc As FuncHandler, _
ByVal xFrom As Integer, ByVal xTo As Integer)
'描画領域の原点を中央に移動
Dim xMax As Integer = targetImage.Width * 0.5
Dim xMin As Integer = -1 * xMax
Dim yMax As Integer = targetImage.Height * 0.5
Dim yMin As Integer = -1 * yMax
g.TranslateTransform(xMax, yMax)
'描画領域のY座標の向きを逆にする(Y座標の上方向をプラスにする)
g.ScaleTransform(1, -1)
'格子を描画する(水平線)
For y As Single = 0 To yMax Step 10
g.DrawLine(Pens.Cyan, xMin, y, xMax, y)
Next
For y As Single = 0 To yMin Step -10
g.DrawLine(Pens.Cyan, xMin, y, xMax, y)
Next
'格子を描画する(垂直線)
For x As Single = 0 To xMax Step 10
g.DrawLine(Pens.Cyan, x, yMin, x, yMax)
Next
For x As Single = 0 To xMin Step -10
g.DrawLine(Pens.Cyan, x, yMin, x, yMax)
Next
'関数のグラフを描画
For x As Integer = xFrom To xTo
Try
g.DrawLine( _
Pens.Black, x - 1, _
fnc.Invoke(x - 1), x, fnc.Invoke(x))
Catch ex As ArithmeticException
'ゼロ除算、数値演算のオーバーフロー、
'定義されていない演算エラーは無視する
End Try
Next
Finally
If Not (g Is Nothing) Then
g.Dispose()
End If
End Try
End Sub
''' <summary>
''' 関数(一次関数)
''' </summary>
''' <param name="x">入力</param>
''' <returns>出力</returns>
''' <remarks></remarks>
Public Function fnc1(ByVal x As Integer) As Integer
Return x * 2
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim img As New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
'関数の参照をデリゲート変数に格納する
'※関数を変更する場合は、デリゲート変数の中身を変更する。
Dim f As New FuncHandler(AddressOf Me.fnc1)
'関数のグラフを描画する。
DrawFunction(img, f, img.Width * -0.5, img.Width * 0.5)
''' <summary>
''' 関数(二次関数)
''' </summary>
''' <param name="x">入力</param>
''' <returns>出力</returns>
''' <remarks></remarks>
Public Function fnc1(ByVal x As Integer) As Integer
Return 0.05 * (x - 10) * (x + 50)
End Function
''' <summary>
''' 関数(三角関数)
''' </summary>
''' <param name="x">入力</param>
''' <returns>出力</returns>
''' <remarks></remarks>
Public Function fnc1(ByVal x As Integer) As Integer
Return 50 * Math.Sin(x / 15)
End Function
[VB.NET]関数を三角関数で定義した場合
実行結果(4)PictureBoxに、平方根の関数のグラフを描画する例
・平方根の関数も同様です。
''' <summary>
''' 関数(平方根)
''' </summary>
''' <param name="x">入力</param>
''' <returns>出力</returns>
''' <remarks></remarks>
Public Function fnc1(ByVal x As Integer) As Integer
Return 10 * Math.Sqrt(x)
End Function
''' <summary>
''' 指定したImageを分割する。
''' </summary>
''' <param name="originalImage">オリジナルImage</param>
''' <param name="verticalNum">分割数(垂直)</param>
''' <param name="horizontalNum">分割数(水平)</param>
''' <returns>分割済Image</returns>
''' <remarks></remarks>
Public Shared Function DivideImage( _
ByVal originalImage As Image, _
ByVal verticalNum As Integer, ByVal horizontalNum As Integer _
) As Image()()
'引数チェック
If originalImage Is Nothing Then
Throw New ArgumentNullException("originalImage is null")
End If
If verticalNum < 1 Then
Throw New ArgumentOutOfRangeException("verticalNum < 1")
End If
If horizontalNum < 1 Then
Throw New ArgumentOutOfRangeException("horizontalNum < 1")
End If
Dim g As Graphics = Nothing
Dim rtnImage()() As Image = New Image(verticalNum - 1)() {}
For i As Integer = 0 To verticalNum - 1
rtnImage(i) = New Image(horizontalNum - 1) {}
Next
Dim originalSize As Size = originalImage.Size
Dim divWidth As Integer = originalSize.Width / horizontalNum
Dim divHeight As Integer = originalSize.Height / verticalNum
For vCnt As Integer = 0 To verticalNum - 1
For hCnt As Integer = 0 To horizontalNum - 1
Try
'空のImageを作成
rtnImage(vCnt)(hCnt) = New Bitmap(divWidth, divHeight)
'Imageを描画する為のGraphicsオブジェクトを作成
g = System.Drawing.Graphics.FromImage( _
rtnImage(vCnt)(hCnt))
'オリジナルImageから描画対象の領域を算出
'[補足]
' 領域の開始座標を
' ・hCnt * divWidth
' ・vCnt * divHeight
' ではなく、
' ・hCnt * originalSize.Width / horizontalNum
' ・vCnt * originalSize.Height / verticalNum
' にしているのは、誤差を軽減する為です。
Dim rect As New Rectangle( _
hCnt * originalSize.Width / horizontalNum, _
vCnt * originalSize.Height / verticalNum, _
divWidth, _
divHeight _
)
'オリジナルImageの内容を描画
g.DrawImage( _
originalImage, 0, 0, rect, GraphicsUnit.Pixel)
Finally
If Not (g Is Nothing) Then
'Graphicsオブジェクトを解放
g.Dispose()
End If
End Try
Next
Next
Return rtnImage
End Function
[VB.NET]Imageを分割する例
/// <summary>
/// 指定したImageを分割する。
/// </summary>
/// <param name="originalImage">オリジナルImage</param>
/// <param name="verticalNum">分割数(垂直)</param>
/// <param name="horizontalNum">分割数(水平)</param>
/// <returns>分割済Image</returns>
/// <remarks></remarks>
public static Image[][] DivideImage(
Image originalImage, int verticalNum, int horizontalNum)
{
//引数チェック
if (originalImage == null) {
throw new ArgumentNullException("originalImage is null");
}
if (verticalNum < 1) {
throw new ArgumentOutOfRangeException("verticalNum < 1");
}
if (horizontalNum < 1) {
throw new ArgumentOutOfRangeException("horizontalNum < 1");
}
Graphics g = null;
Image[][] rtnImage = new Image[verticalNum][];
for (int i = 0; i <= verticalNum - 1; i++) {
rtnImage(i) = new Image[horizontalNum];
}
Size originalSize = originalImage.Size;
int divWidth = originalSize.Width / horizontalNum;
int divHeight = originalSize.Height / verticalNum;