Visual Basic.NET - Fractal VonKoch

 
Vista:
sin imagen de perfil

Fractal VonKoch

Publicado por José (2 intervenciones) el 02/09/2025 12:08:30
Imports Microsoft.VisualBasic
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Math
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Width = 1200
Me.Height = 500
End Sub
Private Sub BtnKoch_Click(sender As Object, e As EventArgs) Handles BtnKoch.Click

Dim nivel As Integer
nivel = HScrollBarNivel.Value - 1
Dim p0 As New Point(100, 400)
Dim p4 As New Point(1100, 400)
VonKoch(p0, p4, nivel)

End Sub
Private Sub VonKoch(p0, p4, nivel)

Dim i As Integer
Dim ltotal As Single
Dim l As Single
Dim angulo As Single
ltotal = Sqrt((p4.Y - p0.Y) * (p4.Y - p0.Y) + (p4.X - p0.X) * (p4.X - p0.X))
l = ltotal / 3
angulo = Atan((p4.Y - p0.Y) / (p4.X - p0.X))
Dim p1 As New Point(p0.X + ltotal / 3, p0.Y + (p4.Y - p0.Y) / 3)
Dim p2 As New Point(p1.X + l * Cos(angulo + PI / 3), p1.Y - l * Sin(angulo + PI / 3))
Dim p3 As New Point(p0.X + 2 * ltotal / 3, p0.Y + 2 * (p4.Y - p0.Y) / 3)

Dim p(4) As Point
p(0) = p0
p(1) = p1
p(2) = p2
p(3) = p3
p(4) = p4
Dim mypen As Pen
mypen = New Pen(Drawing.Color.Blue, 2)
Dim mygraphics As Graphics = Me.CreateGraphics

Dim mispuntos As Point() = {p0, p1, p2, p3, p4}

For i = 0 To 3
If nivel <= 1 Then
mygraphics.DrawLine(mypen, p0, p4)
Else
VonKoch(p(i), p(i + 1), nivel - 1)

End If
Next
End Sub
Private Sub HScrollBarNivel_Scroll(sender As Object, e As ScrollEventArgs) Handles HScrollBarNivel.Scroll
Dim nivel As Integer
nivel = HScrollBarNivel.Value - 1
LabelNivel.Text = "Nivel = " & Str(nivel)
Label1.Text = Str(nivel)
End Sub
Private Sub BtnFin_Click(sender As Object, e As EventArgs) Handles BtnFin.Click
End
End Sub
End Class
Nivel-2
Nivel-3
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Fractal VonKoch

Publicado por José (2 intervenciones) el 04/09/2025 10:52:37
He realizado algunos cambios en el código y en los niveles 2 y 3 funciona correctamente, en el nivel 4 hay dos segmentos que no. No entiendo cual es el problema.
Adjunto el nuevo código e imágenes. En el nivel 4 debería salir cómo está en rojo.

Imports Microsoft.VisualBasic
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Math
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Width = 1200
Me.Height = 500
End Sub
Private Sub BtnKoch_Click(sender As Object, e As EventArgs) Handles BtnKoch.Click

Dim nivel As Integer
nivel = HScrollBarNivel.Value - 1
Dim p0 As New Point(100, 400)
Dim p4 As New Point(1100, 400)
VonKoch(p0, p4, nivel)

End Sub
Private Sub VonKoch(p0, p4, nivel)

Dim i As Integer
Dim dx = p4.x - p0.x
Dim dy = p4.y - p0.y
Dim ltotal As Single
Dim l As Single
Dim angulo As Single
'ltotal = Sqrt((p4.Y - p0.Y) * (p4.Y - p0.Y) + (p4.X - p0.X) * (p4.X - p0.X))
ltotal = Sqrt(dx * dx + dy * dy)
l = ltotal / 3
angulo = Atan((p4.Y - p0.Y) / (p4.X - p0.X))

Dim p1 As New Point(p0.x + dx / 3, p0.y + dy / 3)
Dim p2 As New Point(p1.X + Cos(angulo - PI / 3) * l, p1.Y + Sin(angulo - PI / 3) * l)
Dim p3 As New Point(p4.x - dx / 3, p4.y - dy / 3)
Dim p(4) As Point

p(0) = p0
p(1) = p1
p(2) = p2
p(3) = p3
p(4) = p4
Dim mypen As Pen
mypen = New Pen(Drawing.Color.Blue, 2)
Dim mygraphics As Graphics = Me.CreateGraphics

Dim mispuntos As Point() = {p0, p1, p2, p3, p4}

For i = 0 To 3
If nivel <= 1 Then
mygraphics.DrawLine(mypen, p0, p4)
Else
VonKoch(p(i), p(i + 1), nivel - 1)

End If
Next
End Sub
Private Sub HScrollBarNivel_Scroll(sender As Object, e As ScrollEventArgs) Handles HScrollBarNivel.Scroll
Dim nivel As Integer
nivel = HScrollBarNivel.Value - 1
LabelNivel.Text = "Nivel = " & Str(nivel)
Label1.Text = Str(nivel)
End Sub
Private Sub BtnFin_Click(sender As Object, e As EventArgs) Handles BtnFin.Click
End
End Sub
End Class
Nivel-2
Nivel-3
Nivel-4
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar