Robotica

Bresenham 2D


Algoritmo de Bresenham clásico. El programa traza líneas en cualquier sentido.

El valor inicial es de (0,0).

Introduce valores para X entre 0 y 640; para Y entre 0 y 480.

Todos los algoritmos de Bresenham, de cualquier dimensión que publico aquí, también funciona con números enteros negativos.

Programa escrito en FreeBasic, compatible con QBasic.

Bájate el programa cliqueando aquí.

 




Dim    As Integer   x,  y, Incrd1, Incrd2, IncrXold, IncrXnew, IncrYold, IncrYnew 
Dim    As Integer   DistX, DistY, NumPixel, Decision, Bucle
Dim    As Integer   Xnew, Ynew, Xold, Yold


Screen 12 ' Pantalla Grafica
  
Xold=0
Yold=0
  
While (1)
 Input "Eje X:", Xnew
 Input "Eje Y:", Ynew

 DistX = Abs(Xnew - Xold)

 DistY = Abs(Ynew - Yold)
  
 If DistX >= DistY Then
    NumPixel = DistX + 1
    Decision = (2 * DistY) - DistX
    Incrd1 = DistY * 2
    Incrd2 = (DistY - DistX) * 2
    IncrXold = 1
    IncrXnew = 1
    IncrYold = 0
    IncrYnew = 1
 Else
    NumPixel = DistY + 1
    Decision = (2 * DistX) - DistY
    Incrd1 = DistX * 2
    Incrd2 = (DistX - DistY) * 2
    IncrXold = 0
    IncrXnew = 1
    IncrYold = 1
    IncrYnew = 1
 End If
  
 If Xold > Xnew Then
    IncrXold = -IncrXold
    IncrXnew = -IncrXnew
 End If
  
 If Yold > Ynew Then
    IncrYold = -IncrYold
    IncrYnew = -IncrYnew
 End If
  
 x = Xold
 y = Yold
  
 For Bucle = 1 To NumPixel
 
       PSet (x,y), 1
      If Decision < 0 Then
        Decision = Decision + Incrd1
        x = x + IncrXold
        y = y + IncrYold
     Else
        Decision = Decision + Incrd2
        x = x + IncrXnew
        y = y + IncrYnew
     End If
   
 Next Bucle
  
 Xold = Xnew
 Yold = Ynew 

Wend

End