Page 1 of 2
Finding the distance between two points
Posted: Mon Oct 04, 2010 9:35 pm
by Lord Uber Dowzen
Hi, I was just wondering (to help in the learning process of Lua) if someone could look over this code I spent 10 minutes making to find the distance between two sets of x,y coordinates:
Code: Select all
function find_distance(xorigin, yorigin, xdestination, ydestination)
if xorigin > xdestination then
xdistance = xorigin - xdestination
elseif xorigin < xdestination then
xdistance = xdestination - xorigin
end
if yorigin > ydestination then
ydistance = yorigin - ydestination
elseif yorigin < ydestination then
ydistance = ydestination - yorigin
end
return distance = sqrt(xdistance ^ 2 + ydistance ^ 2)
end
I know it's terrible, but in which respects? Thanks
Re: Finding the distance between two points
Posted: Mon Oct 04, 2010 10:13 pm
by ivan
How about just:
Code: Select all
function distance ( x1, y1, x2, y2 )
local dx = x1 - x2
local dy = y1 - y2
return math.sqrt ( dx * dx + dy * dy )
end
Negative * a negative = positive so the first part of your code is not necessary.
Also, ^2 is considered slower to execute than x * x
Lastly, xdistance in your function is global - probably a better idea to make it a local variable.
Re: Finding the distance between two points
Posted: Mon Oct 04, 2010 10:30 pm
by Jasoco
This is what I use. One line.
Code: Select all
function distanceFrom(x1,y1,x2,y2) return math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2) end
Re: Finding the distance between two points
Posted: Tue Oct 05, 2010 3:04 am
by Lord Uber Dowzen
Ah, yes, those both make a lot more sense than mine. I think my problem might have been that I came up with the initial concept for the function while away from my computer and did all the test calculations in my head, then when I was at my computer spent 10 minutes typing it all out so that probably accounts for my solution being overly complicated and not working. Thanks for those.
Expect a lot more dumb questions over the coming weeks...
Re: Finding the distance between two points
Posted: Tue Oct 05, 2010 9:47 am
by zac352
Look how easy it is with vectors.
Re: Finding the distance between two points
Posted: Tue Oct 05, 2010 12:38 pm
by ivan
zac352 wrote:Look how easy it is with vectors.
How would that work? Lua doesn't have operator overloading although might be libs for it I think.
Re: Finding the distance between two points
Posted: Tue Oct 05, 2010 1:01 pm
by vrld
ivan wrote:How would that work?
With
metatables. You can define a
function that gets called whenever you try to access a table-field that is not there (for example "magnitude").
ivan wrote:Lua doesn't have operator overloading
Not entirely true. You can overload some
arithmetic (i.e. +, -, *, /) and
relational (i.e. <, <=, ==).
in zac352's example,
(p1-p2) invokes the
__sub-metamethod, which returns a table with the (self-defined) vector-metatable.
magnitude may be a field of the table, which gets set at invoking the
__sub metamethod or be an example of the
__index metamethod.
Metatables are a very powerful mechanism in Lua. If you are interested go ahead and read the
section about methamethods in the Programming in Lua book.
For an example of how to use them in a vector class, either search the forum for zac's vector class (which he posted multiple times), or have a look at mine at
github.
Re: Finding the distance between two points
Posted: Tue Oct 05, 2010 1:23 pm
by zac352
That's an awesome explanation.
But yeah, my vectors can be indexed several ways:
r,g,b,a,x,y,z,w,1,2,3,4,5,6,7...
magnitude,<insert lots of functions here>...
Re: Finding the distance between two points
Posted: Wed Oct 06, 2010 1:17 pm
by arquivista
Can't A-Star pathfinding Love's version help you?
http://love2d.org/forums/viewtopic.php?f=5&t=1797
Re: Finding the distance between two points
Posted: Wed Sep 25, 2013 10:14 am
by mawg
Sorry to sound dumb, back what units are these functions returning? Km? Miles? Nautical miles? Metres, or what?
And does anyoen know of a LUA version of Vincenty's formula? See
http://www.delphiforfun.org/Programs/Ma ... stance.htm