, Visual C#

       


41.1. Form1_Load.

' z = f = f(x, y):

Private Function f(ByVal x As Single, ByVal y As Single) _

As Single

f = 2 * x * x * x * x - 3 * x * x + 4 * y * y

End Function

' df/dx:

Private Function df_dx(ByVal x As Single, _

ByVal y As Single) As Single

df_dx = 8 * x * x * x - 6 * x

End Function

' df/dy:

Private Function df_dy(ByVal x As Single, _

ByVal y As Single) As Single

df_dy = 8 * y

End Function

' :

Private myPen As Pen

' :

Private Sub Form1_Load(ByVal sender As System.Object, _

ByVal e As EventArgs) Handles MyBase.Load

' :

myPen = New Pen(Color.Black, 0)

' PictureBox1

' g Graphics:

Dim bmp As New Bitmap(PictureBox1.ClientSize.Width, _

PictureBox1.ClientSize.Height)

Dim g As Graphics = Graphics.FromImage(bmp)

'

' PictureBox1

'-1.5 <= x <= 1.5, -1.5 <= y <= 1.5:

Const x_min As Single = -1.5

Const x_max As Single = 1.5

Const y_min As Single = -1.5

Const y_max As Single = 1.5

g.ScaleTransform(bmp.Width / (x_max - x_min), _

bmp.Height / (y_max - y_min))

g.TranslateTransform(-x_min, -y_min, _

System.Drawing.Drawing2D.MatrixOrder.Prepend)

' :

For LevelCurves As Integer = -3 To 10

PlotLevelCurve(g, CSng( _

LevelCurves / 4), -4, 4, -4, 4, 0.05, 1, 1, 0.002)

Next

' :

PictureBox1.Image = bmp

End Sub


.
41.2.
.
' :
Private Sub FindPointOnCurve(ByRef x As Single, _
ByRef y As Single, ByVal LevelCurves As Single, _
Optional ByVal start_x As Single = 0.1, _
Optional ByVal start_y As Single = 0.2, _
Optional ByVal tolerance As Single = 0.01, _
Optional ByVal initial_delta As Single = 0.1)
Dim dx As Single : Dim dy As Single
Dim dz As Single : Dim dist As Single
Dim delta As Single : Dim f_xy As Single
Dim direction As Integer
' :
x = start_x : y = start_y : delta = initial_delta
' :
Do
f_xy = f(x, y) : dz = LevelCurves - f_xy
If Abs(dz) < tolerance Then Exit Do
' :
If Sign(dz) <> direction Then
' . delta:
delta = delta / 2 : direction = Sign(dz)
End If
' :
Gradient(x, y, dx, dy)
If Abs(dx) + Abs(dy) < 0.001 Then Exit Do
' :
x = x + dx * delta * direction
y = y + dy * delta * direction
Loop
End Sub
' :
Private Sub Gradient(ByVal x As Single, _
ByVal y As Single, ByRef dx As Single, ByRef dy As Single)
Dim dist As Single
dx = df_dx(x, y) : dy = df_dy(x, y)
dist = CSng(Sqrt(dx * dx + dy * dy))
If Abs(dist) < 0.0001 Then
dx = 0 : dy = 0
Else
dx = dx / dist : dy = dy / dist
End If
End Sub
' :
' myArrayVB_2(i, j):
Dim N_x_2 As Integer = 20000
Dim N_y_2 As Integer = 1
' myArrayVB_2(i, j) Single ,


' i = 0,1,2,3,...,N_x; j = 0,1,2,3,...,N_y:
Dim myArrayVB_2(20000, 1) As Single '- .
' myArrayVB_2:
Dim N_1_myArrayVB_2 As Integer
' :
Dim ii As Integer = -1
' f(x, y) = LevelCurves:
Private Sub PlotLevelCurve(ByVal g As Graphics, _
ByVal LevelCurves As Single, ByVal x_min As Single, _
ByVal x_max As Single, ByVal y_min As Single, _
ByVal y_max As Single, _
Optional ByVal step_size As Single = 0.1, _
Optional ByVal start_x As Single = 1.0, _
Optional ByVal start_y As Single = 1.0, _
Optional ByVal tolerance As Single = 0.02)
' :
Dim i, j As Integer
' 1- .
' 1- myArrayVB(i, j):
Dim N_x As Integer = 2000
Dim N_y As Integer = 1
' 1- myArrayVB(i, j) Single,
' i = 0,1,2,3,...,N_x; j = 0,1,2,3,...,N_y:
Dim myArrayVB(N_x, N_y) As Single '- .
' myArrayVB:
Dim N_1_myArrayVB As Integer
' :
Dim num_points As Integer
Dim x0 As Single : Dim y0 As Single
Dim x1 As Single : Dim y1 As Single
Dim x2 As Single : Dim y2 As Single
Dim dx As Single : Dim dy As Single
' (x0, y0) LevelCurves:
FindPointOnCurve(x0, y0, LevelCurves, _
start_x, start_y, tolerance)
' :
num_points = 1
' LevelCurves:
x2 = x0
y2 = y0
i = -1 ' .
' Do - Loop:
Do
x1 = x2
y1 = y2
' :
Gradient(x2, y2, dx, dy)


If Abs(dx) + Abs(dy) < 0.001 Then Exit Do
x2 = x2 + dy * step_size
y2 = y2 - dx * step_size
FindPointOnCurve(x2, y2, LevelCurves, x2, y2, _
tolerance)
' ( ) :
'g.DrawLine(myPen, x1, y1, x2, y2)
' - 1-
' , :
i = i + 2
myArrayVB(i, 0) = x1
myArrayVB(i, 1) = y1
myArrayVB(i + 1, 0) = x2
myArrayVB(i + 1, 1) = y2
N_1_myArrayVB = i + 1 ' .
'
' 2- , :
ii = ii + 2
myArrayVB_2(ii, 0) = x1
myArrayVB_2(ii, 1) = y1
myArrayVB_2(ii + 1, 0) = x2
myArrayVB_2(ii + 1, 1) = y2
N_1_myArrayVB_2 = ii + 1 ' .
' :
num_points = num_points + 1
', :
If x2 < x_min Or x2 > x_max Or _
y2 < y_min Or y2 > y_max _
Then Exit Do
' 4 ,
' :
If num_points >= 4 Then
If Sqrt((x0 - x2) * (x0 - x2) + (y0 - y2) _
* (y0 - y2)) <= step_size * 1.1 Then
' ( ):
'g.DrawLine(myPen, x2, y2, x0, y0)
' 1- :
i = i + 2
myArrayVB(i, 0) = x2
myArrayVB(i, 1) = y2
myArrayVB(i + 1, 0) = x0
myArrayVB(i + 1, 1) = y0
N_1_myArrayVB = i + 1 ' .


' 2- ,
' :
ii = ii + 2
myArrayVB_2(ii, 0) = x2
myArrayVB_2(ii, 1) = y2
myArrayVB_2(ii + 1, 0) = x0
myArrayVB_2(ii + 1, 1) = y0
N_1_myArrayVB_2 = ii + 1 ' .
Exit Do
End If
End If
Loop ' Do - Loop.
' N_first_line N_last_line
' myArrayVB:
Dim N_first_line, N_last_line As Integer
N_first_line = 1
N_last_line = N_1_myArrayVB
' N_first_line
' N_last_line
'myArrayVB(0, 0) myArrayVB(0, 1):
myArrayVB(0, 0) = N_first_line
myArrayVB(0, 1) = N_last_line
' myArrayVB(2000, 1):
Dim k As Integer
i = -1
For k = N_first_line To N_last_line
i = i + 2
x1 = myArrayVB(i, 0)
y1 = myArrayVB(i, 1)
x2 = myArrayVB(i + 1, 0)
y2 = myArrayVB(i + 1, 1)
g.DrawLine(myPen, x1, y1, x2, y2)
Next
' N_first_line_2 N_last_line_2
' myArrayVB_2 :
Dim N_first_line_2, N_last_line_2 As Integer
N_first_line_2 = 1
N_last_line_2 = N_1_myArrayVB_2
' N_first_line_2
' N_last_line_2
'myArrayVB_2(0, 0) myArrayVB_2(0, 1):
myArrayVB_2(0, 0) = N_first_line_2
myArrayVB_2(0, 1) = N_last_line_2
' - myArrayVB_2(20000, 1) .
' sw StreamWriter
' D:\MyDocs\MyTest_LevelCurves.txt.
' "":
Dim sw As StreamWriter = _
New StreamWriter("D:\MyDocs\MyTest_LevelCurves.txt")
' myArrayVB_2(i, j) -
' WriteLine:
For i = 0 To N_x_2
For j = 0 To N_y_2
sw.WriteLine(myArrayVB_2(i, j))
Next
Next
sw.Close()
End Sub
. Visual C#, Visual C++ ( ) , , .









Forekc.ru
, , , , , , , , , ,