Finding the distance between two points

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Lord Uber Dowzen
Prole
Posts: 18
Joined: Wed Sep 29, 2010 3:02 am

Finding the distance between two points

Post 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
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Finding the distance between two points

Post 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.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Finding the distance between two points

Post 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
Lord Uber Dowzen
Prole
Posts: 18
Joined: Wed Sep 29, 2010 3:02 am

Re: Finding the distance between two points

Post 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...
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Finding the distance between two points

Post by zac352 »

Code: Select all

(p1-p2).magnitude
Look how easy it is with vectors. :crazy:
Hello, I am not dead.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Finding the distance between two points

Post by ivan »

zac352 wrote:

Code: Select all

(p1-p2).magnitude
Look how easy it is with vectors. :crazy:
How would that work? Lua doesn't have operator overloading although might be libs for it I think.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Finding the distance between two points

Post 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.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Finding the distance between two points

Post by zac352 »

That's an awesome explanation. :o
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>...
Hello, I am not dead.
User avatar
arquivista
No longer with us
Posts: 266
Joined: Tue Jul 06, 2010 8:39 am
Location: Insert Geolocation tag here
Contact:

Re: Finding the distance between two points

Post by arquivista »

Can't A-Star pathfinding Love's version help you?
http://love2d.org/forums/viewtopic.php?f=5&t=1797
--------------------------------------------------------
To Do: Insert Signature Here
--------------------------------------------------------
mawg
Prole
Posts: 1
Joined: Wed Sep 25, 2013 10:05 am

Re: Finding the distance between two points

Post 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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 72 guests