Search for coordinates in a path - iphone

I'm new in iOS iPhone development and I'm having some problems using maps.
I'm trying to find a way to create a path with lots of coordinates points (latitude, longitude) and then check if a specific coordinate point is inside this path. I've already looked for how to do this using Apple Maps and the new Google SDK, but with no success.
The complete problem is:
I have some coordinates (but not all) that represents the path that a vehicle pass by. I need to check if a specific coordinate is inside this vehicle path. For example: I have the coordinates X, Y and Z. If I create a path with this coordinates the complete path will have the coordinates X, Y, W and Z. With this path I can check for the coordinate W, that was not in the coordinates used to construct the path. It's something like creating a path between two points but telling the map to create a route using some checkpoints and after check if a random coordinate is inside this path.
There is a way to do something like this?

I guess for a simple solution you could use something like
(MKUserLocation *)userLocation;
CLLocation* X = [init from some coordinates];
CLLocation* Y = [init from some coordinates];
CLLocation* W = userLocation.location;
CLLocationDistance distToX = [X distanceFromLocation:W];
CLLocationDistance distToY = [Y distanceFromLocation:W];
and to detect how far W is from the line defined by X,Y, you can adjust the following code:
def pointLineDistance(p,v1,v2):
return (numpy.linalg.norm(p-v1)**2 * numpy.linalg.norm(v2-v1)**2 - (numpy.dot((v1-p),(v2-v1)))**2) / numpy.linalg.norm(v2-v1)**2
Sorry that I am using python & numpy here, just copy & pasted it, but thats the 2d / 3d generalized implementation of a distance between a point p (your W) and two other vectors v1,v2 (your X,Y).
Look up http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
I don't know how it complies for spherical surface coordinates but it's ok for Eucledian space.
Cheers,
EL

Related

Is it posssible to find an x/y coordinate without Angles? i need this for a target locator in a Scratch Program

My scratch project:https://scratch.mit.edu/projects/657133771
i need a triangulation/trilateralation equation for a target locator. otherwise the tank is just stupid.
Try using the Distance Formula (Pythagorean Theorem) to calculate the distance between points. Simply use:
distance = sqrt( (x2-x1)^2 + (y2-y1)^2 )
Make the x and y change at the same time. It will give an angle effect, and your sprite does not have to turn. Not exactly a formula, but if you match up the x and y changes in the right way, you get different angles.

Best way to find the top most left coordinate point in set of Cartesian coordinates?

I have a set of cartesian coordinates. What is the best way to find the top most left coordinate?
My approach is to find max Y and then corresponding lowest X coordinate. (Since there could be multiple points with same Ymax). This works fine but wonders if there are other cute methods?
If your x and y coordinates are not sorted, then you have to check all the input coordinates - at first your y values (for topmost, the higher the better) and then all corresponding x values for such y, but now you look for minimal x.

MATLAB: Failed to create geometry. The stl file is invalid, more than two facets share an edge

im trying to create a crater alpha shape.
this is what i wrote so far:
ddr=0.12;
a=1/ddr/2; %semi major axis. horizontal axes are equal
n=91; %number of points for x y vectors.
x = linspace(-a,a,n);
y = linspace(-a,a,n);
[X,Y] = meshgrid(x,y);
Z =real(sqrt(1-(X.^2)/a^2-(Y.^2)/a^2))*-1+1; % the plus 1 for Z>0
shp = alphaShape(X(:),Y(:),Z(:));
plot(shp);
[elements,nodes] = boundaryFacets(shp);
nodes = nodes';
elements = elements';
model = createpde();
geometryFromMesh(model,nodes,elements);
but im getting the following error:
Failed to create geometry. The stl file is invalid, more than two facets share an edge.
thank you for your help
I suspected that the problem comes only because of the interface of one object over the another (possibly there are more than one objects in your geometry), even though there are no overlaps. Try import a single object instead of importing the whole geometry.
Another solution is to convert your stl file to faces and vertices using stlread.m
and reconstruct the geometry.
Good luck!

Matlab and color point specific areas

I have a grid with fixed points and random generated user's positions.
Distances for each point and user are measured from the beginning of the axis 0.0. I want to associate each user to the closest fixed point. I calculate both distance vectors and the min of them per user is pointing to the closest fixed point.
But i am stuck on finding a working way so each fixed point and associated user have something same in plot, p.e. same color and color area.
So my problem is two dimensional:
First is to manage to associate each user to its closest fixed poind
How to color the result.
Thank you.
For the point searching I would use dsearchn for this kind of thing. You can use it with or without delaunay triangulation depending on the ratio of users to fixed sites. I tend to use it the quick and easy way, which in your case would be:
indices_of_closest_fixed_points = dsearchn(fixed_points, user_points)
As for the colors I would suggest you define a color map using something like
mymap = lines(n)
where n is the number of fixed points you have. You can then use scatter to plot the points with specific colors and sizes. Perhaps something like this to get you started:
x = user_points(1,:);
y = user_points(2,:);
S = []; % point sizes, left empty for now
C = mymap(indices_of_closest_fixed_points,:); %colors
scatter(x,y,S,C);
To find the nearest point simply compute the euclidean distance between each user point and the complete set of fixed points. Then the index of the shortest distance will also be the index of the fixed point.
dist = calc_dist(fixedPts, aSingleUserPt)
[~, idx] = min(dist);
To solve the color problem, you'll need to create a colormap from a fixed point index to a unique color. Then when you plot a user point you will set the color of the plot equal to the colormap evaluated at idx
Note the euclidean distance is very easy to calcuate:
euc_dist = sqrt( (x1 - x2)^2 + (y1 - y2)^2 );
There are functions on File Exchange that will let you compute this quickly.

How to calculate Cross-Track error (GPS/Core Location)

Does anyone know how to determine determine the "Cross-Track Error"?
For those who are unfamiliar: You are driving along a line from Point "A" to point "B". When in transit, when you veer off that line, the distance from your current position to the line is the cross-track error.
I have a simple algorithm now which works, using basic geometry with the latitude and longitude of the three points - the problem is that it does not take "great circle" calculations into account (i.e. actual meters-per-degree longitude varies depending on your latitude, and does not equal that of the latitude).
In other words - if you know of a "great circle" formula for determining this, please let me know - but it is not a straight Cartesian geometry problem.
Brad,
I'm not sure which ellipsoid model you are using since you don't say. If you aren't using an ellipsoid model in you current calculations, you may find this helpful:
http://www.movable-type.co.uk/scripts/latlong-vincenty.html
The Vincenty algorithm is more accurate that the Haversine algorithm.
Once you have accurate distances for A-B, A-C and B-C, it should be straightforward to determine your distance from C to the line A-B. Something like a binary search of the distances from points on A-B to C, looking for the shortest value.
James
This is this text from the link to the accepted answer - should it go dead:
Here’s a new one: I’ve sometimes been asked about distance of a point from a great-circle path (sometimes called cross track error).
Formula: dxt = asin( sin(δ13) ⋅ sin(θ13−θ12) ) ⋅ R
where:
δ13 is (angular) distance from start point to third point
θ13 is (initial) bearing from start point to third point
θ12 is (initial) bearing from start point to end point
R is the earth’s radius
JavaScript:
var δ13 = d13 / R;
var dXt = Math.asin(Math.sin(δ13)*Math.sin(θ13-θ12)) * R;
Here, the great-circle path is identified by a start point and an end point – depending on what initial data you’re working from, you can use the formulas above to obtain the relevant distance and bearings. The sign of dxt tells you which side of the path the third point is on.
The along-track distance, from the start point to the closest point on the path to the third point, is:
Formula: dat = acos( cos(δ13) / cos(δxt) ) ⋅ R
where:
δ13 is (angular) distance from start point to third point
δxt is (angular) cross-track distance
R is the earth’s radius
JavaScript:
var dAt = Math.acos(Math.cos(δ13)/Math.cos(dXt/R)) * R;
If dealing with latitude and longitude, the forumla you're looking for is the "Haversine" formula. It takes into account the curvature of the earth's surface.
http://en.wikipedia.org/wiki/Haversine_formula
Good luck.
The CLLocation API provides
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
Which uses a formula (it does not specify whether is it Haversine or
Vincenty or other) that takes into account the curvature of the earth. This returns the distance in meters between the 2 CLLocations but does not account for any difference in altitude.