Mandel


Public Class Complex
Public re, im As Double
Public Imag, Real As Double
Public Sub New()
Real = 0
Imag = 0
End Sub
Public Sub New(ByVal re As Double, ByVal im As Double)
Me.Real = re
Me.Imag = im
End Sub
Public Function Add(ByVal z As Complex) As Complex
Return New Complex(Me.Real + z.Real, Me.Imag + z.Imag)
End Function
Public Function Mul(ByVal z As Complex) As Complex
Return New Complex(Me.Real * z.Real - Me.Imag * z.Imag, _
Me.Real * z.Imag + Me.Imag * z.Real)
End Function
Public Function Norm() As Double
Return Me.Real * Me.Real + Me.Imag * Me.Imag
End Function
Public Overrides Function ToString() As String
Return String.Format("({0} + i {1})", Real, Imag)
End Function
End Class





***************************************



Public Class Form1
Inherits System.Windows.Forms.Form

' bmp jest globalne dla klasy
' w tej bitmapie rysujemy fraktal
Dim bmp As Bitmap

#Region " Windows Form Designer generated code "



'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.PictureBox1 = New System.Windows.Forms.PictureBox
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(40, 24)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Start"
'
'PictureBox1
'
Me.PictureBox1.Location = New System.Drawing.Point(64, 16)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(400, 240)
Me.PictureBox1.TabIndex = 1
Me.PictureBox1.TabStop = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(480, 273)
Me.Controls.Add(Me.PictureBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Fraktal Mandelbrota"
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

' konstrukcja obiektu Bitmap o wymiarach takich jak PictureBox
' moze byc tutaj albo np w metodzie Load
bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)


End Sub

Private Sub PictureBox1_Paint(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
' rysujemy zawartosc obiektu bmp
e.Graphics.DrawImage(bmp, 0, 0)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim min, max, cc, p, z As Complex
Dim i, j, ind As Integer

cc = New Complex()
min = New Complex(-2.0, -1.0)
max = New Complex(1.0, 1.0)

' tutaj lepiej for bo i tak ma sie zawsze wykonac
' tyle samo razy
For j = 0 To bmp.Height - 1
For i = 0 To bmp.Width - 1

cc.Real = i * (max.Real - min.Real) / bmp.Width
cc.Imag = j * (max.Imag - min.Imag) / bmp.Height
p = min.Add(cc)
z = New Complex(0, 0)

ind = 0
Dim ok As Boolean = True

' sprawdzamy czy punkt nalezy do zbioru
' 255 maksymalna ilosc iteracji
' jesli po 255 dalej nalezy to zakladamy ze nalezy
' wieksza wartosc da dokladniejsza klasyfikacje
' zwiekszajac czas obliczen
Do While (ind < 255 And ok)
z = z.Mul(z).Add(p)
If (z.Norm() > 4) Then
ok = False
End If
ind = ind + 1
Loop

' ok=true znaczy ze punkt nalezy do zbioru
If ok Then
bmp.SetPixel(i, j, Color.Yellow)
Else
bmp.SetPixel(i, j, Color.Red)
End If
Next i
' % w c# to Mod w VB
' co ile wierszy odmalowywac w trakcie liczenia
If j Mod 6 = 0 Then
Application.DoEvents()
PictureBox1.Invalidate()
End If
Next j
PictureBox1.Invalidate()

End Sub


End Class

Wyszukiwarka

Podobne podstrony:
mandel05
mandel
1312061056481206 witn mandela
Krzywe Mandelbrota
PROJEKT FRAKTAL Mandelbar csproj FileListAbsolute
Osip Mandelsztam Wiersze
FRAKTAL Mandelbar csproj FileListAbsolute
mandelit
Mandelbar csproj FileListAbsolute
Zbiór Mandelbrota
Mandelli CNM Plasma MZ44 89 1m
Avner Mandelman
mandel
Mandel Socjalizm w przededniu 21 w

więcej podobnych podstron