You are on page 1of 23

1.

RECURSIVIDAD
ejercicio

analizar el siguiente

Module Module2
Public Const maxfilas As Integer = 3500
Public Const maxcol As Integer = 100
Public contador As Integer, tcombi As Integer
Public ne As Integer = 4
Public B(maxcol) As Integer
Public B1(maxcol) As Integer
Public A(maxfilas, maxcol) As Integer
Function Diferentes(ByVal VA() As Integer,
ByVal ne As Integer) As Boolean
Dim fila As Integer, col As Integer
Dim valor As Boolean = True
For fila = 0 To ne - 1
For col = fila + 1 To ne
If VA(fila) = VA(col) Then
valor = False
Exit For
End If
Next
Next
Diferentes = valor
End Function
Sub Imprimirvector(ByVal VA() As Integer, ByVal nf As Integer)
Dim fila As Integer
For fila = 0 To nf
Console.Write(" {0} ", VA(fila).ToString.PadLeft(5))
Next
End Sub
Sub Permutaciones(ByRef A(,) As Integer, ByRef B() As Integer, ByVal B1()
As Integer, ByVal n As Integer, ByVal ne As Integer)
Dim I As Integer, k As Integer, valor
If n > 0 Then
For k = 0 To ne
B(n) = B1(k)
permutaciones(A, B, B1, n - 1, ne)
Next k
Else
For k = 0 To ne
B(n) = B1(k)
valor = Diferentes(B, ne)
If valor = True Then
For I = 0 To ne
A(contador, I) = B(I)
Next I
contador = contador + 1

End If
Next k
End If
End Sub
Sub PermutacionesConRepeticiones(ByRef A(,) As Integer, ByRef B() As
Integer, ByVal B1() As Integer, ByVal n As Integer, ByVal ne As Integer)
Dim I As Integer, k As Integer
If n > 0 Then
For k = 0 To ne
B(n) = B1(k)
PermutacionesConRepeticiones(A, B, B1, n - 1, ne)
Next k
Else
For k = 0 To ne
B(n) = B1(k)
For I = 0 To ne
A(contador, I) = B(I)
Next I
contador = contador + 1
Next k
End If
End Sub
Sub IniciarMatriz(ByVal A(,) As Integer, ByVal nf As Integer, ByVal nc As
Integer)
Dim fila As Integer, col As Integer
For fila = 0 To nf - 1
For col = 0 To nc - 1
A(fila, col) = 0
Next
Next
End Sub
Sub IniciarVector(ByVal V() As Integer, ByVal nf As Integer)
Dim fila As Integer
For fila = 0 To nf
V(fila) = fila
Next
End Sub
Sub ImprimirMatriz(ByVal A(,) As Integer, ByVal nf As Integer, ByVal nc As
Integer)
Dim fila As Integer, col As Integer
For fila = 0 To nf - 1
Console.WriteLine()
For col = 0 To nc - 1
Console.Write("{0} ", A(fila, col).ToString.PadLeft(5))
A(fila, col) = 0
Next
Next

End Sub
End Module
CODIGO DEL MODULO 1
Option Explicit On
Module Module1
Sub main()
contador = 0
ne = 3
IniciarVector(B1, ne + 1)
PermutacionesConRepeticiones(A, B, B1, ne - 1, ne - 1)
tcombi = contador
Console.WriteLine("PERMUTACIONES CON REPETICION")
ImprimirMatriz(A, tcombi, ne)
contador = 0
Console.WriteLine()
Console.WriteLine("PERMUTACIONES SIN REPETICION")
Permutaciones(A, B, B1, ne - 1, ne - 1)
tcombi = contador
ImprimirMatriz(A, tcombi, ne)
Console.ReadLine()
End Sub
End Module
Implemente el mismo problema como funciones personalizadas en Excel para
ello consulte el documento adjunto enviado
Paso 1 Clic en la ficha progrador y luego visual basic
Luego insertar modulo
Su estructura de proyecto debe quedar asi

Escriba los codigo del modulo1 y modulo 2


' ****** codigo del Modulo 1
Function PermuSinRep(Rango As Range)
contador = 0

Dim col As Integer


Dim ne As Integer
Dim B(0 To maxcol) As Integer
ne = Rango.Columns.Count
Dim B1(0 To maxcol) As Integer
For col = 0 To ne - 1
B1(col) = Rango.Cells(1, col + 1)
Next
Call PermutacionesSR(B, B1, ne - 1, ne - 1)
PermuSinRep = A
End Function
Function PermuRepeticiones(Rango As Range)
contador = 0
Dim col As Integer
Dim ne As Integer
Dim B(0 To maxcol) As Integer
ne = Rango.Columns.Count
Dim B1(0 To maxcol) As Integer
For col = 0 To ne - 1
B1(col) = Rango.Cells(1, col + 1)
Next
Call PermutacionesConRepeticiones(A, B, B1, ne - 1, ne - 1)
PermuRepeticiones = A
End Function
CODIGO DEL MODULO 2
Public Const maxfilas As Integer = 3500
Public Const maxcol As Integer = 100
Public contador As Integer, tcombi As Integer
Public B(0 To maxcol) As Integer
Public A(0 To maxfilas, 0 To maxcol) As Integer
Function Diferentes(VA() As Integer, ByVal ne As Integer) As Boolean
Dim fila As Integer, col As Integer
Dim valor As Boolean
valor = True
For fila = 0 To ne - 1
For col = fila + 1 To ne
If VA(fila) = VA(col) Then
valor = False
Exit For
End If
Next
Next
Diferentes = valor
End Function
Sub PermutacionesConRepeticiones(A() As Integer, B() As Integer, B1()
As Integer, n As Integer, ne As Integer)
Dim I As Integer, k As Integer

If n > 0 Then
For k = 0 To ne
B(n) = B1(k)
Call PermutacionesConRepeticiones(A, B, B1, n - 1, ne)
Next k
Else
For k = 0 To ne
B(n) = B1(k)
For I = 0 To ne
A(contador, I) = B(I)
Next I
contador = contador + 1
Next k
End If
End Sub
Sub PermutacionesSR(B() As Integer, B1() As Integer, n As Integer, ne As
Integer)
Dim I As Integer, k As Integer, valor
If n > 0 Then
For k = 0 To ne
B(n) = B1(k)
Call PermutacionesSR(B, B1, n - 1, ne)
Next k
Else
For k = 0 To ne
B(n) = B1(k)
valor = Diferentes(B, ne)
If valor = True Then
For I = 0 To ne
A(contador, I) = B(I)
Next I
contador = contador + 1
End If
Next k
End If
End Sub
Con esto ya se implemnto las siguientes funciones definidas por el usuario
Para usarlos use la formula de las funciones
Y se tiene las siguientes funciones definidas

Use por ejemplo la funcion PermuSinRep que obtiene las permutaciones sin
repeticiones

Seleccione las celdas amarillas con F2 y para convertirlo a matriz presione


las teclas control + shift + enter y se obtiene el siguiente resultado

Arreglar

Option Explicit On
Option Strict On
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Dim Pen As Pen
Dim Grafico As Graphics
Dim brocha As SolidBrush
Dim npuntos As Integer = 1000
Dim lx As Single = 3
Dim ly As Single = 0
Dim lambda As complexNum
Dim z0 As complexNum
Dim zx As Single = 1.5
Dim zy As Single = 0.4
'// Establace los limites de las coordenadas del plano complejo
Public xComplexMin As Single = -0.25 ' -200 * 4 '-2.0
Public xComplexMax As Single = 1.25 ' 50 * 8 ' 0.5
Public yComplexMin As Single = -0.75 '-125 * 4 '-1.25
Public yComplexMax As Single = 0.75 '125 * 4 '1.25
Public Ex As Single = 100
Public Ey As Single = 100
Public Cx As Single = 200
Public cy As Single = 200
Public Structure complexNum
Public x As Single
Public y As Single
End Structure
Sub solveQuadraticEq(ByVal lambda As complexNum, ByRef z As complexNum)
Dim lambdaMagSq, DscrMag As Single

Dim discscr As complexNum


Static fourOverLambda As complexNum
fourOverLambda.x = 0
fourOverLambda.y = 0
Static firstPoint As Boolean = True
If firstPoint = True Then
' calcula el numero compleo 4 dividido por lambda
lambdaMagSq = lambda.x * lambda.x + lambda.y * lambda.y
fourOverLambda.x = 4 * lambda.x / lambdaMagSq
fourOverLambda.y = -4 * lambda.y / lambdaMagSq
firstPoint = False
End If
discscr.x = 1 - z.x * fourOverLambda.x + z.y * fourOverLambda.y
discscr.y = z.x * fourOverLambda.y + z.y * fourOverLambda.x
DscrMag = CSng(Math.Sqrt(discscr.x * discscr.x + discscr.y * discscr.y))
If (DscrMag + discscr.x < 0) Then
z.x = 0
Else
z.x = CSng(Math.Sqrt((DscrMag - discscr.x) / 2))
End If
If (DscrMag - discscr.x < 0) Then
z.y = 0
Else
z.y = CSng(0.5 * CSng(Math.Sqrt((DscrMag - discscr.x) / 2)))
End If
'If (Rnd() < 0.5) Then
' z.x = -z.x
' z.y = -z.y
'End If
If discscr.y < 0 Then
z.x = -z.x
z.x = CSng(0.5 * (1 - z.x))
End If
End Sub
Sub plotPoint(ByVal z As complexNum)
Grafico.FillRectangle(brocha, Cx + z.x * Ex, cy + z.y * Ey, 14, 14)
End Sub
Sub SelfSqTransf(ByRef lambda As complexNum, ByVal z As complexNum, ByVal
NumPoints As Integer)
Dim k As Integer
' salta los primeros puntos
For k = 0 To 10 - 1
solveQuadraticEq(lambda, z)
Next
For k = 0 To NumPoints - 1
solveQuadraticEq(lambda, z)
plotPoint(z)
Next
End Sub

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


System.EventArgs) Handles MyBase.Load
Grafico = PictureBox1.CreateGraphics
Pen = New Pen(Color.Red, 2)
brocha = New SolidBrush(Color.FromArgb(255, 0, 0))
End Sub
Private Sub BtnFractal_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnFractal.Click
btnIniciarTodo_Click(sender, e)
lambda.x = lx
lambda.y = ly
z0.x = zx
z0.y = zy
SelfSqTransf(lambda, z0, npuntos)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnIniciar.Click
DataGridView1.ColumnCount = 2
DataGridView1.RowCount = 9
DataGridView1.Columns(1).Width = 70
For fila = 0 To DataGridView1.RowCount - 1
DataGridView1.Rows(fila).HeaderCell.Value = fila.ToString
Next
DataGridView1.Columns(0).HeaderText = "PARAMETROS"
DataGridView1.Columns(1).HeaderText = "VALOR"
DataGridView1.Rows(0).Cells(0).Value = "NPuntos"
DataGridView1.Rows(0).Cells(1).Value = npuntos
DataGridView1.Rows(1).Cells(0).Value = "lamdax"
DataGridView1.Rows(1).Cells(1).Value = lx
DataGridView1.Rows(2).Cells(0).Value = "lamday"
DataGridView1.Rows(2).Cells(1).Value = ly
DataGridView1.Rows(3).Cells(0).Value = "Cx"
DataGridView1.Rows(3).Cells(1).Value = Cx
DataGridView1.Rows(4).Cells(0).Value = "Cy"
DataGridView1.Rows(4).Cells(1).Value = Cy
DataGridView1.Rows(5).Cells(0).Value = "Ex"
DataGridView1.Rows(5).Cells(1).Value = Ex
DataGridView1.Rows(6).Cells(0).Value = "Ey"
DataGridView1.Rows(6).Cells(1).Value = Ey
DataGridView1.Rows(7).Cells(0).Value = "Zx"
DataGridView1.Rows(7).Cells(1).Value = Zx
DataGridView1.Rows(8).Cells(0).Value = "Zy"
DataGridView1.Rows(8).Cells(1).Value = Zy

End Sub

Private Sub btnIniciarTodo_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnIniciarTodo.Click
npuntos = CInt(DataGridView1.Rows(0).Cells(1).Value)
lx = CInt(DataGridView1.Rows(1).Cells(1).Value)
ly = CInt(DataGridView1.Rows(2).Cells(1).Value)
Cx = CSng(DataGridView1.Rows(3).Cells(1).Value)
cy = CSng(DataGridView1.Rows(4).Cells(1).Value)
Ex = CSng(DataGridView1.Rows(5).Cells(1).Value)
Ey = CSng(DataGridView1.Rows(6).Cells(1).Value)
zx = CSng(DataGridView1.Rows(7).Cells(1).Value)
zy = CSng(DataGridView1.Rows(8).Cells(1).Value)
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnBorrar.Click
Grafico.Clear(Color.White)
End Sub
End Class

Maduracion de una fruta platanos

Imports System.Drawing
Public Class Form1
Dim Grafico1 As Graphics
Dim Grafico2 As Graphics
Dim Grafico3 As Graphics
Dim Grafico4 As Graphics
Dim Grafico5 As Graphics

Dim Grafico6 As Graphics


Dim Grafico7 As Graphics
Dim GraficoCombinado As Graphics
Dim Contfactor As Single = 0
Dim Color As Color
Dim Pict As Bitmap
Dim Pict1 As Bitmap
Dim Pict2 As Bitmap
Dim Pict3 As Bitmap
Dim Pict4 As Bitmap
Dim Pict5 As Bitmap
Dim Pict6 As Bitmap
Dim Pict7 As Bitmap
Const maxfilas As Integer = 50, maxcol As Integer = 50
Dim alto As Integer = maxfilas, ancho As Integer = maxcol
Dim Brocha1 As SolidBrush
Dim Brocha2 As SolidBrush
Dim Brocha3 As SolidBrush
Dim Brocha4 As SolidBrush
Dim Brocha5 As SolidBrush
Dim Brocha6 As SolidBrush
Dim Brocha7 As SolidBrush
Dim brochaG As SolidBrush
Dim Rojos1(maxfilas, maxcol) As Integer, Verdes1(maxfilas, maxcol) As
Integer, Azules1(maxfilas, maxcol) As Integer
Dim Rojos2(maxfilas, maxcol) As Integer, Verdes2(maxfilas, maxcol) As
Integer, Azules2(maxfilas, maxcol) As Integer
Dim Rojos3(maxfilas, maxcol) As Integer, Verdes3(maxfilas, maxcol) As
Integer, Azules3(maxfilas, maxcol) As Integer
Dim Rojos4(maxfilas, maxcol) As Integer, Verdes4(maxfilas, maxcol) As
Integer, Azules4(maxfilas, maxcol) As Integer
Dim Rojos5(maxfilas, maxcol) As Integer, Verdes5(maxfilas, maxcol) As
Integer, Azules5(maxfilas, maxcol) As Integer
Dim Rojos6(maxfilas, maxcol) As Integer, Verdes6(maxfilas, maxcol) As
Integer, Azules6(maxfilas, maxcol) As Integer
Dim Rojos7(maxfilas, maxcol) As Integer, Verdes7(maxfilas, maxcol) As
Integer, Azules7(maxfilas, maxcol) As Integer
Dim rojo, verde, azul As Byte
Dim cx As Integer = 1, cy As Integer = 1
Dim factor As Single = 0.1
Dim velocidad As Single
Dim valor As Single = 0

Private Sub CargarGrafico(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnCargarGrafico.Click
Dim fila As Integer, col As Integer
For fila = 0 To alto - 1
For col = 0 To ancho - 1
Color = Pict1.GetPixel(col, fila)
Rojos1(fila, col) = Color.R
Verdes1(fila, col) = Color.G
Azules1(fila, col) = Color.B
Color = Pict2.GetPixel(col, fila)
Rojos2(fila, col) = Color.R
Verdes2(fila, col) = Color.G
Azules2(fila, col) = Color.B
Color = Pict3.GetPixel(col, fila)
Rojos3(fila, col) = Color.R
Verdes3(fila, col) = Color.G
Azules3(fila, col) = Color.B
Color = Pict4.GetPixel(col, fila)
Rojos4(fila, col) = Color.R
Verdes4(fila, col) = Color.G
Azules4(fila, col) = Color.B
Color = Pict5.GetPixel(col, fila)
Rojos5(fila, col) = Color.R
Verdes5(fila, col) = Color.G
Azules5(fila, col) = Color.B
Color = Pict6.GetPixel(col, fila)
Rojos6(fila, col) = Color.R
Verdes6(fila, col) = Color.G
Azules6(fila, col) = Color.B
Color = Pict7.GetPixel(col, fila)
Rojos7(fila, col) = Color.R
Verdes7(fila, col) = Color.G
Azules7(fila, col) = Color.B
Next col
Next
'System.Threading.Thread.Sleep(10) ' 1 segundo
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
PictureBox1.Width = ancho + cx
PictureBox1.Height = alto + cy

PictureBox2.Width = ancho + cx
PictureBox2.Height = alto + cy
PictureBox3.Width = ancho + cx
PictureBox3.Height = alto + cy
PictureBox4.Width = ancho + cx
PictureBox4.Height = alto + cy
PictureBox5.Width = ancho + cx
PictureBox5.Height = alto + cy
PictureBox6.Width = ancho + cx
PictureBox6.Height = alto + cy
PictureBox7.Width = ancho + cx
PictureBox7.Height = alto + cy
Cuadro.Width = ancho * 2 + cx
Cuadro.Height = alto * 2 + cy
Pict1 = New Bitmap("e:\datos2\platano1.bmp")
Pict2 = New Bitmap("e:\datos2\platano2.bmp")
Pict3 = New Bitmap("e:\datos2\platano3.bmp")
Pict4 = New Bitmap("e:\datos2\platano4.bmp")
Pict5 = New Bitmap("e:\datos2\platano5.bmp")
Pict6 = New Bitmap("e:\datos2\platano6.bmp")
Pict7 = New Bitmap("e:\datos2\platano7.bmp")
'Pict1 = New Bitmap("e:\datos2\N1.bmp")
'Pict2 = New Bitmap("e:\datos2\N2.bmp")
'Pict3 = New Bitmap("e:\datos2\N3.bmp")
'Pict4 = New Bitmap("e:\datos2\N4.bmp")
'Pict5 = New Bitmap("e:\datos2\N5.bmp")
'Pict6 = New Bitmap("e:\datos2\N6.bmp")
'Pict7 = New Bitmap("e:\datos2\N7.bmp")

Brocha1 = New SolidBrush(Drawing.Color.Aqua)


Brocha2 = New SolidBrush(Drawing.Color.Aqua)
Brocha3 = New SolidBrush(Drawing.Color.Aqua)
Brocha4 = New SolidBrush(Drawing.Color.Aqua)
Brocha5 = New SolidBrush(Drawing.Color.Aqua)
Brocha6 = New SolidBrush(Drawing.Color.Aqua)
Brocha7 = New SolidBrush(Drawing.Color.Aqua)

brochaG = New SolidBrush(Drawing.Color.Aqua)


Grafico1 = PictureBox1.CreateGraphics
Grafico2 = PictureBox2.CreateGraphics
Grafico3 = PictureBox3.CreateGraphics
Grafico4 = PictureBox4.CreateGraphics

Grafico5 = PictureBox5.CreateGraphics
Grafico6 = PictureBox6.CreateGraphics
Grafico7 = PictureBox7.CreateGraphics
GraficoCombinado = Cuadro.CreateGraphics
End Sub
Private Sub Iniciar(sender As Object, e As EventArgs) Handles
BtnIniciar.Click
For fila = 0 To alto - 1
For col = 0 To ancho - 1
rojo = Rojos1(fila, col)
verde = Verdes1(fila, col)
azul = Azules1(fila, col)
Brocha1.Color = Color.FromArgb(rojo, verde, azul)
Grafico1.FillRectangle(Brocha1, cx + col, cy + fila, 1, 1)
rojo = Rojos2(fila, col)
verde = Verdes2(fila, col)
azul = Azules2(fila, col)
Brocha2.Color = Color.FromArgb(rojo, verde, azul)
Grafico2.FillRectangle(Brocha2, cx + col, cy + fila, 1, 1)
rojo = Rojos3(fila, col)
verde = Verdes3(fila, col)
azul = Azules3(fila, col)
Brocha3.Color = Color.FromArgb(rojo, verde, azul)
Grafico3.FillRectangle(Brocha3, cx + col, cy + fila, 1, 1)
rojo = Rojos4(fila, col)
verde = Verdes4(fila, col)
azul = Azules4(fila, col)
Brocha4.Color = Color.FromArgb(rojo, verde, azul)
Grafico4.FillRectangle(Brocha4, cx + col, cy + fila, 1, 1)
rojo = Rojos5(fila, col)
verde = Verdes5(fila, col)
azul = Azules5(fila, col)
Brocha5.Color = Color.FromArgb(rojo, verde, azul)
Grafico5.FillRectangle(Brocha5, cx + col, cy + fila, 1, 1)
rojo = Rojos6(fila, col)
verde = Verdes6(fila, col)
azul = Azules6(fila, col)
Brocha6.Color = Color.FromArgb(rojo, verde, azul)
Grafico6.FillRectangle(Brocha6, cx + col, cy + fila, 1, 1)
rojo = Rojos7(fila, col)
verde = Verdes7(fila, col)

azul = Azules7(fila, col)


Brocha7.Color = Color.FromArgb(rojo, verde, azul)
Grafico7.FillRectangle(Brocha7, cx + col, cy + fila, 1, 1)

Next
Next
Contfactor = 0
End Sub
Private Sub btnMadurar_Click_1(sender As Object, e As EventArgs)
Handles btnMadurar.Click
velocidad = TextBox1.Text
factor = TextBox2.Text
Timer1.Interval = velocidad
Timer1.Enabled = True
End Sub
Private Sub btnDetener_Click(sender As Object, e As EventArgs) Handles
btnDetener.Click
Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles
Timer1.Tick
Contfactor = Contfactor + factor
TextBox4.Text = Contfactor
Graficar(sender, e)
End Sub
Sub graficar12(valor As Single)
Dim rojo12, verde12, azul12 As Integer
For fila = 0 To alto - 1
For col = 0 To ancho - 1
rojo12 = Rojos1(fila, col) * (1 - valor) + Rojos2(fila, col) * valor
verde12 = Verdes1(fila, col) * (1 - valor) + Verdes2(fila, col) * valor
azul12 = Azules1(fila, col) * (1 - valor) + Azules2(fila, col) * valor
brochaG.Color = Color.FromArgb(rojo12, verde12, azul12)
GraficoCombinado.FillRectangle(brochaG, cx + col, cy + fila, 1, 1)
Next
Next
End Sub
Sub graficar23(valor As Single)
Dim rojo23, verde23, azul23 As Integer
For fila = 0 To alto - 1
For col = 0 To ancho - 1
rojo23 = Rojos2(fila, col) * (1 - valor) + Rojos3(fila, col) * valor

verde23 = Verdes2(fila, col) * (1 - valor) + Verdes3(fila, col) * valor


azul23 = Azules2(fila, col) * (1 - valor) + Azules3(fila, col) * valor
brochaG.Color = Color.FromArgb(rojo23, verde23, azul23)
GraficoCombinado.FillRectangle(brochaG, cx + col, cy + fila, 1, 1)
Next
Next
End Sub
Sub graficar34(valor As Single)
Dim rojo34, verde34, azul34 As Integer
For fila = 0 To alto - 1
For col = 0 To ancho - 1
rojo34 = Rojos3(fila, col) * (1 - valor) + Rojos4(fila, col) * valor
verde34 = Verdes3(fila, col) * (1 - valor) + Verdes4(fila, col) * valor
azul34 = Azules3(fila, col) * (1 - valor) + Azules4(fila, col) * valor
brochaG.Color = Color.FromArgb(rojo34, verde34, azul34)
GraficoCombinado.FillRectangle(brochaG, cx + col, cy + fila, 1, 1)
Next
Next
End Sub
Sub graficar45(valor As Single)
Dim rojo45, verde45, azul45 As Integer
For fila = 0 To alto - 1
For col = 0 To ancho - 1
rojo45 = Rojos4(fila, col) * (1 - valor) + Rojos5(fila, col) * valor
verde45 = Verdes4(fila, col) * (1 - valor) + Verdes5(fila, col) * valor
azul45 = Azules4(fila, col) * (1 - valor) + Azules5(fila, col) * valor
brochaG.Color = Color.FromArgb(rojo45, verde45, azul45)
GraficoCombinado.FillRectangle(brochaG, cx + col, cy + fila, 1, 1)
Next
Next
End Sub

Sub graficar56(valor As Single)


Dim rojo56, verde56, azul56 As Integer
For fila = 0 To alto - 1
For col = 0 To ancho - 1
rojo56 = Rojos5(fila, col) * (1 - valor) + Rojos6(fila, col) * valor
verde56 = Verdes5(fila, col) * (1 - valor) + Verdes6(fila, col) * valor
azul56 = Azules5(fila, col) * (1 - valor) + Azules6(fila, col) * valor
brochaG.Color = Color.FromArgb(rojo56, verde56, azul56)
GraficoCombinado.FillRectangle(brochaG, cx + col, cy + fila, 1, 1)
Next
Next

End Sub

Sub graficar67(valor As Single)


Dim rojo67, verde67, azul67 As Integer
For fila = 0 To alto - 1
For col = 0 To ancho - 1
rojo67 = Rojos6(fila, col) * (1 - valor) + Rojos7(fila, col) * valor
verde67 = Verdes6(fila, col) * (1 - valor) + Verdes7(fila, col) * valor
azul67 = Azules6(fila, col) * (1 - valor) + Azules7(fila, col) * valor
brochaG.Color = Color.FromArgb(rojo67, verde67, azul67)
GraficoCombinado.FillRectangle(brochaG, cx + col, cy + fila, 1, 1)
Next
Next
End Sub

Private Sub Graficar(sender As Object, e As EventArgs) Handles


btnGraficar.Click
Contfactor = TextBox4.Text
If Contfactor <= 1 Then
graficar12(Contfactor)
Else
If Contfactor <= 2 Then
graficar23(Contfactor - 1)
Else
If Contfactor <= 3 Then
graficar34(Contfactor - 2)
Else
If Contfactor <= 4 Then
graficar45(Contfactor - 3)
Else
If Contfactor <= 5 Then
graficar56(Contfactor - 4)
Else
If Contfactor <= 6 Then
graficar67(Contfactor - 5)
Else
Contfactor = 0
End If
End If
End If
End If
End If
End If
End Sub

Private Sub btnManual_Click(sender As Object, e As EventArgs) Handles


btnManual.Click
Contfactor = Contfactor + factor
TextBox4.Text = Contfactor
Graficar(sender, e)
End Sub
End Class

Problema de recorrido

Versin mejorada en modo DOS

Imports System.IO
Module Module2
Public camino As Integer = 0
Public Const niveles As Integer = 2
Public Const ne As Integer = 6
Public X(ne) As Integer
Public Y(ne) As Integer
Public Const maxfilas As Integer = 20
Public Const maxcol As Integer = 40
Public nf As Integer = 20
Public nc As Integer = 40
Public A(maxfilas, maxcol) As Integer
Public Cx As Integer = 1

Public Cy As Integer = 2
Sub VerMatriz(Cx As Integer, Cy As Integer, A(,) As Integer, nf As Integer, nc
As Integer)
Dim fila, col As Integer
For fila = 0 To nf - 1
For col = 0 To nc - 1
Console.SetCursorPosition(Cx + col, Cy + fila)
Console.ForegroundColor = A(fila, col)
Console.Write("{0}", A(fila, col))
Next
Next
End Sub
Sub ImPrimirSer(Cx As Integer, Cy As Integer, X() As Integer, Y() As Integer,
ne As Integer)
Dim fila As Integer
For fila = 0 To ne - 1
Console.ForegroundColor = 14 - fila
Console.SetCursorPosition(Cx + X(fila), Cy + Y(fila))
Console.Write("{0}", fila + 1)
Next
End Sub
Sub RecuperarMatriz(ByVal nombrearchivo As String, ByVal A(,) As Integer,
ByVal nf As Integer, ByVal nc As Integer)
Dim srLector As StreamReader
srLector = New StreamReader(nombrearchivo)
Dim fila As Integer, col As Integer
Dim cadena As String = ""
Dim subcadena As String
Dim pos As Integer = 0
Dim inicio As Integer = 1
For fila = 0 To nf - 1
cadena = srLector.ReadLine()
cadena = cadena & Chr(9)
inicio = 1
For col = 0 To nc - 1
pos = InStr(inicio, cadena, Chr(9))
subcadena = Mid(cadena, inicio, pos - inicio)
A(fila, col) = Val(subcadena)
inicio = pos + 1
Next
Next
'Form1.ListBox1.Items.Add("Archivo leido satisfactoriamente")
srLector.Close()
End Sub
Function ContarVecinos(Cx As Integer, Cy As Integer, nivel As Integer)
Dim fila, col As Integer
Dim suma As Integer = 0
Dim valor As Integer

Dim r As Integer
For fila = Cy - nivel To Cy + nivel
For col = Cx - nivel To Cx + nivel
valor = A(fila, col)
r = evaluar(col, fila)
If (valor = camino And r = 1) Then suma = suma + 1
Next
Next
Return suma
End Function
Function buscar(cx As Integer, cy As Integer)
Dim fila As Integer
Dim r1 As Integer = 0
For fila = 0 To ne - 1
If (X(fila) = cx And Y(fila) = cy) Then
r1 = 1
Exit For
End If
Next
Return r1
End Function
Function ContarCerca(cx As Integer, cy As Integer, nivel As Integer) As
Integer
Dim fila As Integer
Dim suma As Integer = 0
Dim valor As Integer = 0
' cuanto vecinos mas cercanos t
For fila = cy - nivel To cy + nivel
For col = cx - nivel To cx + nivel
valor = buscar(col, fila)
If (valor = 1) Then suma = suma + 1
Next
Next
Return suma
End Function
Function Evaluar(cx As Integer, cy As Integer) As Integer
Dim r1 As Integer = 0
Dim r2 As Integer = 1
Dim r3 As Integer = 0
If (A(cy, cx) = camino) Then r1 = 1
For fila = 0 To ne - 1
If cx = X(fila) And cy = Y(fila) Then
r2 = 0
Exit For
End If
Next
If (r1 = 1 And r2 = 1) Then r3 = 1
Return r3
End Function

Function ModificarSer(X() As Integer, Y() As Integer) As Integer


Dim res As Integer
Dim Cx, Cy As Integer
Dim mayor As Integer = -1
Dim menor As Integer = 100
Dim cont As Integer = 0
Cx = X(0)
Cy = Y(0)
Dim Suma(3) As Integer
Dim smenor(3) As Integer
Dim dirMenor As Integer = 100
Dim dir1 As Integer = -1
For fila = 0 To 3
Suma(fila) = -1
smenor(fila) = 100
Next
If (Evaluar(Cx + 1, Cy) = 1) Then Suma(0) = ContarVecinos(Cx + 1, Cy,
niveles)
If (Evaluar(Cx, Cy - 1) = 1) Then Suma(1) = ContarVecinos(Cx, Cy - 1,
niveles)
If (Evaluar(Cx - 1, Cy) = 1) Then Suma(2) = ContarVecinos(Cx - 1, Cy,
niveles)
If (Evaluar(Cx, Cy + 1) = 1) Then Suma(3) = ContarVecinos(Cx, Cy + 1,
niveles)
If (Evaluar(Cx + 1, Cy) = 1) Then smenor(0) = ContarCerca(Cx + 1, Cy,
niveles)
If (Evaluar(Cx, Cy - 1) = 1) Then smenor(1) = ContarCerca(Cx, Cy - 1,
niveles)
If (Evaluar(Cx - 1, Cy) = 1) Then smenor(2) = ContarCerca(Cx - 1, Cy,
niveles)
If (Evaluar(Cx, Cy + 1) = 1) Then smenor(3) = ContarCerca(Cx, Cy + 1,
niveles)
' encontrar el mayor
If (Suma(0) >= mayor) Then
mayor = Suma(0)
dir1 = 1
End If
If (Suma(1) >= mayor) Then
mayor = Suma(1)
dir1 = 2
End If
If (Suma(2) >= mayor) Then
mayor = Suma(2)
dir1 = 3
End If
If (Suma(3) >= mayor) Then
mayor = Suma(3)
dir1 = 4
End If
If dir1 < 0 Then Return dir1

' si hay varios mayores empate si escoge el que tiene menos vecinos
For fila = 0 To 3
If Suma(fila) >= mayor Then
If (smenor(fila) <= menor) Then
menor = smenor(fila)
dirMenor = fila
cont = cont + 1
End If
End If
Next
' si hay mas de dos
For fila = ne - 1 To 1 Step -1
X(fila) = X(fila - 1)
Y(fila) = Y(fila - 1)
Next
If cont = 1 Then
' modificamos
Select Case dir1
Case 1
X(0) = Cx + 1
Case 2
Y(0) = Cy - 1
Case 3
X(0) = Cx - 1
Case 4
Y(0) = Cy + 1
End Select
Else ' si hay empate
Select Case dirMenor + 1
Case 1
X(0) = Cx + 1
Case 2
Y(0) = Cy - 1
Case 3
X(0) = Cx - 1
Case 4
Y(0) = Cy + 1
End Select
End If
Return dir1
End Function
End Module

Module Module1
Sub Main()
Dim Nombre As String = "E:\datos1\matriz40x20.txt"

Dim res As Integer, fila As Integer


For fila = 0 To ne - 1
X(fila) = 4
Y(fila) = 5 + fila
Next
RecuperarMatriz(Nombre, A, nf, nc)
VerMatriz(Cx, Cy, A, nf, nc)
Console.ForegroundColor = 14
ImPrimirSer(Cx, Cy, X, Y, ne)
Console.ReadLine()
Console.ForegroundColor = 10
While (1)
' Console.Clear()
VerMatriz(Cx, Cy, A, nf, nc)
res = ModificarSer(X, Y)
ImPrimirSer(Cx, Cy, X, Y, ne)
If res <= 0 Then
Exit While
End If
System.Threading.Thread.Sleep(50) ' 1 segundo
' Console.ReadLine()
End While
Console.WriteLine("FINal\b")
Console.ReadLine()
End Sub
End Module

You might also like