一昨日、「フラクタル図形の世界へのいざない」(http://qiita.com/kiuyas/items/dee169b7fea69519a18d )というお題でJava版の「コッホ曲線」と「シェルピンスキーのギャスケット」を描くコードを載せ、その後「フラクタル図形の世界へのいざない(for C#)」と題してそのC#版を載せましたが、続きましてそのVB.NET版をお届けします。
KochCurveという名前でWindowsフォームアプリケーションを作成し、Form1.vbを以下のものに書き換えてください。
Form1.vb
Public Class Form1
Private Const PAI As Double = 3.1415926535893934
Private Const thetaOf60Degree As Double = 60 * PAI / 180
Private maxLevel As Integer = 4
Public Sub New()
InitializeComponent()
SetBounds(0, 0, 640, 480, BoundsSpecified.Size)
BackColor = Color.Black
Me.Text = "コッホ曲線"
End Sub
Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
DrawKochCurve(e.Graphics, 0, 100, 639, 100, 0)
End Sub
Private Sub DrawKochCurve(g As Graphics, x1 As Double, y1 As Double, x2 As Double, y2 As Double, level As Integer)
If level = maxLevel Then
g.DrawLine(Pens.Yellow, CInt(x1), CInt(479 - y1), CInt(x2), CInt(479 - y2))
Else
Dim vx As Double = (x2 - x1) / 3.0
Dim vy As Double = (y2 - y1) / 3.0
Dim xx1 As Double = x1 + vx
Dim yy1 As Double = y1 + vy
Dim v1 As Double() = Rotate(thetaOf60Degree, vx, vy)
Dim xx2 As Double = xx1 + v1(0)
Dim yy2 As Double = yy1 + v1(1)
Dim v2 As Double() = Rotate(-thetaOf60Degree, vx, vy)
Dim xx3 As Double = xx2 + v2(0)
Dim yy3 As Double = yy2 + v2(1)
level += 1
DrawKochCurve(g, x1, y1, xx1, yy1, level)
DrawKochCurve(g, xx1, yy1, xx2, yy2, level)
DrawKochCurve(g, xx2, yy2, xx3, yy3, level)
DrawKochCurve(g, xx3, yy3, x2, y2, level)
End If
End Sub
Private Function Rotate(theta As Double, x As Double, y As Double) As Double()
Dim sinTheta As Double = Math.Sin(theta)
Dim cosTheta As Double = Math.Cos(theta)
Dim x2 As Double = cosTheta * x - sinTheta * y
Dim y2 As Double = sinTheta * x + cosTheta * y
Return New Double() {x2, y2}
End Function
End Class
SierpinskiGasketという名前でWindowsフォームアプリケーションを作成し、Form1.vbを以下のものに書き換えてください。
Form1.vb
Public Class Form1
Private maxLevel As Integer = 6
Public Sub New()
InitializeComponent()
SetBounds(0, 0, 640, 480, BoundsSpecified.Size)
BackColor = Color.Black
Me.Text = "シェルピンスキーのギャスケット"
End Sub
Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
DrawSierpinskiGasket(e.Graphics, 319, 40, 30, 430, 609, 430, 0)
End Sub
Private Sub DrawSierpinskiGasket(g As Graphics, x1 As Double, y1 As Double, x2 As Double, y2 As Double, x3 As Double, y3 As Double, level As Integer)
If level = maxLevel Then
g.DrawLine(Pens.Lime, CInt(x1), CInt(y1), CInt(x2), CInt(y2))
g.DrawLine(Pens.Lime, CInt(x2), CInt(y2), CInt(x3), CInt(y3))
g.DrawLine(Pens.Lime, CInt(x3), CInt(y3), CInt(x1), CInt(y1))
Else
Dim xx1 As Double = (x1 + x2) / 2.0
Dim yy1 As Double = (y1 + y2) / 2.0
Dim xx2 As Double = (x2 + x3) / 2.0
Dim yy2 As Double = (y2 + y3) / 2.0
Dim xx3 As Double = (x3 + x1) / 2.0
Dim yy3 As Double = (y3 + y1) / 2.0
DrawSierpinskiGasket(g, x1, y1, xx1, yy1, xx3, yy3, level + 1)
DrawSierpinskiGasket(g, x2, y2, xx1, yy1, xx2, yy2, level + 1)
DrawSierpinskiGasket(g, x3, y3, xx3, yy3, xx2, yy2, level + 1)
End If
End Sub
End Class
気が向いたら、HTML5版とか作るかもしれません。
では。