• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 191 plots causes crash slenkar Senior Member    Posts: 282 Threads: 48 Joined: Feb 2011 Reputation: 0 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 X1X2         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. boriel Administrator       Posts: 1,715 Threads: 54 Joined: Aug 2019 Reputation: 16 02-14-2012, 02:31 PM I will try to debug. Also, you might want to have a look at DRAW asm routine, which already uses Bresenham's algorithm. Maybe you have some overflow which leads to a stack or ROM variables (e.g. 23659) to be accidentally overwritten? slenkar Senior Member    Posts: 282 Threads: 48 Joined: Feb 2011 Reputation: 0 02-14-2012, 03:01 PM It was my fault, it was an array over-run :oops: here is a distance function to make up for it: 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 Sqr((x^2)+(y^2)) End Function``` this bresenham line drawing algorithm lets you do line-of-sight to see if 2 units can see each other in a game « Next Oldest | Next Newest »

Forum Jump:

Users browsing this thread: 1 Guest(s) 