Public Module Draw

    'Dim colCLR As Color = Color.Transparent 
    Dim colCLR As Color = Color.White

    Dim W1 As Integer = My.Resources.Resource1._1.Width
    Dim H1 As Integer = My.Resources.Resource1._1.Height

    Dim W2 As Integer = My.Resources.Resource1._2.Width
    Dim H2 As Integer = My.Resources.Resource1._2.Height

    Dim NRad As Integer = W2 / 4
    Dim Nsh As Integer = W2 / 8
    Dim NStep As Integer = (W2 - NRad - Nsh) / Main.N_Med


    Public pic_sp As New Bitmap(W1, H1)

    Dim G_sp As Graphics = Graphics.FromImage(pic_sp)

    Public pic_sp2 As New Bitmap(W1, H1)

    Dim G_sp2 As Graphics = Graphics.FromImage(pic_sp2)


    Public pic_ev As New Bitmap(W1, H1)

    Dim G_ev As Graphics = Graphics.FromImage(pic_ev)


    Dim r2 As Integer = W2 / 2

    Dim WY As Double = W1 * 408 / 600

    Dim img1 As Bitmap = My.Resources.Resource1._1
    Dim img2 As Bitmap = My.Resources.Resource1._2
    Dim img3 As Bitmap = My.Resources.Resource1._3

    Dim imgM(Main.N_Med - 1) As Bitmap

    Dim R(2, 2) As Double

    Public Sub Ini(alfa As Double, betta As Double)

        Main.PictureBox1.Width = W1 * Main.PictureBox1.Height / H1
        Main.PictureBox2.Width = W1 * Main.PictureBox2.Height / H1
        Main.PictureBox4.Width = W1 * Main.PictureBox2.Height / H1



        ' Расчет матрицы поворота для аксанометрии
        Dim r1(,) As Double = New Double(2, 2) {{1, 0, 0}, {0, Math.Cos(alfa), Math.Sin(alfa)}, {0, -Math.Sin(alfa), Math.Cos(alfa)}}
        Dim r2(,) As Double = New Double(2, 2) {{Math.Cos(betta), 0, -Math.Sin(betta)}, {0, 1, 0}, {Math.Sin(betta), 0, Math.Cos(betta)}}

        Dim s As Double

        For i = 0 To 2

            For j = 0 To 2

                s = 0

                For k = 0 To 2

                    s += r1(i, k) * r2(k, j)

                Next

                R(i, j) = s

            Next

        Next

        imgM(0) = New Bitmap(My.Resources.Resource1.m1)
        imgM(1) = New Bitmap(My.Resources.Resource1.m2)
        imgM(2) = New Bitmap(My.Resources.Resource1.m3)
        imgM(3) = New Bitmap(My.Resources.Resource1.m4)
        imgM(4) = New Bitmap(My.Resources.Resource1.m5)


    End Sub
   
    Public Sub Draw_Info()

        Dim st As String = ""

        For i = 0 To Main.N_Med - 1

            st &= i.ToString & " - "

            For Each j In Main.Info(i)

                st &= j.ToString & " "

            Next

            st &= vbCrLf

        Next

        Main.TextBox1.Text = st

    End Sub
    Public Sub DrawA_sp_ev()

        Dim b As Boolean

        G_sp.Clear(colCLR)

        G_sp.DrawImage(img1, 0, 0, W1, H1)


        For ix = 0 To Main.X_GC - 1
            For iy = 0 To Main.Y_GC - 1

                If Main.GCol(ix, iy).InUse Then

                    b = False

                    For iz = 0 To Main.Z_Col - 1

                        If Main.GCol(ix, iy).Column(iz).A_Spont Or Main.GCol(ix, iy).Column(iz).A_Evoked Then

                            For j = 0 To Main.N_Med - 1

                                DrawNeuron(G_sp, ix, iy, iz)

                                b = True

                            Next


                        End If

                    Next

                    If b Then
                        DrawCol_slim(G_sp, ix, iy)
                    End If




                End If

            Next
        Next




        Main.PictureBox1.Image = pic_sp

        Draw_Info()

    End Sub

    Public Sub DrawA_sp2()

        Dim b As Boolean

        pic_sp2 = pic_ev.Clone

        G_sp2 = Graphics.FromImage(pic_sp2)

        For ix = 0 To Main.X_GC - 1
            For iy = 0 To Main.Y_GC - 1

                If Main.GCol(ix, iy).A Then

                    b = False

                    For iz = 0 To Main.Z_Col - 1

                        If Main.GCol(ix, iy).Column(iz).A_Spont Then

                            For j = 0 To Main.N_Med - 1

                                DrawNeuron(G_sp2, ix, iy, iz)

                                b = True

                            Next


                        End If

                    Next

                    If b Then
                        DrawCol_slim(G_sp2, ix, iy)
                    End If




                End If

            Next
        Next




        Main.PictureBox4.Image = pic_sp2

        Draw_Info()

    End Sub

    Public Sub DrawA_ev()


        G_ev.Clear(colCLR)

        G_ev.DrawImage(img1, 0, 0, W1, H1)


        For ix = 0 To Main.X_GC - 1
            For iy = 0 To Main.Y_GC - 1

                If Main.GCol(ix, iy).A Then
                    DrawCol_wide(G_ev, ix, iy)
                End If

            Next
        Next


        Main.PictureBox2.Image = pic_ev

        Draw_Info()

    End Sub

    Private Sub DrawCol_slim(ByRef G As Graphics, ix As Integer, iy As Integer)

        Dim x, y As Integer

        Transf(ix, iy, 0, x, y)


        G.DrawImage(img3, x + 18, y, img3.Width, img3.Height)


    End Sub

    Private Sub DrawCol_wide(ByRef g As Graphics, ix As Integer, iy As Integer)

        Dim x, y As Integer

        Transf(ix, iy, 0, x, y)


        g.DrawImage(img2, x, y, img2.Width, img2.Height)


    End Sub
    Private Sub DrawNeuron(ByRef G As Graphics, ix As Integer, iy As Integer, iz As Integer)

        Dim x, y As Integer

        For i = 0 To Main.N_Med - 1

            If Main.GCol(ix, iy).Column(iz).A_med_sp(i) Or Main.GCol(ix, iy).Column(iz).A_med_ev(i) Then

                Transf(ix, iy, iz, x, y)

                G.DrawImage(imgM(i), x + i * 6, y + W2)

            End If


        Next



    End Sub

    Public Sub Transf(ix As Integer, iy As Integer, iz As Integer, ByRef x As Integer, ByRef y As Integer)

        Dim A(2) As Double
        Dim C(2) As Double

        Dim Y_shift As Integer = W1 * 0.15
        Dim X_shift As Integer = W1 * 0.01

        A(0) = ix / Main.X_GC * W1 * 0.95
        A(2) = iy / Main.Y_GC * W1 * 0.95
        A(1) = 0

        Dim s As Double

        For i = 0 To 2

            s = 0

            For j = 0 To 2

                s += R(j, i) * A(j)

            Next

            C(i) = s

        Next

        x = C(0) + x_shift
        y = C(2) + Y_shift + iz / Main.Z_Col * H2 * 0.9


    End Sub

    ' Картина поля 
    Public Sub Draw_Fields()

        Dim pic1 As New Bitmap(X_C, Y_C)

        Dim Y As Integer

        For ix = 0 To X_C - 1
            For iy = 0 To Y_C - 1


                Y = SumFields(ix, iy) * 100

                If Y > 255 Then Y = 255

                pic1.SetPixel(ix, iy, Color.FromArgb(Y, Y, Y))

            Next
        Next

        Main.PictureBox3.Image = pic1
        Main.PictureBox3.Update()

    End Sub

End Module
