How do I get the distance between two locations? [SOLVED]

Discussion in 'Plugin Development' started by Ryo, Aug 27, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Ryo

    I'd assume this is quite the novice question here. But the thing is that I can easily get the distance between two 2-dimensional locations. But I'm not quite sure for what formula would be used to get the distance between two 3-dimensional locations (Since in this case you would be adding the Z values). Looking through the Bukkit javadocs I found this method distance(Location o) from org.bukkit.Location.distance(Location o) Basically it seems this what I need to achieve my 3-dimensional distance. But here is where I'm having some trouble, how exactly do I use this method? It only has one parameter so I'm a bit confused as of how this would be used.
    For anyone who has used it, could you post me a little code snippet showing me how it would be used?

    Thank you in advance,
     
  2. Offline

    DrBowe

    <firstLocation>.distance(<secondLocation>)
    Then it returns the generic distance between the two
     
    Vurtix, hammale and fromgate like this.
  3. Offline

    Ryo

    Wow didn't expect to get a reply that fast. Thank you.
     
  4. Offline

    nisovin

    For your learning pleasure, the regular distance formula can be extended to any number of dimensions. It isn't limited to just two. Here's the normal distance formula in two dimensions:

    distance = sqrt[ (x1-x2)^2 + (y1-y2)^2 ]

    Here's the distance formula in three dimensions:

    distance = sqrt[ (x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 ]

    See how awesome that is? You don't need to worry about this, since that is already built in, but it's good to know anyway.
     
    MarkehMe and Vurtix like this.
  5. Offline

    Ryo

    *facepalm* yes, I knew of the basic X and Y distance formula but now I feel dumb not knowing that you simply add the new dimension into the formula. Thank you for sharing.
     
Thread Status:
Not open for further replies.

Share This Page