Creating a point a certain angle from another - iphone

I have a point A, I also have the angle.
I also have the distance from point A to point B.
What I want to do is create point B a certain angle away from point A.
Im a bit of a maths idiot so any help would be great.

Your point will be this one:
NSPoint PointB = NSMakePoint(PointA.x + distance * sin(angle),
PointA.y + distance * cos(angle));

Bx=Ax+distance*cos(angle)
By=Ay+distance*sin(angle)

Related

Finding the tangent on a given point of a polyline

I have a list of X,Y coordinates that represents a road. For every 5 meters, I need to calculate the angle of the tangent on this road, as I have tried to illustrate in the image.
My problem is that this road is not represented by a mathematical function that I can simply derive, it is represented by a list of coordinates (UTM33N).
In my other similar projects we use ArcGIS/ESRI libraries to perform geographical functions such as this, but in this project I need to be independent of any software that require the end user to have a license, so I need to do the calculations myself (or find a free/open source library that can do it).
I am using a cubic spline function to make the line rounded between the coordinates, since all tangents on a line segment would just be parallell to the segment otherwise.
But now I am stuck. I am considering simply calculating the angle between any three points on the line (given enough points), and using this to find the tangents, but that doesn't sound like a good method. Any suggestions?
In the end, I concluded that the points were plentiful enough to give an accurate angle using simple geometry:
//Calculate delta values
var dx = next.X - curr.X;
var dy = next.Y - curr.Y;
var dz = next.Z - curr.Z;
//Calculate horizontal and 3D length of this segment.
var hLength = Math.Sqrt(dx * dx + dy * dy);
var length = Math.Sqrt(hLength * hLength + dz * dz);
//Calculate horizontal and vertical angles.
hAngle = Math.Atan(dy/dx);
vAngle = Math.Atan(dz/hLength);

Get CGPoint x points in front of node

I am working on a game in sprite kit and have been trying to get a point in front of a node. I've been reading up on trigonometry but have not been able to do it.
The problem: Get a CGPoint x units in front of an SKSpriteNode, relative to zRotation. See the illustration here: http://i.stack.imgur.com/TGZ51.png
I have understood that i can use the adjacent and opposite lengths in the triangle to calculate the distance of the hypotenuse (and that the hypotenuse is a vector?). However, i've failed to understand how to get this vector relative to current zPosition and how to get a point from the vector.
I would be grateful if anyone can provide some sample code or point me in a direction where i can find more info.
Thanks a lot!
I solved it after trying some more and here's how i did it:
- (CGVector)convertAngleToVector:(CGFloat)radians {
CGVector vector;
vector.dx = cos(radians) * 40;
vector.dy = sin(radians) * 40;
return vector;
}
I call the method with the sprites zRotation which gives me a vector. The number 40 decides how long the vector is. Then i just added the vector to the current position.

Figuring out distance and course between two coordinates

I have 2 coordinates and would like to do something seemingly straightforward. I want to figure out, given:
1) Coordinate A
2) Course provided by Core Location
3) Coordinate B
the following:
1) Distance between A and B (can currently be done using distanceFromLocation) so ok on that one.
2) The course that should be taken to get from A to B (different from course currently traveling)
Is there a simple way to accomplish this, any third party or built in API?
Apple doesn't seem to provide this but I could be wrong.
Thanks,
~Arash
EDIT:
Thanks for the fast responses, I believe there may have been some confusion, I am looking to get the course (bearing from point a to point b in degrees so that 0 degrees = north, 90 degrees = east, similar to the course value return by CLLocation. Not trying to compute actual turn by turn directions.
I have some code on github that does that. Take a look at headingInRadians here. It is based on the Spherical Law of Cosines. I derived the code from the algorithm on this page.
/*-------------------------------------------------------------------------
* Given two lat/lon points on earth, calculates the heading
* from lat1/lon1 to lat2/lon2.
*
* lat/lon params in radians
* result in radians
*-------------------------------------------------------------------------*/
double headingInRadians(double lat1, double lon1, double lat2, double lon2)
{
//-------------------------------------------------------------------------
// Algorithm found at http://www.movable-type.co.uk/scripts/latlong.html
//
// Spherical Law of Cosines
//
// Formula: θ = atan2( sin(Δlong) * cos(lat2),
// cos(lat1) * sin(lat2) − sin(lat1) * cos(lat2) * cos(Δlong) )
// JavaScript:
//
// var y = Math.sin(dLon) * Math.cos(lat2);
// var x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
// var brng = Math.atan2(y, x).toDeg();
//-------------------------------------------------------------------------
double dLon = lon2 - lon1;
double y = sin(dLon) * cos(lat2);
double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
return atan2(y, x);
}
See How to get angle between two POI?
Depending on how much work you want to put in this one, I would suggest looking at Tree Traversal Algorithms (check the column on the right), things like A* alpha star, that you can use to find your find from one point to another, even if obstacles are in-between.
If I understand you correctly, you have the current location and you have some other location. You want to find the distance (as the crow flies) between the two points, and to find a walking path between the points.
To answer your first question, distanceFromLocation will find the distance across the earth's surface between 2 points, that is it follows the curvature of the earth, but it will give you the distance as the crow flies. So I think you're right about that.
The second question is a much harder. What you want to do is something called path-finding. Path finding, require's not only a search algorithm that will decide on the path, but you also need data about the possible paths. That is to say, if you want to find a path through the streets, the computer has to know how the streets are connected to each other. Furthermore, if you're trying to make a pathfinder that takes account for traffic and the time differences between taking two different possible paths, you will need a whole lot more data. It is for this reason that we usually leave these kinds of tasks up to big companies, with lots of resources, like Google, and Yahoo.
However, If you're still interested in doing it, check this out
http://www.youtube.com/watch?v=DoamZwkEDK0

implementing a simple geofence in objective-C

I am trying to implement somewhat of a simple geofence algorithm that basically does the following:
Say I have two point A and B (each point has a latitude and longitude
value in earth).
I can draw a straight line from point A to point B
I can set a perimeter, which is a rectangle, around that line (see drawing
below for more clarity)
What I want to do is as follows, if the phone current location is outside of this red perimeter then it triggers something, basically a delegate. The perimeter size should be able to be adjusted to a percentage size, so 5% would be a small perimeter around the line and 70% would be a large perimeter around the line. Be aware that the perimeter should be a rectangle, not circle with radius. I am guessing that there will be a bunch of if statements involved in building this... if anyone could come up with a simple and elegant solution to this (would be great if I can see code in objective-C) that would be awesome. Or any guidance would be helpful as well
You can create a path from the four points of the rectangle and then use CGPathContainsPoint to check whether the current location is inside the path.
As for the conversion of latitude and longitude to planar x, y coordinates, the simplest solution is to use Mercator projection using Map Kit. Check Understanding Map Geometry for more info.
Here's an example:
// create four rectangle points from A, B
dx = (B.x - A.x) * 0.05; // 5% of the A-B length
dy = (B.y - A.y) * 0.05;
// topmost corner, above B
points[0].x = B.x + dx - dy;
points[0].y = B.y + dy + dx;
//rightmost corner, to the right from B
points[1].x = B.x + dx + dy;
points[1].y = B.y + dy - dx;
...
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, points[0].x, points[0].y);
CGPathAddLineToPoint(path, NULL, points[1].x, points[1].y);
CGPathAddLineToPoint(path, NULL, points[2].x, points[2].y);
CGPathAddLineToPoint(path, NULL, points[3].x, points[3].y);
CGPathCloseSubpath(path);
// convert latitude, longitude to planar coordinates
MKMapPoint location = MKMapPointForCoordinate([newLocation coordinate]);
BOOL inside = CGPathContainsPoint(path, NULL, CGPointMake(location.x, location.y), YES);
CGPathRelease(path);
Note: This code expects that the current location is a point, while in reality, it is a point and a radius of accuracy, which is effectively a circle. This complicates things a bit because now you need to define how to handle situations when the current location is not known exactly, but you only know that it's somewhere in the circle. If the rectangle is large (say 5 km), then you may simply require radius of accuracy less than 50m, do the calculation as if the current location was exact and ignore the small inaccuracy of the computation. If the rectangle is smaller (say 50m), you may also do the calculation as if the current location was exact, but then the false positives probability would be higher (e.g. sometimes you would be detected as in the rectangle while you would be standing outside of it).
Or you may want to go for the "perfect" solution and do circle-rectangle intersection, which is more complex and may result not only in YES and NO answers but also in "with this accuracy it cannot be determined whether you are inside or outside of the rectangle".
You need to find the nearest point on the main A-B line to the users location. Look at the following link for more information... Point Line
Now given that you can find the nearest point on a line from users point (current location) you can check if the distance between their location and the nearest point is within the threshold you are interested in, if it exceeds it then they are 'outside' of the zone around the line.

How to calculate short & long distance via Haversine?

I am looking for a way to calculate the distance between 2 points on the globe. We've been told to use Haversine, which works fine to calculate the shortest distance between the 2 points.
Now, I'd like to calculate the "long distance" between to points. So suppose you have 2 cities, A in the west and B in the east. I want to know the distance from B to A if I would travel eastwards around the globe and then reach A coming from the west.
I've tried changing a couple of things in the haversine function, but doesn't seem to work.
Anyone know how I can simply do this using small adjustments to the haversine function?
This is what I'm using now:
lat1, lat2, lng1, lng2 are in radians
part1 = sin(lat2) * sin(lat1);
part2 = cos(lat2) * cos(lat1) * cos(lng1 - lng2);
distance = 6378.8 * acos(part1 + part2);
The way I see it is that you can draw a circle around the globe between the 2 cities. The long distance the the circumference of that circle minus the short distance. But in contrary of what was replied, the circle's length is not equal to the earth's circumference. This is only the case for 2 points on the equator.
Tnx
Jeroen
The circumference of the earth is approx 40,075KM, work out the short distance and subtract it from that.