Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

PRINCIPIO DE RECURSIVIDAD: GRÁFICOS

EJEMPLO 1
CODIGO:
Module Module1
Function distancia(ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single,
ByVal y2 As Single) As Single
distancia = CSng(Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)))
End Function
Sub linea(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer)
Dim cx, cy As Integer
Console.SetCursorPosition(x1, y1)
Console.Write("*")
Console.SetCursorPosition(x2, y2)
Console.Write("*")
If distancia(x1, y1, x2, y2) >= 2 Then
cx = (x1 + x2) / 2
cy = (y1 + y2) / 2
linea(x1, y1, cx, cy)
linea(cx, cy, x2, y2)
End If
End Sub
Sub Main()
Dim x1, y1, x2, y2, x3, y3 As Integer
x1 = 20 : y1 = 22
x2 = 40 : y2 = 2
x3 = 60 : y3 = 22
Console.ForegroundColor = 9
linea(x1, y1, x2, y2)
Console.ForegroundColor = 10
linea(x2, y2, x3, y3)
Console.ForegroundColor = 12
linea(x3, y3, x1, y1)
Console.ReadLine()
End Sub
End Module

IMAGEN:
EJEMPLO 2:
CODIGO:
Module Module1

Function dist(ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal


y2 As Single) As Single

dist = CSng(Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)))

End Function

Sub linea(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer)

Dim cx, cy As Integer

Console.SetCursorPosition(x1, y1)

Console.Write("*")

Console.SetCursorPosition(x2, y2)

Console.Write("*")

If dist(x1, y1, x2, y2) >= 2 Then

cx = (x1 + x2) / 2

cy = (y1 + y2) / 2

linea(x1, y1, cx, cy)

linea(cx, cy, x2, y2)

End If

End Sub

Sub Main()

Dim x1, y1, x2, y2, x3, y3, x4, y4 As Integer


x1 = 20 : y1 = 20

x2 = 20 : y2 = 5

x3 = 5 : y3 = 5

x4 = 5 : y4 = 20

Console.ForegroundColor = 9

linea(x1, y1, x2, y2)

Console.ForegroundColor = 10

linea(x2, y2, x3, y3)

Console.ForegroundColor = 12

linea(x3, y3, x4, y4)

Console.ForegroundColor = 11

linea(x4, y4, x1, x1)

Console.ForegroundColor = 8

End Sub

End Module

IMAGEN:

EJERCICIO:

Graficar (Líneas, Barras y Texto) Valores por la Derecha con VISUAL BASIC

Se trata de una pequeña aplicación que implementa una forma de Graficar Valores
con Inicio por la Derecha.
CÓDIGO:

Public Class Form1

Dim MIARRAY As ArrayList 'PARA ALMACENAR LAS LECTURAS


Dim MIX As Integer 'ESPACIADO DE LAS LECTURAS EN EL GRAFICO
Dim MIRANDOM As Random 'SIMULAMOS LAS LECTURAS CON UN NUMERO ALEATORIO
Dim NUEVO As Integer 'VALORES MINIMO Y MAXIMO ESPERADOS
Dim TEXTO As Integer = 5 'ESPACIO PARA QUE SIEMPRE QUEPA EL TEXTO Y LA GRAFICA
NO SE PEGE A LA BASE DEL PICTUREBOX
Dim FUENTE As Font 'FUENTE UTILIZADA PARA EL TEXTO
Dim DIBUJO As Graphics 'DIBUJARA SOBRE EL PICTUREBOX
Dim TRAZO As Pen 'TRAZO DE LA GRAFICA DE LINEAS Y BARRAS
Dim VALOR As Integer 'LECTURA DE DATOS
Dim RECTANGULO As Rectangle 'RECTANGULO DE LA GRAFICA DE BARRAS

Private Sub ButtonINICIAR_Click(sender As System.Object, e As System.EventArgs)


Handles ButtonINICIAR.Click
Me.DoubleBuffered = True 'PARA TRATAR DE EVITAR EL PARPADEO

MIARRAY = New ArrayList 'INICIALIZACION DEL ARRAY

For I = 0 To 59 'LO RELLENAMOS DE 0s PARA QUE EMPIECE A MOSTRAR LOS DATOS


SIGNIFICATIVOS POR LA DERECHA
MIARRAY.Add(0)
Next

DIBUJO = PictureBox1.CreateGraphics 'DIBUJARA SOBRE EL PICTUREBOX


DIBUJO.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias 'PARA QUE SEAN
LINEAS SUAVES
TRAZO = New Pen(Brushes.Blue, 1) 'TRAZO DE LA GRAFICA DE LINEAS Y BARRAS
FUENTE = New Font("VERDANA", TEXTO, FontStyle.Bold) 'FUENTE UTILIZADA PARA
EL TEXTO
Timer1.Interval = 1000 'INICIAMOS LAS LECTURAS
Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles


Timer1.Tick

MIRANDOM = New Random 'SIMULAMOS LAS LECTURAS CON UN NUMERO ALEATORIO


NUEVO = MIRANDOM.Next(PictureBox1.Height / 3, PictureBox1.Height * 2 / 3)
'VALORES MINIMO Y MAXIMO ESPERADOS

LabelVALOR.Text = NUEVO 'PRESENTA EL VALOR

MIARRAY.Add(NUEVO) 'AÑADIMOS LA NUEVA LECTURA AL ARRAY

If MIARRAY.Count > 60 Then 'SI YA HAY 60 ELIMINA EL MAS ANTIGUO


MIARRAY.RemoveAt(0)
PictureBox1.Refresh() 'BORRA EL PICTUREBOX
DIBUJO.DrawLine(Pens.Gray, 0, CInt(PictureBox1.Height / 3),
PictureBox1.Width, CInt(PictureBox1.Height / 3)) 'ESCALA MAXIMO
DIBUJO.DrawLine(Pens.Gray, 0, CInt(PictureBox1.Height * 2 / 3),
PictureBox1.Width, CInt(PictureBox1.Height * 2 / 3)) 'ESCALA MINIMO
End If
MIX = 0 'PARA QUE EMPIECE A DIBUJAR A PARTIR DE X=0

For I = 0 To MIARRAY.Count - 2 'DIBUJA TODAS LAS LECTURAS QUE HAY EN EL


ARRAY
VALOR = MIARRAY(I) 'LECTURA DE DATOS

If CheckBoxBARRAS.Checked Then 'DIBUJA BARRAS


RECTANGULO = New Rectangle(MIX, PictureBox1.Height - VALOR - TEXTO,
3, VALOR) 'RECTANGULO DE LA GRAFICA DE BARRAS
DIBUJO.FillRectangle(Brushes.Blue, RECTANGULO)
End If
If CheckBoxLINEAS.Checked Then 'DIBUJA LINEAS
DIBUJO.DrawLine(TRAZO, MIX, PictureBox1.Height - VALOR - TEXTO, MIX
+ 10, PictureBox1.Height - MIARRAY(I + 1) - TEXTO)
End If
If CheckBoxTEXTO.Checked Then 'ESCRIBE EL VALOR
DIBUJO.DrawString(VALOR, FUENTE, Brushes.Red, MIX - TEXTO,
PictureBox1.Height - VALOR - TEXTO * 2)
End If

MIX += 10 'AVANZA 10 PIXELES(PROPORCIONALIDAD CON PICTUREBOX.WIDTH)


Next

End Sub
End Class

IMAGENES DE PRUEBA:
METODOS NÚMERICOS:
Gráficar la función en console Visual Basic.
CÓDIGO:
Module Module1
Function f(ByVal x As Single) As Single
Return (x - 1) * (x) * (x - 3)

End Function

Function dx(ByVal x As Single) As Single


Dim h As Single = 0.001
Dim deriv As Single = (f(x + h) - f(x)) / h
Return deriv
End Function

Function Trapecial(ByVal a As Single, ByVal b As Single, ByVal n As Integer) As


Single
Dim at As Single = 0, ap, dx, x, y1, y2 As Single
dx = (b - a) / n
For x = a To b - dx Step dx
y1 = f(x)
y2 = f(x + dx)
ap = (y1 + y2) / 2 * dx
at = at + ap
Next
Return at
End Function

Sub main()

Dim area As Single


Dim raiz1 As Single
Dim x, y As Single
Dim li = 0, ls As Single = 3
Dim dx As Single = 0.5
Console.WriteLine()
Console.WriteLine(" VALORES DE LA FUNCIÓN F= (X - 1)*(X + 0)*(X - 3)")
Console.WriteLine("{0,8} {1,8} {2,8}", "X", "y1", "raiz")
Console.WriteLine("==============================")
x = li
While (x <= ls)
y = f(x)
If y = 0 Then
raiz1 = x
Console.WriteLine("{0,8:F2} {1,8:f2} {2}", x, y, " ES RAIZ")
Else

Console.WriteLine("{0,8:F2} {1,8:f2} {2}", x, y, " NO RAIZ")


End If

x += dx
End While
area = Trapecial(0, 1, 100) + Math.Abs(Trapecial(1, 3, 100))
Console.WriteLine("==============================")
Console.WriteLine("")
Console.WriteLine("El Area sombreada es {0} ", area)

Dim cx As Single = 40
Dim cy As Single = 20
Dim ex As Single = 6
Dim ey As Single = -6
Console.SetCursorPosition(cx, cy)

For x = 1 To 70
Console.SetCursorPosition(CInt(x), cy)
Console.WriteLine("-")
Next
For y = 10 To 35
Console.SetCursorPosition(cx, CInt(y))
Console.WriteLine("|")
Next
For x = li To ls Step dx / 20
y = f(x)
Console.SetCursorPosition(CInt(cx + x * ex), CInt(cy + y * ey))
Console.WriteLine("*")
Next
Console.ReadLine()
End Sub
End Module

IMAGEN DE PRUEBA:
EJEMPLO 2:
CODIGO
Module Module1
Function f(ByVal x As Single) As Single
Return (x - 1) * (x - 3) * (x - 2)

End Function

Function dx(ByVal x As Single) As Single


Dim h As Single = 0.001
Dim deriv As Single = (f(x + h) - f(x)) / h REM también puede ser
f(x)-f(x-h)
Return deriv
End Function

Function Trapecial(ByVal a As Single, ByVal b As Single, ByVal n As Integer) As


Single
Dim at As Single = 0, ap, dx, x, y1, y2 As Single
dx = (b - a) / n
For x = a To b - dx Step dx
y1 = f(x)
y2 = f(x + dx)
ap = (y1 + y2) / 2 * dx
at = at + ap
Next
Return at
End Function

Sub main()

Dim area As Single


Dim raiz1 As Single
Dim x, y As Single
Dim li = 0.5, ls As Single = 3.4
Dim dx As Single = 0.25
Console.WriteLine()
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine(" VALORES DE LA FUNCIÓN F= (X - 1)(X - 3)(X - 2)")
Console.WriteLine("{0,8} {1,8} {2,8}", "X", "y1", "raiz")
Console.WriteLine("==============================")
x = li
While (x <= ls)
y = f(x)
If y = 0 Then
raiz1 = x
Console.ForegroundColor = 14
Console.WriteLine("{0,8:F2} {1,8:f2} {2}", x, y, " ES RAIZ")
Else
Console.ForegroundColor = 11
Console.WriteLine("{0,8:F2} {1,8:f2} {2}", x, y, " NO RAIZ")
End If

x += dx
End While
area = Math.Abs(Trapecial(1, 2, 100)) + Math.Abs(Trapecial(2, 3, 100))
Console.ForegroundColor = 10
Console.WriteLine("==============================")
Console.WriteLine("")
Console.WriteLine("El Area sombreada es {0} ", area)

Dim cx As Single = 40
Dim cy As Single = 20
Dim ex As Single = 12
Dim ey As Single = -12
Console.SetCursorPosition(cx, cy)
REM coordenadas
Console.ForegroundColor = CType(13, ConsoleColor)
For x = 1 To 70
Console.SetCursorPosition(CInt(x), cy)
Console.WriteLine("-")
Next
For y = 10 To 35
Console.SetCursorPosition(cx, CInt(y))
Console.WriteLine("|")
Next
Console.ForegroundColor = CType(10, ConsoleColor)
For x = li To ls Step dx / 20
y = f(x)
Console.SetCursorPosition(CInt(cx + x * ex), CInt(cy + y * ey))
Console.WriteLine("*")
Next
Console.ReadLine()
End Sub
End Module

IMAGEN:

You might also like