02-14-2012, 01:58 PM
Code:
Function distance (x1 as Integer, y1 as Integer, x2 as Integer, y2 as Integer) as Integer
Dim x as Integer
x=Abs(x1-x2)
Dim y as Integer
y=Abs(y1-y2)
Return x+y
End Function
Dim bresx(0 to 255) as Integer
Dim bresy(0 to 191) as Integer
Function line(X1 as Integer,Y1 as Integer,X2 as Integer,Y2 as Integer) as Integer
'Draws a line of individual pixels 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
Dim dist as Integer
dist=line(1,1,100,95)-1
PRINT AT 0,0 ; PAPER 1 ; INK 0 ; "dist "+STR(dist)
for x=0 to dist
Plot bresx(x),bresy(x)
next
'PRINT AT 2,0 ; PAPER 1 ; INK 0 ; "bresx "+STR(bresx(108))
'PRINT AT 3,0 ; PAPER 1 ; INK 0 ; "bresx "+STR(bresx(57))
'PRINT AT 4,0 ; PAPER 1 ; INK 0 ; "bresx "+STR(bresx(18))
'PRINT AT 5,0 ; PAPER 1 ; INK 0 ; "bresy "+STR(bresy(108))
'PRINT AT 6,0 ; PAPER 1 ; INK 0 ; "bresy "+STR(bresy(59))
'PRINT AT 7,0 ; PAPER 1 ; INK 0 ; "bresy "+STR(bresy(1))
'main loop
100 LET j$ = INKEY$
GOTO 100
Im implementing a bresenham thingy for line of sight calculations,
whenever the length of the line exceeds 191 the screen gets corrupted,
191 just happens to be the height of the screen, but I dont see the relation.