Domanda

3rd grade question:

How do you calculate the distance between two points on a flat surface?

I have been going through the Google_Results and it seems everything i find applies to Long/Lat and not a flat surface.

I'm working on making ObjectA choose between ObjetsC,D,E... , select the closest one and move toward it. So I have to loop through my SQL table, pull out what's in range, and loop through the results to calculate distances.

Any help with this math I haven't had to remember in years would be appreciated.

È stato utile?

Soluzione

You will need to use a Euclidean distance formula (specifically for 2 dimensions). Basically the formula is:

d = sqrt((x1 - x2)^2 + (y1 - y2)^2)
  • d is your final distance
  • sqrt is the square root (the sqrt() function in PHP)
  • x1 and y1 are your x-y coordinates for your first point
  • x2 and y2 are your x-y coordinates for your second point
  • ^2 means raise to the second power (use the pow() function for PHP)

Pythagoras was the Greek philosopher who developed the Pythagorean Theorem, which can be used to derive the 2-dimensional distance formula (which is different from Euclid's derivation).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top