Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
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.
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).
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have access to a dataset that contains latitude and longitude pairs, but they are ill formatted and are not properly represented in the data set.
For example, a lat-long pair might look like this: 31333445, 105530865 when it should be 31.333445, -105.530865. Given the data set I am working with I know that the min value for latitude is 31.0 and the max is 37.0, and the min/max of longitude is -103 to -109.
If I was given a piece of paper and a pencil I could easily correct these myself, but the correction needs to happen on the fly when we receive input from a different program. We have no control over how the data is formatted until it hits our system and then we can make changes and corrections, and the lat-long pairs are all in a integer format listed above rather than a float.
What would be the best way to go about manually correcting this error? I am using PHP for our processing system.
If they're the same length then just divide by 1000000 and make negative where needed:
echo $lat / 1000000;
echo -$lon / 1000000;
If not then get the number of numbers at the start (2 and 3 here) making negative if needed, then insert a decimal and the remaining:
echo substr($lat, 0, 2) . '.' . substr($lat, 2);
echo -substr($lon, 0, 3) . '.' . substr($lon, 3);
You can use floatval() on the results if needed.
If the number of digits is always the same fixed size then use the solution suggested by #AbraCadaver in his comment ( just divide by 1000000 and multiply with -1)
If the number of digits can be different you need a different solution
and I have got a weird idea (at 0:24am)
I would convert the number to a string resulting in "31333445"
then concatenate "0." with the "31333445" resulting in "0.31333445"
then convert it back to a double resulting in 0.31333445
and then multiply it with 100 resulting in 31.333445 (and multiply the other value with -1 )
:-B
With this solution it does not matter if the number you get from outside has 3 or 14 digits
May sound weird but should work.
If this sounds to be a useful solution i will put into code tomorrow.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have to data frames with X, Y, and Z coordinates. I want to find the distance between all of the points in the two data frames. (Like the distance between entry A1 and every entry in B, A2 and every entry in B, and so on, and vice versa). I basically did this:
1.) Wrote a function that calculates the distance between two points.
2.) Used the distanceFinder function to create a function that finds the distance between one point in a group, and every other point in the opposite group.
3.) Created a function called bigDistance() that calls filter() on every entry in one group, and appends the results to an empty data frame through a for loop until it's completed.
This code takes about 2 minutes to run on the file I'm experimenting with, and I just found out that I have to translate this algorithm to PHP... so I guess this is kind of an optimization question, because I feel like PHP would be way slower at making these computations than R? Sorry if people find this "off-topic" but yeah, super new to programming and Big O notation and stuff, so any tips would be amazing! Thanks!
The dist function does exactly what you are looking for.
myDf <- data.frame(
x = rnorm(8),
y = rnorm(8),
z = rnorm(8)
)
dist(myDf)
# 1 2 3 4 5 6 7
# 2 3.0457054
# 3 1.7260658 3.2107845
# 4 1.2839101 3.4596211 2.9451175
# 5 1.5656231 4.0154389 2.3421445 2.3612348
# 6 1.9294650 1.6655718 1.7977887 2.8726174 2.5815296
# 7 2.1842743 3.5274692 3.8552701 1.0984651 2.9951244 3.3220919
# 8 1.4795857 3.5364663 0.5567753 2.7033371 1.9226225 2.0631788 3.6624082
It seems to be pretty fast as well (73ms on average)
library(microbenchmark)
mb <- microbenchmark(dist(myDf))
mb
# Unit: microseconds
# expr min lq mean median uq max neval
# dist(myDf) 70.436 71.453 77.4083 72.978 82.133 172.911 100
autoplot(mb)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a problem:
C. Clark's model of urban population densities indicating that population density varies with distance from the center of the city according to the equation Dx=D0e-bx where Dx is the population density at distance x from the center. D0 is the density at the center, e is the base of the natural logarithms, and b is a natural logarithm measuring the rate of change of density with distance.
How can I write this formula in php language?
Thanks!
What I have tried:
Dx = D/(x/b*b);
But I thinks this is wrong.
Not sure what's the problem here, so here's how I got it and you can say what did I get wrong. Afterwards, you can edit your question :)
Hence:
// this should be given
$d0 = 1.22;
$b = 0.37;
function dx($x = 0) {
global $d0, $b;
return $d0 /exp($b*$x);
}
echo dx(6.74); // d_{6.74}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need help I have the following 2 points on the map point A lat1/long1 and point B lat2/long2 in google map I have distance, arrived_time, bearingradians and speed.
With these data how can I get a estimated latitude longitude (google map format) to the next point on 10 minutes,20,30 and 40 minutes ?, having start and end point.
Point A
lat1=37.78472
lon1=-122.39913
Point B
lat2=37.78240
lon2=-121.23208
bearingradians=270
distance=102 KM
arrive_time=50 minutes
speed =122 KM/H
example:
http://hmarina.sytes.net/mapagoogle.jpg
What I need you to calculate the nexts points, I going to use PHP, or where should I start
Thank you
There are multiple ways to calculate this. Some of them are quite complex.
You could use Vincenty's formula, which is often used for bearing and distance calculations.
The formula needs Long/Lat of the starting point, the bearing and a distance.
I doubt, that you want to reimplement this algo, so here you go: Implementing Vincenty's Formula in PHP
Another solution could be to use vectors to calculate the destination points along a great-circle given distance and bearing from start point. This approach might be a bit easier, then to work with spherical trigonometry. http://www.movable-type.co.uk/scripts/latlong-vectors.html and https://stackoverflow.com/a/1739066/1163786
Another one is to calculate the intermediate points on a great-circle.
http://williams.best.vwh.net/avform.htm#Intermediate
Let's use Vincenty here and re-calc your end-point, given a starting-point, bearing and distance:
a starting point: Point A lat1=37.78472; lon1=-122.39913;
the bearing: approx. 89
the distance: 102 km
Result: Latitude: 37°47′42″N 37.79506902, Longitude: 121°14′28″W -121.24119021
That is pretty close your Point B.
Now, you want to determine the future position (lang/lat) by calculating the distance you will travel based on your current speed and your known time interval.
In other words, your next point is 10 minutes from the starting point given speed 122 km/h and 89 bearing.
Calculate new distance: 122 km/h = 2033.33 m/min, so in 10 minutes: 20333.33 m = 20,333 km approx.
You new data for the formula:
a starting point, here: Point A lat1=37.78472; lon1=-122.39913;
the bearing: approx. 89
the distance: 20,333 km
And re-run vincenty with these values to get Lat/Long...
This might be of help:
PHP library for geo/nav calculations
Google Maps API v3 provides Navigation Helper methods
You have: speed = 122Km/h => You can calculate 10 minute walked (n _km)
You can calculate distance with 2 point distance
sqrt((lat1 - lat2)^2 + (lng1-lng2)^2);
you have distance and lat1, how to calculate lat2:
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
suppose I have one array having the values:
array(20,40,30,15,60,50,10)
Now what i want is to I need to create bunch of 100 or near to hundred.and create separate round for each set of 100(or near to 100).
Say
Case 1:
Round 1: array(60,30,10) // 100 or near to 100
Round 2: array(40,50) // 100 or near to 100
Round 3: array(15,20) // 100 or near to 100 or remaining
Case 2:
Round 1: array(60,40) // 100 or near to 100
Round 2: array(50,20,30) // 100 or near to 100
Round 3: array(15,10) // 100 or near to 100 or remaining
So how can I achieve this?
Is there any algorithm regarding this that I can study?
You are describing the binpacking problem, which is NP-Complete, thus there is no known polynomial solution to solve it.
You can try an exponential approach (check all possibilities) if you need exact. If you want "close enough" you can search the literature for some approximation algorithm, or use some heuristic search solution from the AI field such as Genetic Algorithm or Hill Climbing.