Ok, so I am still new to asking questions on Stack Overflow, so please be nice :)
I have an issue that I have been trying to solve for a month now with failed results time and time again. I have found various questions somewhat similar to this, but nothing that has struck me as the definitive answer.
The closest that I have found so far is this: Convert GPS coordinates to coordinate plane.
In short, what I need is to know is if a GPS Coordinate is either on or inside an ellipse.
KNOWNS:
UNKNOWNS / NEEDS:
Please assist as for some reason, I just cannot wrap my brain around this math.
UPDATE: To add more information, the values are all quite small in the grand scheme of things.
As an example: If a user wants to know if another user has entered some park/field/geofenced area or some other type of physical area. The physical area in this case is designed as an Ellipse.
As an addition, this is written in Objective-C. Below, you will see a random "+90" degrees, this is there as the mechanisms underlying see 0 degrees as North (Navigation) when I want it to be the Unit-Circle "Normal" .
Associated Code with Discussion:
- (BOOL)isLocation:(CLLocation *)location withinEllipse:(Ellipse *)ellipse
{
BOOL locationIsWithinEllipse = NO;
double ellipseWidth = ellipse.width;
double ellipseHeight = ellipse.height;
CLLocationDegrees locationAngleDegrees = 360 - ((int)([self headingBetweenCoordinate1:location.coordinate coordinate2:ellipse.locationCenter.coordinate] + 90) % 360);//Invert Direction
double xOffsetInMeters = (ellipseWidth/2) * COS(DEGREES_TO_RADIANS(locationAngleDegrees));
double yOffsetInMeters = (ellipseHeight/2) * SIN(DEGREES_TO_RADIANS(locationAngleDegrees));
// The logic below will currently grab the Top-Right point as if it were a box, not the point on the Ellipse,
// This is where things are broken. I need this to be the GPS Coordinate of the Point on the Ellipse with angle (locationAngleDegrees)
// Grab the Coordinate on the Ellipse in the heading of the Test Point
CLLocationDegrees pointLat = [ellipse.locationCenter addToLocationDistanceInMeters:yOffsetInMeters withBearingInDegrees:0].coordinate.latitude;
CLLocationDegrees pointLong = [ellipse.locationCenter addToLocationDistanceInMeters:xOffsetInMeters withBearingInDegrees:90].coordinate.longitude;
CLLocation * testPointOnEllipseLocation = [[CLLocation alloc] initWithLatitude:pointLat longitude:pointLong];
// Just check if the Test Point is closer than the Distance of the Ellipse Point
if(ABS([location distanceFromLocation:ellipse.locationCenter]) <= ABS([testPointOnEllipseLocation distanceFromLocation:ellipse.locationCenter]))
{
locationIsWithinEllipse = YES;
}
return locationIsWithinEllipse;
}
UPDATE:
I am still trying to get this math correct. I understand how to get it done with "school math", but how can I apply that to my code in the example? Also, I seriously don't understand all of this rotation stuff as I have written all of my code to be agnostic of any rotation. I believe that is all handled in Apple's low-level location stuff.
Can anyone please assist?
I have solved this by using some mathematical calculations from Google and guidance from the (very helpful) following question: How to determine if a latitude & longitude is within an ellipse
I also used this reference for the Foci calculations: http://www.mathopenref.com/ellipsefoci.html
To quote, from Cody in the link, the primary logic: "My ellipse is relatively small so I assumed it was a true (flat) ellipse. I was able to locate the lat lon of the foci of the ellipse then if the sum of the distances from the point of interest to each focus is less than 2a (the major axis radius), then it is within the ellipse."
I do appreciate the suggestions presented! Thank You!