I am trying to solve a bug which the distance between two points displays incorrectly.
When you hold right mouse button and circle around, sometimes the distance value displays as -1.#IND0000.
I've been guessing that this is a rounding problem, is there anyway to fix this?
Graphics 640,480,0
x = 250
y = 250
xspeed = 0
yspeed = 0
targetx = x
targety = y
sx = 0
sy = 0
velocity = 0
angle:Float = 0
Repeat
Cls
distance:Float = Sqr((x-targetx)*(x-targetx) - (y-targety)*(y-targety))
If MouseDown(2) 'Right Click
targetx = MouseX()
targety = MouseY()
dx = Abs(targetx-x)
dy = Abs(targety-y)
If x < targetx
sx = 1
Else
sx = -1
EndIf
If y < targety
sy = 1
Else
sy = -1
EndIf
err = dx-dy
ax = targetx-x
ay = targety-y
angle = ATan2(ax,ay) * -1 + 90
EndIf
If KeyDown(KEY_SPACE)
x = 250
y = 250
EndIf
If Not(x-targetx=0) Or Not(y-targety=0)
e2 = 2*err
If e2 >= (-1 * dy)
err :- dy
x :+ sx
EndIf
If e2 < dx
err :+ dx
y :+ sy
EndIf
EndIf
DrawRect(x,y,8,8)
DrawLine(x,y,targetx,targety)
'Draw 30 degree cone splash borders
DrawLine(x, y, x + 100 * Cos(angle-15), y + 100 * Sin(angle-15))
DrawLine(x, y, x + 100 * Cos(angle+15), y + 100 * Sin(angle+15))
'Draw middleline according to angle
'DrawLine(x, y, x + 100 * Cos(angle), y + 100 * Sin(angle))
DrawText("Angle: " + angle, 50, 50)
DrawText("Current Coord: " + x + " , " + y, 50, 80)
DrawText("Target Coord: " + targetx + " , " + targety, 50, 110)
DrawText("dx: " + dx + " dy: " + dy, 50, 140)
DrawText("distance: " + distance, 50, 170)
Flip
Until KeyDown(KEY_ESCAPE) Or AppTerminate()
End
|