How to pass float in pygame.draw.circle() - pygame

I am trying to code Fourier Series on PyGame. I am having a problem that I can't pass float values in the function:
circle_x = radius * math.cos(angle)
circle_y = radius * math.sin(angle)
pygame.draw.circle(screen, (0, 255, 255), (circle_x, circle_y), 8, 0)

There are no floating-point pixel co-ordinates, they're all integers from 0 to window_width-1. So there is no reason for pygame.draw.circle() to take floating-point arguments. Even if you wrote your own circle-drawing function, it just moves the need to use integers from the circle-level to the pixel-level.
Probably you can just use the round( float_value, decimal_places=0 ) function, which will round to an integer if the decimal places parameter is 0 (the default)
circle_x = round( radius * math.cos(angle) )
circle_y = round( radius * math.sin(angle) )
pygame.draw.circle(screen, (0, 255, 255), (circle_x, circle_y), 8, 0)

Related

MySQL query to return closest place comparing to csv stored coordinates along with the distance in meters

I have these two tables:
Table locations
coords
28.08982880911016,-31.649963296195086
Table places
lat
lon
place
28.08982880911016
-31.649963296195086
Test
I want a MySQL (version 5.7.41 compatible) query which can return all records in table 1; however, I do not want to display the coords value as they stored; instead, I want to use ST_Distance_Sphere or Haversine formula to check each coords with the nearest place respecting these cases:
If the nearest value is less than 300 meter it returns that corresponding place value
If the nearest value is between 300 and 1500 meters it returns: "Near " + the value of the corresponding place + "~" + the nearest value in meters + "m"
If the nearest value is more than 1500 it returns "N/A" 3.
Please note that I have around a million records in the table locations, so I need the simplest query with minimal performance impact.
Thanks in advance.
Performing the distance calculation between all locations and all places would be very expensive so we will create a bounding box, which can use available indices to get a subset of places on which to perform the distance calculations.
We are building a bounding box slightly bigger than the 1500m radius we are interested in (to allow for inaccuracy):
dist in degrees latitude = (dist / rEarth) * (180 / pi)
dist in degrees longitude = (dist / rEarth) * (180 / pi) / cos(latitude * pi/180)
dist = 1.550 km
rEarth ≈ 6371 km
dist in degrees latitude = (dist / rEarth) * (180 / pi)
dist in degrees latitude = (1.55 / 6371) * (180 / pi)
dist in degrees latitude = DEGREES(1.55/6371)
dist in degrees latitude = 0.013939
dist in degrees longitude = (dist / rEarth) * (180 / pi) / cos(latitude * pi/180)
dist in degrees longitude = 0.013939 / COS(RADIANS(latitude))
Start the join with a 3.100 km x 3.100 km bounding box centered on the location.coords
Then do point to point distance calc for the entities found:
SELECT *,
/*
* We are pre-splitting the coords here so we don't have to do
* it repeatedly inside the correlated sub-query
*/
SUBSTRING_INDEX(l.coords, ',', 1) AS loc_lat,
SUBSTRING_INDEX(l.coords, ',', -1) AS loc_lon,
(
SELECT CONCAT(
ROUND(ST_Distance_Sphere(
ST_GeomFromText(CONCAT('POINT(', REPLACE(l.coords, ',', ' '),')')),
ST_GeomFromText(CONCAT('POINT(', lat, ' ', lon,')'))
), -1),
'::',
place
) dist
FROM places
WHERE lat BETWEEN loc_lat - 0.013939 AND loc_lat + 0.013939
AND lon BETWEEN loc_lon - (0.013939 / COS(RADIANS(loc_lat))) AND loc_lon + (0.013939 / COS(RADIANS(loc_lat)))
HAVING dist <= 1500
ORDER BY dist ASC
LIMIT 1
) AS place,
(
SELECT
CASE
WHEN SUBSTRING_INDEX(place, '::', 1) < 300 THEN SUBSTRING_INDEX(place, '::', -1)
WHEN SUBSTRING_INDEX(place, '::', 1) BETWEEN 300 AND 1500 THEN CONCAT('Near ', SUBSTRING_INDEX(place, '::', -1), '~', SUBSTRING_INDEX(place, '::', 1))
ELSE 'N/A'
END
) AS place_text
FROM locations l;
This query will work but how it will perform is another matter entirely. Ideally, you should have two composite indices on places: (lat, lon) and (lon, lat) but having at least one of these is critical, otherwise you will be constantly table scanning.

Normalization of histogram in Octave by hist(y,x,norm)?

I was trying to normalize the histogram of uniformly distributed random numbers in the interval [0,10]. In octave documentation I came across the function as hist(y, x, norm) which I applied and got the histogram normalized in the interval. The code that I used is
m=input('Number of random numbers required = ')
v=10*rand(1,m)
hist(v,10,1,"facecolor", "b", "edgecolor", "w",'linewidth',1.5);
title(['Normalized histogram for ' num2str(m) ' uniformly distributed random numbers'])
set(gca,'fontsize',30)
but as I changed the bin number to 50 then for getting the normalized histogram I had to change the third argument of hist() to 5.
Here is the code
m=input('Number of random numbers required = ')
v=10*rand(1,m)
hist(v,50,5,"facecolor", "b", "edgecolor", "w",'linewidth',1.5);
title(['Normalized histogram for ' num2str(m) ' uniformly distributed random numbers'])
set(gca,'fontsize',30)
then only it produced a normalized histogram as the previous one. What's actually happening here? Why I need to change the norm argument to 5 when I changed the bin number to 50?
When I tried to normalize the gaussian distribution using same method I got it wrong ?( I had to do it write all the algorithm to get the correct answer) so I would like to know how the norm argument works ?
here is the code that i tried for gaussian distribution which yielded the wrong result
m=input('Number of random numbers required = ');
v=randn(1,m)
[f,x]=hist(v,50);
hold on;
g = 1 / sqrt(2 * pi) * exp(-0.5 * x .^ 2);
plot(x, g, 'r','linewidth',1.5);
hist(v,50,5,"facecolor", "b", "edgecolor", "w",'linewidth',1.5);
title(['Normalized histogram for ' num2str(m) ' gaussian distributed random numbers'])
set(gca,'fontsize',30)
The reason is because you are trying to compare a frequency histogram with a frequency DENSITY function. Which means you have not taken the effect of the bin width into account.
In other words, if we consider each value in v, which represents histogram height, to be the product of the bin width w and a 'density' value h such that v = h * w at each element, then your normalisation says that sum(v) = 1, and therefore sum(h * w) = w * sum(h) = 1.
Therefore in order to obtain a valid 'density' function from your current values, you need to divide your current values in v by the (constant) bin width w, to obtain the density value h.
Compare with this code:
m = 5000;
v = randn( m, 1 );
g = 1 / sqrt(2 * pi) * exp( -0.5 * x .^ 2 );
[nn,xx] = hist( v, 50, 1, 'facecolor', 'b', 'edgecolor', 'w', 'linewidth', 1.5);
nn = nn / diff(xx)(1);
bar( xx, nn, 0.5, 'facecolor', [0, 0.5, 1], 'edgecolor', [0, 0.25, 0.5] );
hold on;
plot(x, g, 'r','linewidth', 3);
title({ ['Normalized histogram for ', num2str(m)], ' gaussian distributed random numbers'})
set(gca,'fontsize',20)
hold off

Octave value replacement with logical indexing on multidimensional matrices

I have a matrix named IMG, it is a n * m * 3 shaped matrix (an hsv image). What I am trying to achieve is
IF IMG(x, y, 1) < 1/2
THEN IMG(X, Y, 2) = 0.
Logical indexing looks like a solution but with that way we can only access the condition index (IMG(x, y, 1)). With the code below I am changing first indices of the pixels but I want to change second one.
IMG( IMG(:, :, 1) > 1/2 ) = 0;
Thanks for your help.
One simple solution is to extract the whole plane, modify it, then put it back:
s = IMG(:, :, 2);
s(IMG(:, :, 1) > 1/2) = 0;
IMG(:, :, 2) = s;
It is also possible to play around with linear indices, which is more generic, but also more complex:
index = find(IMG(:, :, 1) > 1/2);
offset = size(IMG, 1) * size(IMG, 2);
IMG(index + offset) = 0;
You can multiply the image by a mask:
IMG(:, :, 2) = IMG(:, :, 2) .* (IMG(:, :, 1) <= (1/2)) ;
Or use compound assignment:
IMG(:, :, 2) .*= IMG(:, :, 1) <= (1/2);
Another fast option is reshaping the array:
sz =size(IMG) ;
IMG = reshape(IMG, [], 3);
IMG(IMG(:,1)>1/2, 1), 2) = 0;
IMG = reshape(IMG, sz) ;
Other, possibly less efficient, option is using ifelse :
IMG(:, :, 2) = ifelse(IMG(:, :, 1) > 1/2, 0, IMG(:, :, 2) ) ;
looking for another one-line solution without any intermediate holding variable, the following was proposed for a multi-dimensional array on the Octave Help list:
a( (a(:,:,3)<.5) & shiftdim(1:size(a,3)==2,-1) ) = 0
for example:
>> a = rand(2,3,3)
a =
ans(:,:,1) =
0.63416 0.28912 0.33463
0.76642 0.51474 0.28130
ans(:,:,2) =
0.99748 0.26000 0.45671
0.73153 0.44499 0.24099
ans(:,:,3) =
0.94726 0.77252 0.12698
0.27069 0.46458 0.55833
>> a( (a(:,:,3)<.5) & shiftdim(1:size(a,3)==2,-1) ) = 0
a =
ans(:,:,1) =
0.63416 0.28912 0.33463
0.76642 0.51474 0.28130
ans(:,:,2) =
0.99748 0.26000 0.00000
0.00000 0.00000 0.24099
ans(:,:,3) =
0.94726 0.77252 0.12698
0.27069 0.46458 0.55833

convex square - matlab

Given an interactive input of 4 points marked clockwise with the mouse pointer, I need to check using Matlab whether the shape that was drawn is a quadrilateral convex or not. I saw some people that suggested a gift wrapping algorithm. My thought was just using tan, such that if I have an angle greater than 180 degrees, the shape is not a convex.
Can you suggest a better way to do it? I'd appreciate your reference to following code:
showImage(imageA)
hold on
% Initially, the list of points is empty.
xy = [];
n = 0;
% Loop, picking up the points.
disp('Please enter corners of place to insert image in clockwise order.')
for i = 1:4
[xi,yi] = ginput(1);
plot(xi,yi,'yo')
xy(:,i) = [xi;yi];
end
%check if this is a convex quadrillateral
a1 = ( xy(2,2) - xy(2,1) ) / ( xy(1,2) - xy(1,1) );
a2 = ( xy(2,3) - xy(2,2) ) / ( xy(1,3) - xy(1,2) );
a3 = ( xy(2,4) - xy(2,3) ) / ( xy(1,4) - xy(1,3) );
a4 = ( xy(2,1) - xy(2,4) ) / ( xy(1,1) - xy(1,4) );
tan1 = abs( atand( (a2-a1) /( 1+a1*a2) ) );
tan2 = abs( atand( (a3-a2) / (1+a3*a2) ) );
tan3 = abs( atand( (a4-a3) / (1+a4*a3) ) );
tan4 = abs( atand( (a1-a4) / (1+a1*a4) ) );
if ((tan1 > 180) | (tan2 > 180) | (tan3 > 180) | (tan4 > 180))
disp('this is not a convex quadrillateral!!')
end
Here is a very simple way to do it:
Take all combinations of 3 points (there are 4 in total).
Check if the fourth point is in the triangle defined by using those points as corners.
If any of the fourth points is in the triangle it is not convex, otherwise it is.
I think this will work for n point in general, if you are prepared to do n+1 checks.

Fastest Way to Find Distance Between Two Lat/Long Points

I currently have just under a million locations in a mysql database all with longitude and latitude information.
I am trying to find the distance between one point and many other points via a query. It's not as fast as I want it to be especially with 100+ hits a second.
Is there a faster query or possibly a faster system other than mysql for this? I'm using this query:
SELECT
name,
( 3959 * acos( cos( radians(42.290763) ) * cos( radians( locations.lat ) )
* cos( radians(locations.lng) - radians(-71.35368)) + sin(radians(42.290763))
* sin( radians(locations.lat)))) AS distance
FROM locations
WHERE active = 1
HAVING distance < 10
ORDER BY distance;
Note: The provided distance is in Miles. If you need Kilometers, use 6371 instead of 3959.
Create your points using Point values of Geometry data types in MyISAM table. As of Mysql 5.7.5, InnoDB tables now also support SPATIAL indices.
Create a SPATIAL index on these points
Use MBRContains() to find the values:
SELECT *
FROM table
WHERE MBRContains(LineFromText(CONCAT(
'('
, #lon + 10 / ( 111.1 / cos(RADIANS(#lat)))
, ' '
, #lat + 10 / 111.1
, ','
, #lon - 10 / ( 111.1 / cos(RADIANS(#lat)))
, ' '
, #lat - 10 / 111.1
, ')' )
,mypoint)
, or, in MySQL 5.1 and above:
SELECT *
FROM table
WHERE MBRContains
(
LineString
(
Point (
#lon + 10 / ( 111.1 / COS(RADIANS(#lat))),
#lat + 10 / 111.1
),
Point (
#lon - 10 / ( 111.1 / COS(RADIANS(#lat))),
#lat - 10 / 111.1
)
),
mypoint
)
This will select all points approximately within the box (#lat +/- 10 km, #lon +/- 10km).
This actually is not a box, but a spherical rectangle: latitude and longitude bound segment of the sphere. This may differ from a plain rectangle on the Franz Joseph Land, but quite close to it on most inhabited places.
Apply additional filtering to select everything inside the circle (not the square)
Possibly apply additional fine filtering to account for the big circle distance (for large distances)
Not a MySql specific answer, but it'll improve the performance of your sql statement.
What you're effectively doing is calculating the distance to every point in the table, to see if it's within 10 units of a given point.
What you can do before you run this sql, is create four points that draw a box 20 units on a side, with your point in the center i.e.. (x1,y1 ) . . . (x4, y4), where (x1,y1) is (givenlong + 10 units, givenLat + 10units) . . . (givenLong - 10units, givenLat -10 units).
Actually, you only need two points, top left and bottom right call them (X1, Y1) and (X2, Y2)
Now your SQL statement use these points to exclude rows that definitely are more than 10u from your given point, it can use indexes on the latitudes & longitudes, so will be orders of magnitude faster than what you currently have.
e.g.
select . . .
where locations.lat between X1 and X2
and locations.Long between y1 and y2;
The box approach can return false positives (you can pick up points in the corners of the box that are > 10u from the given point), so you still need to calculate the distance of each point. However this again will be much faster because you have drastically limited the number of points to test to the points within the box.
I call this technique "Thinking inside the box" :)
EDIT: Can this be put into one SQL statement?
I have no idea what mySql or Php is capable of, sorry.
I don't know where the best place is to build the four points, or how they could be passed to a mySql query in Php. However, once you have the four points, there's nothing stopping you combining your own SQL statement with mine.
select name,
( 3959 * acos( cos( radians(42.290763) )
* cos( radians( locations.lat ) )
* cos( radians( locations.lng ) - radians(-71.35368) )
+ sin( radians(42.290763) )
* sin( radians( locations.lat ) ) ) ) AS distance
from locations
where active = 1
and locations.lat between X1 and X2
and locations.Long between y1 and y2
having distance < 10 ORDER BY distance;
I know with MS SQL I can build a SQL statement that declares four floats (X1, Y1, X2, Y2) and calculates them before the "main" select statement, like I said, I've no idea if this can be done with MySql. However I'd still be inclined to build the four points in C# and pass them as parameters to the SQL query.
Sorry I can't be more help, if anyone can answer the MySQL & Php specific portions of this, feel free to edit this answer to do so.
I needed to solve similar problem (filtering rows by distance from single point) and by combining original question with answers and comments, I came up with solution which perfectly works for me on both MySQL 5.6 and 5.7.
SELECT
*,
(6371 * ACOS(COS(RADIANS(56.946285)) * COS(RADIANS(Y(coordinates)))
* COS(RADIANS(X(coordinates)) - RADIANS(24.105078)) + SIN(RADIANS(56.946285))
* SIN(RADIANS(Y(coordinates))))) AS distance
FROM places
WHERE MBRContains
(
LineString
(
Point (
24.105078 + 15 / (111.320 * COS(RADIANS(56.946285))),
56.946285 + 15 / 111.133
),
Point (
24.105078 - 15 / (111.320 * COS(RADIANS(56.946285))),
56.946285 - 15 / 111.133
)
),
coordinates
)
HAVING distance < 15
ORDER By distance
coordinates is field with type POINT and has SPATIAL index
6371 is for calculating distance in kilometres
56.946285 is latitude for central point
24.105078 is longitude for central point
15 is maximum distance in kilometers
In my tests, MySQL uses SPATIAL index on coordinates field to quickly select all rows which are within rectangle and then calculates actual distance for all filtered places to exclude places from rectangles corners and leave only places inside circle.
This is visualisation of my result:
Gray stars visualise all points on map, yellow stars are ones returned by MySQL query. Gray stars inside corners of rectangle (but outside circle) were selected by MBRContains() and then deselected by HAVING clause.
The following MySQL function was posted on this blog post. I haven't tested it much, but from what I gathered from the post, if your latitude and longitude fields are indexed, this may work well for you:
DELIMITER $$
DROP FUNCTION IF EXISTS `get_distance_in_miles_between_geo_locations` $$
CREATE FUNCTION get_distance_in_miles_between_geo_locations(
geo1_latitude decimal(10,6), geo1_longitude decimal(10,6),
geo2_latitude decimal(10,6), geo2_longitude decimal(10,6))
returns decimal(10,3) DETERMINISTIC
BEGIN
return ((ACOS(SIN(geo1_latitude * PI() / 180) * SIN(geo2_latitude * PI() / 180)
+ COS(geo1_latitude * PI() / 180) * COS(geo2_latitude * PI() / 180)
* COS((geo1_longitude - geo2_longitude) * PI() / 180)) * 180 / PI())
* 60 * 1.1515);
END $$
DELIMITER ;
Sample usage:
Assuming a table called places with fields latitude & longitude:
SELECT get_distance_in_miles_between_geo_locations(-34.017330, 22.809500,
latitude, longitude) AS distance_from_input FROM places;
if you are using MySQL 5.7.*, then you can use st_distance_sphere(POINT, POINT).
Select st_distance_sphere(POINT(-2.997065, 53.404146 ), POINT(58.615349, 23.56676 ))/1000 as distcance
SELECT * FROM (SELECT *,(((acos(sin((43.6980168*pi()/180)) *
sin((latitude*pi()/180))+cos((43.6980168*pi()/180)) *
cos((latitude*pi()/180)) * cos(((7.266903899999988- longitude)*
pi()/180))))*180/pi())*60*1.1515 ) as distance
FROM wp_users WHERE 1 GROUP BY ID limit 0,10) as X
ORDER BY ID DESC
This is the distance calculation query between to points in MySQL, I have used it in a long database, it it working perfect! Note: do the changes (database name, table name, column etc) as per your requirements.
set #latitude=53.754842;
set #longitude=-2.708077;
set #radius=20;
set #lng_min = #longitude - #radius/abs(cos(radians(#latitude))*69);
set #lng_max = #longitude + #radius/abs(cos(radians(#latitude))*69);
set #lat_min = #latitude - (#radius/69);
set #lat_max = #latitude + (#radius/69);
SELECT * FROM postcode
WHERE (longitude BETWEEN #lng_min AND #lng_max)
AND (latitude BETWEEN #lat_min and #lat_max);
source
select
(((acos(sin(('$latitude'*pi()/180)) * sin((`lat`*pi()/180))+cos(('$latitude'*pi()/180))
* cos((`lat`*pi()/180)) * cos((('$longitude'- `lng`)*pi()/180))))*180/pi())*60*1.1515)
AS distance
from table having distance<22;
A MySQL function which returns the number of metres between the two coordinates:
CREATE FUNCTION DISTANCE_BETWEEN (lat1 DOUBLE, lon1 DOUBLE, lat2 DOUBLE, lon2 DOUBLE)
RETURNS DOUBLE DETERMINISTIC
RETURN ACOS( SIN(lat1*PI()/180)*SIN(lat2*PI()/180) + COS(lat1*PI()/180)*COS(lat2*PI()/180)*COS(lon2*PI()/180-lon1*PI()/180) ) * 6371000
To return the value in a different format, replace the 6371000 in the function with the radius of Earth in your choice of unit. For example, kilometres would be 6371 and miles would be 3959.
To use the function, just call it as you would any other function in MySQL. For example, if you had a table city, you could find the distance between every city to every other city:
SELECT
`city1`.`name`,
`city2`.`name`,
ROUND(DISTANCE_BETWEEN(`city1`.`latitude`, `city1`.`longitude`, `city2`.`latitude`, `city2`.`longitude`)) AS `distance`
FROM
`city` AS `city1`
JOIN
`city` AS `city2`
The full code with details about how to install as MySQL plugin are here: https://github.com/lucasepe/lib_mysqludf_haversine
I posted this last year as comment. Since kindly #TylerCollier suggested me to post as answer, here it is.
Another way is to write a custom UDF function that returns the haversine distance from two points. This function can take in input:
lat1 (real), lng1 (real), lat2 (real), lng2 (real), type (string - optinal - 'km', 'ft', 'mi')
So we can write something like this:
SELECT id, name FROM MY_PLACES WHERE haversine_distance(lat1, lng1, lat2, lng2) < 40;
to fetch all records with a distance less then 40 kilometers. Or:
SELECT id, name FROM MY_PLACES WHERE haversine_distance(lat1, lng1, lat2, lng2, 'ft') < 25;
to fetch all records with a distance less then 25 feet.
The core function is:
double
haversine_distance( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error ) {
double result = *(double*) initid->ptr;
/*Earth Radius in Kilometers.*/
double R = 6372.797560856;
double DEG_TO_RAD = M_PI/180.0;
double RAD_TO_DEG = 180.0/M_PI;
double lat1 = *(double*) args->args[0];
double lon1 = *(double*) args->args[1];
double lat2 = *(double*) args->args[2];
double lon2 = *(double*) args->args[3];
double dlon = (lon2 - lon1) * DEG_TO_RAD;
double dlat = (lat2 - lat1) * DEG_TO_RAD;
double a = pow(sin(dlat * 0.5),2) +
cos(lat1*DEG_TO_RAD) * cos(lat2*DEG_TO_RAD) * pow(sin(dlon * 0.5),2);
double c = 2.0 * atan2(sqrt(a), sqrt(1-a));
result = ( R * c );
/*
* If we have a 5th distance type argument...
*/
if (args->arg_count == 5) {
str_to_lowercase(args->args[4]);
if (strcmp(args->args[4], "ft") == 0) result *= 3280.8399;
if (strcmp(args->args[4], "mi") == 0) result *= 0.621371192;
}
return result;
}
A fast, simple and accurate (for smaller distances) approximation can be done with a spherical projection. At least in my routing algorithm I get a 20% boost compared to the correct calculation. In Java code it looks like:
public double approxDistKm(double fromLat, double fromLon, double toLat, double toLon) {
double dLat = Math.toRadians(toLat - fromLat);
double dLon = Math.toRadians(toLon - fromLon);
double tmp = Math.cos(Math.toRadians((fromLat + toLat) / 2)) * dLon;
double d = dLat * dLat + tmp * tmp;
return R * Math.sqrt(d);
}
Not sure about MySQL (sorry!).
Be sure you know about the limitation (the third param of assertEquals means the accuracy in kilometers):
float lat = 24.235f;
float lon = 47.234f;
CalcDistance dist = new CalcDistance();
double res = 15.051;
assertEquals(res, dist.calcDistKm(lat, lon, lat - 0.1, lon + 0.1), 1e-3);
assertEquals(res, dist.approxDistKm(lat, lon, lat - 0.1, lon + 0.1), 1e-3);
res = 150.748;
assertEquals(res, dist.calcDistKm(lat, lon, lat - 1, lon + 1), 1e-3);
assertEquals(res, dist.approxDistKm(lat, lon, lat - 1, lon + 1), 1e-2);
res = 1527.919;
assertEquals(res, dist.calcDistKm(lat, lon, lat - 10, lon + 10), 1e-3);
assertEquals(res, dist.approxDistKm(lat, lon, lat - 10, lon + 10), 10);
Here is a very detailed description of Geo Distance Search with MySQL a solution based on implementation of Haversine Formula to mysql. The complete solution description with theory, implementation and further performance optimization. Although the spatial optimization part didn't work correct in my case.
http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL
Have a read of Geo Distance Search with MySQL, a solution
based on implementation of Haversine Formula to MySQL. This is a complete solution
description with theory, implementation and further performance optimization.
Although the spatial optimization part didn't work correctly in my case.
I noticed two mistakes in this:
the use of abs in the select statement on p8. I just omitted abs and it worked.
the spatial search distance function on p27 does not convert to radians or multiply longitude by cos(latitude), unless his spatial data is loaded with this in consideration (cannot tell from context of article), but his example on p26 indicates that his spatial data POINT is not loaded with radians or degrees.
$objectQuery = "SELECT table_master.*, ((acos(sin((" . $latitude . "*pi()/180)) * sin((`latitude`*pi()/180))+cos((" . $latitude . "*pi()/180)) * cos((`latitude`*pi()/180)) * cos(((" . $longitude . "- `longtude`)* pi()/180))))*180/pi())*60*1.1515 as distance FROM `table_post_broadcasts` JOIN table_master ON table_post_broadcasts.master_id = table_master.id WHERE table_master.type_of_post ='type' HAVING distance <='" . $Radius . "' ORDER BY distance asc";
Using mysql
SET #orig_lon = 1.027125;
SET #dest_lon = 1.027125;
SET #orig_lat = 2.398441;
SET #dest_lat = 2.398441;
SET #kmormiles = 6371;-- for distance in miles set to : 3956
SELECT #kmormiles * ACOS(LEAST(COS(RADIANS(#orig_lat)) *
COS(RADIANS(#dest_lat)) * COS(RADIANS(#orig_lon - #dest_lon)) +
SIN(RADIANS(#orig_lat)) * SIN(RADIANS(#dest_lat)),1.0)) as distance;
See: https://andrew.hedges.name/experiments/haversine/
See: https://stackoverflow.com/a/24372831/5155484
See: http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/
NOTE: LEAST is used to avoid null values as a comment suggested on https://stackoverflow.com/a/24372831/5155484
I really liked #Māris Kiseļovs solution, but I like many others may have the Lat and lng's POINTS reversed from his example. In generalising it I though I would share it. In my case I need to find all the start_points that are within a certain radius of an end_point.
I hope this helps someone.
SELECT #LAT := ST_X(end_point), #LNG := ST_Y(end_point) FROM routes WHERE route_ID = 280;
SELECT
*,
(6371e3 * ACOS(COS(RADIANS(#LAT)) * COS(RADIANS(ST_X(start_point)))
* COS(RADIANS(ST_Y(start_point)) - RADIANS(#LNG)) + SIN(RADIANS(#LAT))
* SIN(RADIANS(ST_X(start_point))))) AS distance
FROM routes
WHERE MBRContains
(
LineString
(
Point (
#LNG + 15 / (111.320 * COS(RADIANS(#LAT))),
#LAT + 15 / 111.133
),
Point (
#LNG - 15 / (111.320 * COS(RADIANS(#LAT))),
#LAT - 15 / 111.133
)
),
POINT(ST_Y(end_point),ST_X(end_point))
)
HAVING distance < 100
ORDER By distance;