Public Class Form1 Public Const Pi = 3.14159265358979 Function 色設定(種類 As Integer, CH As Integer) As Color If 種類 = 1 Then 色設定 = Color.FromArgb(0, CH * 64, 0) Else 色設定 = Color.FromArgb(CH * 64, CH * 64, 0) End If End Function Function IX(X As Double) As Integer IX = 0.5 * X + 200 + 0.5 End Function Function IY(Y As Double) As Integer IY = 300 - Y * 0.5 + 0.5 End Function Private Sub DrawStalk(種類 As Integer, X1 As Double, Y1 As Double, X2 As Double, _ Y2 As Double, X3 As Double, Y3 As Double, _ X4 As Double, Y4 As Double) '茎描画 Dim DX As Double, DY As Double, DX1 As Double, DY1 As Double Dim DX2 As Double, DY2 As Double, X As Double, Y As Double Dim XX2 As Double, YY2 As Double, i As Integer, CH As Integer Dim g As Graphics = PictureBox1.CreateGraphics() Dim P As Pen DX = X2 - X1 : DY = Y2 - Y1 If Math.Abs(DX) > Math.Abs(DY) Then DX1 = DX / 5 : DY1 = DY / 5 DX2 = (X4 - X3) / 5 : DY2 = (Y4 - Y3) / 5 Y = Y1 : XX2 = X3 : YY2 = Y3 i = 0 For X = X1 To X2 Step DX1 CH = i Mod 3 : P = New Pen(色設定(種類, CH)) g.DrawLine(P, IX(X), IY(Y), IX(XX2), IY(YY2)) Y = Y + DY1 : XX2 = XX2 + DX2 : YY2 = YY2 + DY2 i = i + 1 Next Else DX1 = DX / 5 : DY1 = DY / 5 DX2 = (X4 - X3) / 5 : DY2 = (Y4 - Y3) / 5 X = X1 : XX2 = X3 : YY2 = Y3 : i = 0 For Y = Y1 To Y2 Step DY1 CH = i Mod 3 : P = New Pen(色設定(種類, CH)) g.DrawLine(P, IX(X), IY(Y), IX(XX2), IY(YY2)) X = X + DX1 : XX2 = XX2 + DX2 : YY2 = YY2 + DY2 i = i + 1 Next End If End Sub Private Sub DrawStalk1(X1 As Double, Y1 As Double, X2 As Double, _ Y2 As Double, X3 As Double, Y3 As Double, _ X4 As Double, Y4 As Double) ' 緑っぽい茎の描画 DrawStalk(1, X1, Y1, X2, Y2, X3, Y3, X4, Y4) End Sub Private Sub DrawStalk2(X1 As Double, Y1 As Double, X2 As Double, _ Y2 As Double, X3 As Double, Y3 As Double, _ X4 As Double, Y4 As Double) '茶色っぽい茎描画 DrawStalk(2, X1, Y1, X2, Y2, X3, Y3, X4, Y4) End Sub Private Sub DrawFern(X As Double, Y As Double, W As Double, _ L As Double, TH As Double, DTH As Double) Dim X0 As Double, Y0 As Double, XN As Double, YN As Double Dim X1 As Double, Y1 As Double, X2 As Double, Y2 As Double Dim X3 As Double, Y3 As Double, X4 As Double, Y4 As Double Dim XX1 As Double, YY1 As Double, XX2 As Double, YY2 As Double Dim cosTH As Double, sinTH As Double Dim TH0 As Double, WW As Double, LL As Double Dim g As Graphics = PictureBox1.CreateGraphics() Dim P As Pen = New Pen(Color.Black) X0 = X : Y0 = Y : TH0 = TH : WW = W : LL = L cosTH = Math.Cos(TH0) : sinTH = Math.Sin(TH0) X1 = X0 - WW * sinTH : Y1 = Y0 + WW * cosTH X2 = X0 + WW * sinTH : Y2 = Y0 - WW * cosTH Do While WW > 0.05 WW = WW * 0.95 XN = X0 + LL * cosTH : YN = Y0 + LL * sinTH X3 = XN - WW * sinTH : Y3 = YN + WW * cosTH X4 = XN + WW * sinTH : Y4 = YN - WW * cosTH DrawStalk1(X1, Y1, X2, Y2, X3, Y3, X4, Y4) XX1 = X1 - 3 * WW * sinTH : YY1 = Y1 + 3 * WW * cosTH XX2 = X2 + 3 * WW * sinTH : YY2 = Y2 - 3 * WW * cosTH g.DrawLine(P, IX(X1), IY(Y1), IX(XX1), IY(YY1)) g.DrawLine(P, IX(X2), IY(Y2), IX(XX2), IY(YY2)) DrawFern(X1 - 3 * WW * sinTH, Y1 + 3 * WW * cosTH, _ WW * 0.3, LL * 0.3, TH + Pi / 3, Math.Abs(DTH) * 0.8) DrawFern(X2 + 3 * WW * sinTH, Y2 - 3 * WW * cosTH, _ WW * 0.3, LL * 0.3, TH - Pi / 3, -Math.Abs(DTH) * 0.8) X0 = XN : Y0 = YN : X1 = X3 : Y1 = Y3 : X2 = X4 : Y2 = Y4 TH0 = TH0 - DTH cosTH = Math.Cos(TH0) : sinTH = Math.Sin(TH0) LL = LL * 0.9 Loop End Sub '樹木の描画 Private Sub DrawTree(X As Double, Y As Double, W As Double, _ L As Double, TH As Double, DTH As Double) Dim X0 As Double, Y0 As Double, XN As Double, YN As Double Dim X1 As Double, Y1 As Double, X2 As Double, Y2 As Double Dim X3 As Double, Y3 As Double, X4 As Double, Y4 As Double Dim DX As Double, DY As Double, i As Integer Dim cosTH As Double, sinTH As Double, TH0 As Double, WW As Double, LL As Double Dim g As Graphics = PictureBox1.CreateGraphics() Dim P As Brush = New SolidBrush(Color.DarkGreen) X0 = X : Y0 = Y : TH0 = TH : WW = W : LL = L cosTH = Math.Cos(TH0) : sinTH = Math.Sin(TH0) X1 = X0 - WW * sinTH : Y1 = Y0 + WW * cosTH X2 = X0 + WW * sinTH : Y2 = Y0 - WW * cosTH If L < 10 Then For i = 1 To 10 DX = (Rnd() - 0.5) * 100 : DY = (Rnd() - 0.5) * 100 g.FillEllipse(P, IX(X + DX), IY(Y + DY), 2, 2) Next Exit Sub End If i = 0 Do While WW > 0.1 WW = WW * 0.95 XN = X0 + LL * cosTH : YN = Y0 + LL * sinTH X3 = XN - WW * sinTH : Y3 = YN + WW * cosTH X4 = XN + WW * sinTH : Y4 = YN - WW * cosTH DrawStalk2(X1, Y1, X2, Y2, X3, Y3, X4, Y4) i = i + 1 If i = 5 Then DrawTree(X1, Y1, WW * 0.8, LL * 0.8, TH + Pi / 6, Math.Abs(DTH) * 0.8) DrawTree(X2, Y2, WW * 0.8, LL * 0.8, TH - Pi / 6, -Math.Abs(DTH) * 0.8) ElseIf i > 5 Then If i Mod 5 = 0 Then DrawTree(X1, Y1, WW * 0.8, LL * 0.8, TH + Pi / 6, Math.Abs(DTH) * 0.8) ElseIf i Mod 5 = 2 Then DrawTree(X2, Y2, WW * 0.8, LL * 0.8, TH - Pi / 6, -Math.Abs(DTH) * 0.8) End If End If X0 = XN : Y0 = YN : X1 = X3 : Y1 = Y3 : X2 = X4 : Y2 = Y4 TH0 = TH0 - DTH cosTH = Math.Cos(TH0) : sinTH = Math.Sin(TH0) LL = LL * 0.9 Loop End Sub ' スギナ描画 Private Sub DrawSugina(X As Double, Y As Double, W As Double, _ L As Double, TH As Double, DTH As Double) Dim X0 As Double, Y0 As Double, XN As Double, YN As Double Dim X1 As Double, Y1 As Double, X2 As Double, Y2 As Double Dim X3 As Double, Y3 As Double, X4 As Double, Y4 As Double Dim i As Integer Dim cosTH As Double, sinTH As Double, TH0 As Double, WW As Double, LL As Double Dim g As Graphics = PictureBox1.CreateGraphics() Dim P As Brush = New SolidBrush(Color.DarkGreen) X0 = X : Y0 = Y : TH0 = TH : WW = W : LL = L cosTH = Math.Cos(TH0) : sinTH = Math.Sin(TH0) X1 = X0 - WW * sinTH : Y1 = Y0 + WW * cosTH X2 = X0 + WW * sinTH : Y2 = Y0 - WW * cosTH 'DLW = LL * 4 i = 0 Do While WW > 0.1 WW = WW * 0.95 XN = X0 + LL * cosTH : YN = Y0 + LL * sinTH X3 = XN - WW * sinTH : Y3 = YN + WW * cosTH X4 = XN + WW * sinTH : Y4 = YN - WW * cosTH i = (i + 1) Mod 2 If i = 0 Then DrawSugina(X1, Y1, WW * 0.3, LL * 0.3, TH + Pi / 3, Math.Abs(DTH) * 0.8) DrawSugina(X2, Y2, WW * 0.3, LL * 0.3, TH - Pi / 3, -Math.Abs(DTH) * 0.8) End If DrawStalk1(X1, Y1, X2, Y2, X3, Y3, X4, Y4) X0 = XN : Y0 = YN : X1 = X3 : Y1 = Y3 : X2 = X4 : Y2 = Y4 TH0 = TH0 - DTH : cosTH = Math.Cos(TH0) : sinTH = Math.Sin(TH0) LL = LL * 0.9 Loop End Sub ' キリンソウ描画 Private Sub DrawKirinsou(X As Double, Y As Double, W As Double, _ L As Double, TH As Double, DTH As Double) Dim X0 As Double, Y0 As Double, XN As Double, YN As Double Dim X1 As Double, Y1 As Double, X2 As Double, Y2 As Double Dim X3 As Double, Y3 As Double, X4 As Double, Y4 As Double Dim DX As Double, DY As Double, i As Integer Dim cosTH As Double, sinTH As Double, TH0 As Double, WW As Double, LL As Double Dim g As Graphics = PictureBox1.CreateGraphics() Dim P As Brush = New SolidBrush(Color.DarkGreen) X0 = X : Y0 = Y : TH0 = TH : WW = W : LL = L cosTH = Math.Cos(TH0) : sinTH = Math.Sin(TH0) X1 = X0 - WW * sinTH : Y1 = Y0 + WW * cosTH X2 = X0 + WW * sinTH : Y2 = Y0 - WW * cosTH If L < 5 Then For i = 1 To 10 DX = (Rnd() - 0.5) * 7 : DY = (Rnd() - 0.5) * 7 P = New SolidBrush(Color.FromArgb(i * 25.5, 0, 0)) g.FillEllipse(P, IX(X + DX), IY(Y + DY), 2, 2) Next Exit Sub End If i = 0 Do While WW > 0.1 WW = WW * 0.95 XN = X0 + LL * cosTH : YN = Y0 + LL * sinTH X3 = XN - WW * sinTH : Y3 = YN + WW * cosTH X4 = XN + WW * sinTH : Y4 = YN - WW * cosTH i = (i + 1) Mod 2 If i = 0 Then DrawKirinsou(X1, Y1, WW * 0.3, LL * 0.3, TH + Pi / 3, Math.Abs(DTH) * 0.8) Else DrawKirinsou(X2, Y2, WW * 0.3, LL * 0.3, TH - Pi / 3, -Math.Abs(DTH) * 0.8) End If DrawStalk1(X1, Y1, X2, Y2, X3, Y3, X4, Y4) X0 = XN : Y0 = YN : X1 = X3 : Y1 = Y3 : X2 = X4 : Y2 = Y4 TH0 = TH0 - DTH cosTH = Math.Cos(TH0) : sinTH = Math.Sin(TH0) LL = LL * 0.9 Loop End Sub Private Sub delSCR() Dim g As Graphics = PictureBox1.CreateGraphics() g.Clear(Color.White) End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click delSCR() End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click delSCR() : DrawStalk1(-5, -5, -5, 0, 5, -5, 5, 0) DrawFern(0, 0, 5, 60, Pi / 2, 0.05) End Sub Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click delSCR() : DrawStalk2(-5, -5, -5, 0, 5, -5, 5, 0) DrawTree(0, 0, 5, 50, Pi / 2, 0.02) End Sub Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click delSCR() : DrawStalk1(-5, -5, -5, 0, 5, -5, 5, 0) DrawSugina(0, 0, 5, 50, Pi / 2, 0.05) End Sub Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click delSCR() : DrawStalk1(-5, -5, -5, 0, 5, -5, 5, 0) DrawKirinsou(0, 0, 5, 50, Pi / 2, 0.02) End Sub End Class