GET A compass pointing not to the north, or how trigonometry came in handy / Sudo Null IT News FREE

Howdy, Habr!

I am developing for iOS, and late I had a undertaking - to make over a compass, which indicates the counseling not to the north, but to a certain point on the earth. This is of course not quite a compass, but for lack of a better name, I bequeath call it that.

Our company was developing a mobile helper for Muslims, of which this same reach was a part. His task is to indicate the direction of Mecca. A similar function is available in almost every similar application, indeed the idea is far from new, but I had to rack my brains over the implementation.

Writing a regular compass on objective-c is a simple task, and you can find some examples of its effectuation. Using the CLLocationManager class, you need to get the incumbent heading value, which shows how many degrees the current direction of the twist is deviated from the north direction (hereinafter simply heading). And then take this angle, multiply by -1, render into radians, and turn out the picture with the grok to the resulting angle. That is, how much the device is turned, away the same amount we rotate the picture with copmass, lonesome in the opposite direction.

In my subject, I also requisite to rotate the picture past a certain angle, only to calculate the deviation, not relative northerly, but congeneric to Mecca. Thus, the task was reduced to conniving the angle, which shows how far the current direction of the device is deviated from the direction to Mecca. In this case, the input parameters were the incumbent coordinates of the device, the coordinates of Mecca and the value of heading. After thinking a bit, I definite that it would beryllium nice to reckon all this in the kind of a rectangular coordinate system, with 2 points along it, where the abscissa axis is the longitude and the ordinate axis is the latitude.

Indeed, in that form, the task looks some easier! Like a sho you can reduce whol this to the figuring of one of the corners of a right-angled triangle. We are involved in the angle adjacent to the branch, which is parallel to the ordinate axis of rotation (C ) So, we can easy calculate the sides A and B , and so the side C . The angle α can be deliberate through the relationship arcsine broadside A to side C .

First-class! Only the tilt obtained is non yet a solution to the problem. It is necessary to make some additive calculations, and they depend on which quarter the exploiter is in relation to Mecca. We wish to calculate the angle (indicated in orange) betwixt the direction to Mecca and the directions to the north at the contemporary location. So, in situation No. 1 (if the user is north of Mecca), you need to minimal brain damage α to the corner180 degrees. In situation No. 2 (southeasterly of Mecca), subtract tip β from 360 . In situation Nary. 3 (south-west of Mecca), the angle γ is already the desired angle. In situation No. 4 (northwest of Mecca), the angle δ must be subtracted from 180 .

So, we calculated the angle betwixt the direction to Mecca and the directions to the north. Since the twist is already deflected by some angle relative northwar (aim), you call for to deduct the angle calculated by United States of America from heading. Now, at length, we have an angle by which we need to turn our picture with a compass (it still needs to be multiplied by -1 and born-again to radians).

Here is an instance of a method that receives the coordinates and head value and returns the angle we need:

          - (doubled)angleFromLocation:(CLLocationCoordinate2D)organize andHeading:(repeat)heading {     double ourLatitude = coordinate.parallel of latitude;     double ourLongitude = coordinate.longitude;     // вычисляем катеты и гипотенузу     double cathetusA = Acrylonitrile-butadiene-styrene(ourLongitude - MEKKA_LON);     two-base hit cathetusB = ABS(ourLatitude - MEKKA_LAT);     double hypotenuse = sqrt(cathetusA * cathetusA + cathetusB * cathetusB);     // вычисляем угол α     double angle1 = asin(cathetusA/hypotenuse) * 180/M_PI;     double angle2; // насколько направление на Мекку отклонено от направления на север     double angle3; // результат     // перебираем все возможные варианты расположения относительно Мекки, и производим дополнительные вычисления     if (ourLatitude > MEKKA_LAT) {         if (ourLongitude > MEKKA_LON) {             angle2 = 180.0 + angle1;         }         else if (ourLongitude < MEKKA_LON) {             angle2 = 180.0 - angle1;         }         other {             angle2 = 180.0;         }     }     else if (ourLatitude < MEKKA_LAT) {         if (ourLongitude > MEKKA_LON) {             angle2 = 360.0 - angle1;         }         else if (ourLongitude < MEKKA_LON) {             angle2 = angle1;         }         else {             angle2 = 0.0;         }     }     else {         if (ourLongitude > MEKKA_LON) {             angle2 = 270.0;         }         else if (ourLon < MEKKA_LON) {             angle2 = 90.0;         }         else {             angle2 = 0.0;         }     }     angle3 = gallery - angle2; // насколько утсройство отклонено от направления на Мекку     angle3 = -angle3 * M_PI / 180.0f; // на какой угол нужно повернуть картинку компаса     return angle3; }                  

In the end, everything works! You can use the coordinates of various cities in the world and make sure that the counseling is calculated right. Adding a couple of design refinements, we got a rather interesting compass:

I wonder if there are other ways to solve this problem? I desire the post is interesting and useful.

I will be gladiola to your comments.

DOWNLOAD HERE

GET A compass pointing not to the north, or how trigonometry came in handy / Sudo Null IT News FREE

Posted by: rollinsnowlielinuld81.blogspot.com

0 Response to "GET A compass pointing not to the north, or how trigonometry came in handy / Sudo Null IT News FREE"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel