### Line of sight

distance:

line:

- Code:
`Function line(X1 as Integer,Y1 as Integer,X2 as Integer,Y2 as Integer) as Integer`

'creates a line of individual co-ords from X1,Y1 to X2,Y2 at any angle

Dim dist as Integer

dist=distance(X1,Y1,X2,Y2)

dim bresiter as Integer

For bresiter=0 To dist

bresx(bresiter)=0

bresy(bresiter)=0

Next

Dim steep as Integer

if Abs(Y2-Y1) > Abs(X2-X1) then

steep=1

else

steep=0

end if

PRINT AT 1,0 ; PAPER 1 ; INK 0 ; "steep "+STR(steep)

If steep =1 Then

Dim Temp as Integer

Temp=X1

X1=Y1

Y1=Temp

Temp=X2

X2=Y2

Y2=Temp

End If

Dim DeltaX as Integer

DeltaX=Abs(X2-X1)

Dim DeltaY as Integer

DeltaY=Abs(Y2-Y1)

Dim IError as Integer

IError=0

Dim DeltaError as Integer

DeltaError=DeltaY

Dim x as Integer

x=X1 'Start at X1,Y1

Dim y as Integer

y=Y1

Dim XStep as Integer

Dim YStep as Integer

If X1<X2 Then

XStep=1

Else

XStep=-1

end if

If Y1<Y2 Then

YStep=1

Else

YStep=-1 'Direction

end if

Dim iter as Integer

iter=1

While x<>X2

x=x+XStep 'Move in X

IError=IError+DeltaError 'Add to counter

If (IError SHL 1)>DeltaX then 'Would it overflow?

y=y+YStep 'Move in Y

IError=IError-DeltaX 'Overflow/wrap the counter

End if

If steep then

bresx(iter)=y

bresy(iter)=x

Else

bresx(iter)=x

bresy(iter)=y

End If

iter=iter+1

If iter>dist then

'Print at 3,0;PAPER 1;INK 0;"bresenham over distance "+STR(dist)

Return dist

End If

Wend

Return dist

End Function

create 2 arrays to hold the x and y coordinates of the line

The size of the arrays should be the distance of the longest line you intend to create

a line going across the screen of the speccy is about 322 in length

You have 2 characters on a playing field that has x and y coordinates

you call the line function:

one character is at 1,1

the other is at 100,95

then finally you go through the co-ordinates on the line to check if anything is in the way, like walls or other characters