Necesito dibujar una recta en un picturebox por el algoritmo de Bresenham de la recta, pero no se como solucionar la definición de las coordenadas para que lo dibuje bien ...
Código ASP:
Ver original
Dim lx(1), ly(1) As Integer 'Coordenadas de la linea linea
en el mouseDown
Código ASP:
Ver original
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown If CheckBox2.Checked Then 'Linea, guarda primer punto de la linea If Not clicked Then TextBox3.Text = ejex.ToString TextBox4.Text = ejey.ToString lx(0) = e.X ly(0) = e.Y clicked = True End If End If End Sub
y en el mouseUp
Código ASP:
Ver original
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp Dim penMouseUP As Pen = Pens.Blue If CheckBox2.Checked Then 'Linea, guarda primer punto de la linea TextBox5.Text = ejex.ToString TextBox6.Text = ejey.ToString lx(1) = e.X ly(1) = e.Y Bresenham(lx(0), lx(1), ly(0), ly(1), New PlotFunction(AddressOf plot)) PictureBox1.CreateGraphics.DrawLine(penMouseUP, lx(0), ly(0), lx(1), ly(1)) clicked = False End If End Sub
Algoritmo de bresenham...
Código ASP:
Ver original
Sub Bresenham(ByVal x1 As Long, ByVal y1 As Long, ByVal x2 As Long, ByVal y2 As Long, ByVal plot As PlotFunction) Dim steep As Boolean = (Math.Abs(y2 - y1) > Math.Abs(x2 - x1)) If (steep) Then Swap(x1, y1) Swap(x2, y2) End If If (x1 > x2) Then Swap(x1, x2) Swap(y1, y2) End If Dim deltaX As Long = x2 - x1 Dim deltaY As Long = y2 - y1 Dim err As Long = deltaX / 2 Dim ystep As Long Dim y As Long = y1 If (y1 < y2) Then ystep = 1 Else ystep = -1 End If For x As Long = x1 To x2 Dim result As Boolean If (steep) Then result = plot(y, x) Else result = plot(x, y) If (Not result) Then Exit Sub err = err - deltaY If (err < 0) Then y = y + ystep err = err + deltaX End If Next End Sub Function plot(ByVal x As Long, ByVal y As Long) As Boolean pic.CreateGraphics.FillEllipse(Brushes.Green, x, y, 2, 2) Return True 'This just prints each co-ord End Function
Si alguien puede salvarme de meterle ( de forma oculta ) un
Código ASP:
Ver original
PictureBox1.CreateGraphics.DrawLine(penMouseUP, lx(0), ly(0), lx(1), ly(1))
Que este si me lo hace bien ......................
Saludos y gracias