Determine length of line between two points
BlitzMax Forums/BlitzMax Beginners Area/Determine length of line between two points
| ||
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 :) |
| ||
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 |
| ||
Thanks you kindly sswift :) |
| ||
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 |
| ||
don't use ^2 as it is unbelievable slow. Instead useSqr( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ) |
| ||
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. |
| ||
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) |
| ||
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. |
| ||
On my machine a couple of ms to use sqr for 80000 calls per frame. |
| ||
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) |
| ||
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 don't get a runtime error. And Dist should be Dist# btw. |
| ||
runtime error is my sig (I'm changing it as it is quite annoying) integer for simplicity |