' Programa escrito en FreeBasic, compatible con QBasic.
' Algoritmo de Bresenham. Este programa traza lineas en cualquier sentido.
' El valor inicial es de (0,0).
' Introduce valores para X entre 0 y 640, para Y entre 0 y 480.
' Si implementas este programa para otra cosa (NO en este programa),
' comprobaras que tambien puedes introducir numeros negativos.
Dim As Integer Incrd1, Incrd2, IncrXold, IncrXnew, IncrYold, IncrYnew
Dim As Integer DistX, DistY, NumPixel, Decision, Bucle
Dim As Integer Xnew, Ynew, Xold, Yold, x, y
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), 15
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