Determine length of line between two points

BlitzMax Forums/BlitzMax Beginners Area/Determine length of line between two points

kronholm(Posted 2006) [#1]
I wish to determine the length of a line drawn between two points.

Is there a simpler method of doing this than sqrt((x2-x1)^2 + (y2-y1)^2) ? Also if someone knows of a math/trig cheat sheet that would be good for programming, for instance movement and collissions, I'd be very happy :)


sswift(Posted 2006) [#2]
That is the mathematically correct way to do it.

At times you can avoid the square root if you are comparing distances, and there are also equations which approximate distance but are less accurate at certain angles...

But to compute the actual real distance, that's the simplest it gets.

Of course you can wrap that in a function so you don't have to type it out every time:

Function Dist(X1#, Y1#, X2#, Y2#)
Return sqrt((x2-x1)^2 + (y2-y1)^2)
End Function


kronholm(Posted 2006) [#3]
Thanks you kindly sswift :)


Chris C(Posted 2006) [#4]
you may also be interested to know it also works in 3d
(I discovered this for myself about 20 years ago and thought I was so clever - ah bless, youth...)

d.x=p1.x-p2.x
d.y=p1.y-p2.y
d.z=p1.z-p2.z

d=sqr((d.x*d.x)+(d.y*d.y)+(d.z*d.z))

this site is worth looking at http://www.euclideanspace.com/maths/index.htm


Oddball(Posted 2006) [#5]
don't use ^2 as it is unbelievable slow. Instead use

Sqr( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) )



Dreamora(Posted 2006) [#6]
At best don't take any distance at all (if you know they aren't that large) but use squared distances for test. SQR is the second slowest function present (only logarithm is slower) and should be avoided if not needed.


tonyg(Posted 2006) [#7]
Bmax is VERY quick at sqr so you'd need to do a LOT to make more than a couple of millisecs difference.
^ is a bit different but still would need quite a few calculations to make a difference.
I might have done these wrong though...
Function multiply(x,y)
  Return (x*x)+(y*y)
End Function
Function square(x,y)
  Return (x^2)+(y^2)
End Function
Function sqrroot(x#,y#)
  Return Sqr(x*x)+(y*y)
End Function
tim1=MilliSecs()
For x = 1 To 8000
  multiply(x,2)
Next
tim2=MilliSecs()
For x = 1 To 8000
  square(x,2)
Next
tim3=MilliSecs()
For x = 1 To 8000
  sqrroot(x,2)
Next
tim4=MilliSecs()
Print (tim2-tim1)
Print (tim3-tim2)
Print (tim4-tim3)



Dreamora(Posted 2006) [#8]
A couple of millisecs with a timeframe of 16ms for all calculation and drawing per frame is a huge difference.

For that reason I have a vector class I use normally, that calculates its length on creation or modification. That way it won't need to recalculate the sqr unless it was modified.


tonyg(Posted 2006) [#9]
On my machine a couple of ms to use sqr for 80000 calls per frame.


Dreamora(Posted 2006) [#10]
So that would be ~400 objects measuring the distances to each other ...
sounds like enough for freak usage :)

But still ... squared distance is enough for 2D games as the distances can't become that large that they would be a problem (if they are out of display, they aren't disabled)


tonyg(Posted 2006) [#11]
It's no big deal I was just responding to

SQR is the second slowest function present (only logarithm is slower) and should be avoided if not needed.

. I agree if you don't need sqr then don't use it but it's not slow and there are ways to reduce the number of checks required.


bradford6(Posted 2006) [#12]



sswift(Posted 2006) [#13]
I don't get a runtime error. And Dist should be Dist# btw.


bradford6(Posted 2006) [#14]
runtime error is my sig (I'm changing it as it is quite annoying)

integer for simplicity