Calculate distance

BlitzMax Forums/BlitzMax Beginners Area/Calculate distance

Hello,
I need to detect my mouse pointer(mx,my) is nearby a line let's say from (x1,y1) to (x2,y2)
I suppose it can be done like this:
a) Given mx (x1<mx<x2) as mouse coordinate calculate line's y
b) if abs(y-my) < distance then mouse is nearby line
Is there any better way?

Thanks!

Graphics 640,480,0,0

Type TPoint
	Field X:Float
	Field Y:Float
End Type

Type TLine
	Field startPoint:TPoint
	Field endPoint:TPoint
	Field thickness:Float = 10.5
End Type

Global line:TLine = New TLine
	line.startPoint = New TPoint
		line.startPoint.x = 10
		line.startPoint.y = 10

	line.endPoint = New TPoint
		line.endPoint.x = 200
		line.endPoint.y = 300



While Not KeyDown(KEY_ESCAPE)
	Cls
	
	If IsOnLine(MouseX(), MouseY(), line.startPoint.x, line.startPoint.y, line.endPoint.x, line.endPoint.y, line.thickness) Then
		SetColor(255,0,0)
	Else
		SetColor(255,255,255)
	EndIf
	
	DrawLine(line.startPoint.x, line.startPoint.y, line.endPoint.x, line.endPoint.y)
	
		
	
	Flip
Wend

End


Function IsOnLine(px:Float,py:Float,ax:Float,ay:Float,bx:Float,by:Float,radius:Float=1.5)

	Local vx:Float,vy:Float,wx:Float,wy:Float,c1:Float,c2:Float,bb:Float,dd:Float

	vx:Float = bx - ax
	vy:Float = by - ay
	wx:Float = px - ax
	wy:Float = py - ay

	c1:Float = wx*vx + wy*vy
	If  c1 <= 0 
		If ((px-ax)^2+(py-ay)^2)=>(radius^2) Then Return False
	End If
	c2:Float = vx*vx + vy*vy
	If c2<=c1 
		If ((px-bx)^2+(py-by)^2)=>(radius^2) Then Return False
	End If
	bb:Float = c1 / c2

	If Sqr((ax + (bb*vx)-px)^2 + (ay + (bb*vy)-py)^2)<radius Then Return True
	
End Function



Thanks to Fredborg for the IsOnLine function (direct conversion from B3D)

yowzer!

Thanks for the code. Thats what I was looking for! I will use this code in BMax with bbLine (SDK)
Grey: Can you please what "yowzer" means. Its a great moment to learn English idioms :)

Yowzer is similar to wow! Nothing special. :)

yowzer!
Thanks TaskMaster

Yeah a bit like Cor Blimey! Althought that's a bit old now.

Yeah a bit like Cor Blimey! Althought that's a bit old now.


And limited to people like Chaz & Dave and Dick Van Dyk :)

i read some where that the parabolic distance is way faster to calculate then the traditional manhatan distance.. gone see if i find that article again

//Gnasher