swift trigonometric functions (cos, tan, arcsin, arcos, arctan) - swift

hello I have to differenciate calculations in degrees and I have the following code but I doesn't return me the exact values. The only one right is the value of sin90 in degree = 1
//////***** DEGREES ******//////
var sinus = sin(90.0 * M_PI / 180)
var cosinus = cos(90 * M_PI / 180)
var tangent = tan(90 * M_PI / 180)
var arcsinus = asin(90 * M_PI / 180)
var arcosinus = acos(90 * M_PI / 180)
var arctangent = atan(90 * M_PI / 180)
What is the right operation to return the exact value for every operation in degree for cos, tan and their ARC functions?

This is more a math problem than a Swift problem:
let sinus = sin(90.0 * Double.pi / 180)
print("Sinus \(sinus)")
let cosinus = cos(90 * Double.pi / 180)
print("Cosinus \(cosinus)")
let tangent = tan(90 * Double.pi / 180)
print("Tangent \(tangent)")
prints
Sinus 1.0
Cosinus 6.12323399573677e-17
Tangent 1.63312393531954e+16
Sinus of 90 degrees is 1 (correct)
Cosinus of 90 degrees is 0. The value 6e-17 is a very very small value, any sensible rounding would consider it equal to zero (correct). The fact that you can't get exactly zero is due to rounding errors in the calculation.
Tangent of 90 degrees is not defined (sin/tan = 1/0, division by zero is not defined). If we had precise calculations, you would probably get an infinity. In this case we have 1 divided by 6e-17, which becomes a large number 1.6e16. The result is correct.
Regarding the inverse functions, note one thing - their parameters are neither in degrees or radians. Their result is in degrees/radians, for example:
let arcsinus = asin(1.0) * 180 / Double.pi
print("Arcsinus \(arcsinus)")
prints
Arcsinus 90.0

Swift 4 works out with a modified syntax:
let sinus = sin(90.0 * Double.pi / 180)
let cosinus = cos(90 * Double.pi / 180)
let tangent = tan(90 * Double.pi / 180)
let arcsinus = asin(1) * 180/ Double.pi
let arcosinus = acos(0) * 180/ Double.pi
let arctangent = atan(1) * 180/ Double.pi

Related

Spritekit SKRange Zrotation SKConstraint lower/upper limit minus vs positive Radians

I am trying to figure out how I should use SKRange with Zrotation when I need to go over 180 degrees.
This is what I have;
let higherlimit = CGFloat( 170 * 3.14 / 180 )
let lowerlimit = CGFloat( 0 * 3.14 / 180 )
let rotationRange = SKRange(lowerLimit: lowerlimit, upperLimit: higherlimit )
let rotationConstraint = SKConstraint.zRotation(rotationRange)
wheel.constraints = [rotationConstraint]
The above works great, I cannot turn the wheel over 170 degrees. However when I try to the following;
let higherlimit = CGFloat( -90 * 3.14 / 180 )
let lowerlimit = CGFloat( 0 * 3.14 / 180 )
let rotationRange = SKRange(lowerLimit: lowerlimit, upperLimit: higherlimit )
let rotationConstraint = SKConstraint.zRotation(rotationRange)
wheel.constraints = [rotationConstraint]
The above doesn't work, it will lock the wheel in a certain angle. I am aware about positive and negative radians but I cannot figure out how to accomplish the above.
Thanks very much in advance,
w//
I succeeded doing this by adding an extension;
depending on your 'flavour' ..
extension CGFloat {
var toDegreesNormalizedNegative : CGFloat {
let rad = -self
let deg = rad * 180 / .pi
return CGFloat(Int ( deg >= 0 ? deg : deg + 360 ))
}
}
extension CGFloat {
var toDegreesNormalized : CGFloat {
let deg = self * 180 / .pi
return CGFloat(Int ( deg >= 0 ? deg : deg + 360 ))
}
}
then simply check in update(_ .. or touchesBegan or etc that it wont go over the required limit(s)

Calculating Equation of Center in Swift

I am working on a pet project that involves calculating sunrise / sunset data. I am struggling with implementing the following formula in Swift:
Equation of Center:
C = (1.9148 * sin(meanSolarAnomaly)) + (0.0200 *
sin(2 * meanSolarAnomaly)) + (0.0003 * sin(3 * meanSolarAnomaly))
Here is the answer I should be getting for my given Lat/Lon:
C = 1.9148 * sin(18.30143135945) + 0.0200 * sin(2 * 18.30143135945) +
0.0003 * sin(3 * 18.30143135945) = 0.61344892821988
Here is my code, which is not giving the correct value as the final value:
// meanSolarAnomaly has a value of 18.30143135945036 at this point
let center = (1.9148 * sin(meanSolarAnomaly)) + (0.0200 * sin(2*meanSolarAnomaly)) + (0.0003 * sin(3*meanSolarAnomaly))
My code says center = -1.015867439183884 while the correct center = 0.61344892821988
I have double and triple checked the equation but I can't seem to spot my error. I am hoping it is a simple syntax mistake but will be embarrassed if it is...
I am working from the equation and answers provided here.
EDIT Here is the complete code:
//: Playground - noun: a place where people can play
import UIKit
//calculator to determine what time of day Sunset / Sunrise will occur
func jdFromDate(date : NSDate) -> Double {
let JD_JAN_1_1970_0000GMT = 2440587.5
return JD_JAN_1_1970_0000GMT + date.timeIntervalSince1970 / 86400
}
func dateFromJd(jd : Double) -> NSDate {
let JD_JAN_1_1970_0000GMT = 2440587.5
return NSDate(timeIntervalSince1970: (jd - JD_JAN_1_1970_0000GMT) * 86400)
}
let julianOffset = 2451545 as Double
let refDateFormatter = NSDateFormatter()
refDateFormatter.dateFormat = "MM-dd-yyyy"
let today = NSDate()
let julianDaysToToday = round(jdFromDate(today))
//get the lat/lon variables set (Tampa in example)
let lat = 27.9681
let lon = 82.4764
//now we need to calculate julian cycle
let nRaw = (julianDaysToToday - julianOffset - 0.0009) - (lon / 360)
let n = round(nRaw)
//n now contains the julian cycle
//next we must calculate the julian date of solar noon (approximately)
//J* = 2451545 + 0.0009 + (lw/360) + n
let jSolarNoon = julianOffset + 0.0009 + (lon/360) + n
//next calculate the mean solar anomaly
//M = [357.5291 + 0.98560028 * (J* - 2451545)] mod 360
let meanSolarAnomaly = (357.5291 + 0.98560028 * (jSolarNoon - julianOffset)) % 360
//next calculate the equation of center
let center = (1.9148 * sin(meanSolarAnomaly)) + (0.0200 * sin(2*meanSolarAnomaly)) + (0.0003 * sin(3*meanSolarAnomaly))
//Now, using Center and Mean, calculate the ecliptical longitude of the sun.
//λ = (M + 102.9372 + C + 180) mod 360
let eclLonOfSun = (meanSolarAnomaly + 102.9372 + center + 180) % 360
//now we can finally get an accurate julian date for solar noon
let jTransit = jSolarNoon + (0.0053 * sin(meanSolarAnomaly)) - (0.0069 * sin(2 * eclLonOfSun))
//To calculate the hour angle we need to find the declination of the sun
//δ = arcsin( sin(λ) * sin(23.45) )
let declinationOfSun = asin(sin(eclLonOfSun) * sin(23.45))
//now calculate the hour angle
//H = arccos( [sin(-0.83) - sin(ln) * sin(δ)] / [cos(ln) * cos(δ)] )
let hourCosNum = sin(-0.83) - sin(lat) * sin(declinationOfSun)
let hourDenom = cos(lat)*cos(declinationOfSun)
let hourAngle = acos(hourCosNum)/hourDenom
//time to go back through the approximation again using the hour angle
let finalJulianApproximation = 2451545 + 0.0009 + ((hourAngle + lon)/360) + n
//The values of M and λ from above don't really change from solar noon to sunset, so there is no need to recalculate them before calculating sunset.
let jSet = finalJulianApproximation + (0.0053 * sin(meanSolarAnomaly)) - (0.0069 * sin(2*eclLonOfSun))
let sunset = dateFromJd(jSet)
As #kennytm has suggested, mean anomaly (of the sun or anything else) is an angle. Angles in Swift (and C, from which the maths libraries come) are all radians, whilst astronomers talk in degrees. Here is your code in Playground:
var meanSolarAnomaly = 18.30143135945036
var c = (1.9148 * sin(meanSolarAnomaly)) + (0.0200 * sin(2 * meanSolarAnomaly)) + (0.0003 * sin(3 * meanSolarAnomaly))
// = -1.01586743918389 - wrong answer
meanSolarAnomaly = meanSolarAnomaly * M_PI / 180.0
// Convert it to radians
c = (1.9148 * sin(meanSolarAnomaly)) + (0.0200 * sin(2 * meanSolarAnomaly)) + (0.0003 * sin(3 * meanSolarAnomaly))
// = 0.6134489282198807 - right answer

Given point of (latitude,longitude), distance and bearing, How to get the new latitude and longitude

I found a piece of code on web. It calculates the Minimum bounding rectangle by a given lat/lon point and a distance.
private static void GetlatLon(double LAT, double LON, double distance, double angle, out double newLon, out double newLat)
{
double dx = distance * 1000 * Math.Sin(angle * Math.PI / 180.0);
double dy = distance * 1000 * Math.Cos(angle * Math.PI / 180.0);
double ec = 6356725 + 21412 * (90.0 - LAT) / 90.0;
double ed = ec * Math.Cos(LAT * Math.PI / 180);
newLon = (dx / ed + LON * Math.PI / 180.0) * 180.0 / Math.PI;
newLat = (dy / ec + LAT * Math.PI / 180.0) * 180.0 / Math.PI;
}
public static void GetRectRange(double centorlatitude, double centorLogitude, double distance,
out double maxLatitude, out double minLatitude, out double maxLongitude, out double minLongitude)
{
GetlatLon(centorlatitude, centorLogitude, distance, 0, out temp, out maxLatitude);
GetlatLon(centorlatitude, centorLogitude, distance, 180, out temp, out minLatitude);
GetlatLon(centorlatitude, centorLogitude, distance, 90, out minLongitude, out temp);
GetlatLon(centorlatitude, centorLogitude, distance, 270, out maxLongitude, out temp);
}
double ec = 6356725 + 21412 * (90.0 - LAT) / 90.0; //why?
double ed = ec * Math.Cos(LAT * Math.PI / 180); // why?
dx / ed //why?
dy / ec //why?
6378137 is the equator radius, 6356725 is polar radius, 21412 =6378137 -6356725.
from the link, I know a little of the meanings. But these four lines, I don't know why. Could you please help to give more information? Could you please help to let me know the derivation of the formula?
From the link, in the section "Destination point given distance and bearing from start point", it gives another formula to get the result. What is the derivation of the formula?
From this link , I know the derivation of the Haversine Formula, it's very informative. I don't think the formula in the section of "Destination point given distance and bearing from start point" is just a simple reversion of Haversine.
Thanks a lot!
This is a prime example of why commenting your code makes it more readable and maintainable. Mathematically you are looking at the following:
double ec = 6356725 + 21412 * (90.0 - LAT) / 90.0; //why?
This is a measure of eccentricity to account for the equatorial bulge in some fashion. 21412 is, as you know, the difference in earth radius between the equator and pole. 6356725 is the polar radius. (90.0 - LAT) / 90.0 is 1 at the equator, and 0 at the pole. The formula simply estimates how much bulge is present at any given latitude.
double ed = ec * Math.Cos(LAT * Math.PI / 180); // why?
(LAT * Math.PI / 180) is a conversion of latitude from degrees to radians. cos (0) = 1 and cos(1) = 0, so at the equator, you are applying the full amount of the eccentricity while at the pole you are applying none. Similar to the preceding line.
dx / ed //why?
dy / ec //why?
The above seems to be the fractional additions to distance in both the x and y directions attributable to the bulge at any given lat/lon used in the newLon newLat computation to arrive at the new location.
I haven't done any research into the code snippet you found, but mathematically, this is what is taking place. Hopefully that will steer you in the right direction.
Haversine Example in C
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double m2ft (double l) { /* convert meters to feet */
return l/(1200.0/3937.0);
}
double ft2smi (double l) { /* convert feet to statute miles*/
return l/5280.0;
}
double km2smi (double l) { /* convert km to statute mi. */
return ft2smi(m2ft( l * 1000.0 ));
}
static const double deg2rad = 0.017453292519943295769236907684886;
static const double earth_rad_m = 6372797.560856;
typedef struct pointd {
double lat;
double lon;
} pointd;
/* Computes the arc, in radian, between two WGS-84 positions.
The result is equal to Distance(from,to)/earth_rad_m
= 2*asin(sqrt(h(d/earth_rad_m )))
where:
d is the distance in meters between 'from' and 'to' positions.
h is the haversine function: h(x)=sin²(x/2)
The haversine formula gives:
h(d/R) = h(from.lat-to.lat)+h(from.lon-to.lon)+cos(from.lat)*cos(to.lat)
http://en.wikipedia.org/wiki/Law_of_haversines
*/
double arcradians (const pointd *from, const pointd *to)
{
double latitudeArc = (from-> lat - to-> lat) * deg2rad;
double longitudeArc = (from-> lon - to-> lon) * deg2rad;
double latitudeH = sin (latitudeArc * 0.5);
latitudeH *= latitudeH;
double lontitudeH = sin (longitudeArc * 0.5);
lontitudeH *= lontitudeH;
double tmp = cos (from-> lat * deg2rad) * cos (to-> lat * deg2rad);
return 2.0 * asin (sqrt (latitudeH + tmp*lontitudeH));
}
/* Computes the distance, in meters, between two WGS-84 positions.
The result is equal to earth_rad_m*ArcInRadians(from,to)
*/
double dist_m (const pointd *from, const pointd *to) {
return earth_rad_m * arcradians (from, to);
}
int main (int argc, char **argv) {
if (argc < 5 ) {
fprintf (stderr, "Error: insufficient input, usage: %s (lat,lon) (lat,lon)\n", argv[0]);
return 1;
}
pointd points[2];
points[0].lat = strtod (argv[1], NULL);
points[0].lon = strtod (argv[2], NULL);
points[1].lat = strtod (argv[3], NULL);
points[1].lon = strtod (argv[4], NULL);
printf ("\nThe distance in meters from 1 to 2 (smi): %lf\n\n", km2smi (dist_m (&points[0], &points[1])/1000.0) );
return 0;
}
/* Results/Example.
./bin/gce 31.77 -94.61 31.44 -94.698
The distance in miles from Nacogdoches to Lufkin, Texas (smi): 23.387997 miles
*/
I assume 6356725 has something to do with the radius of the earth. Check out this answer, and also take a look at the Haversine Formula.

Trajectory Prediction in Sprite Kit

I'm trying to predict where an object (ball) will move using the following formula
Calculations for t = 1 second
y = VelocityY * t + 0.5*Gravity*t2
x = VelocityX * t
code below:
+(CGVector) getTrajectoryPointWithInitialPosition:(CGVector) initialPosition andInitialVelocity:(CGVector) initialVelocity andSteps: (CGFloat)n andSceneLayer: (SKScene*) sceneLayer
{
// Put data into correct units
CGFloat t = 1.0 / 60.0;
// m/s
CGVector stepVelocity = CGVectorMake(t * initialVelocity.dx, t * initialVelocity.dy);
// m/s^2
CGVector stepGravity = CGVectorMake(t * t * sceneLayer.physicsWorld.gravity.dx, t * t * sceneLayer.physicsWorld.gravity.dy);
initialPosition = CGVectorMake(initialPosition.dx + n * stepVelocity.dx,
initialPosition.dy + n * stepVelocity.dy + 0.5 * (n*n+n) * stepGravity.dy);
return CGVectorMake(initialPosition.dx + n * stepVelocity.dx,
initialPosition.dy + n * stepVelocity.dy + 0.5 * (n*n) * stepGravity.dy);
}
I then launch the ball (stationary/non-dynamic) using the following
CGVector aVelocity = CGVectorMake(initialVelocity.dx*17.5, initialVelocity.dy*17.5);
[ball.physicsBody setVelocity:aVelocity];
What I can't figure out is:
initialVelocity for the ball and the trajectory prediction is the same. If it's the same, why does multiplying initialVelocity for the ball by 17.5 gets the ball movement and the predicted trajectory obtained from above to match up. It looks like it's following the predicted path but I don't understand why multiplying the balls velocity by 17.5 makes the ball
![Before multiplying by 17.5] http://i.imgur.com/bKkPGmh.png - Before multiplying by 17.5
![After multiplying velocity by 17.5] http://i.imgur.com/Ae7sY4i.png - After multiplying by 17.5

How to get angle between two POI?

How do I calculate the angle in degrees between the coordinates of two POIs (points of interest) on an iPhone map application?
I'm guessing you try to calculate the degrees between the coordinates of two points of interest (POI).
Calculating the arc of a great circle:
+(float) greatCircleFrom:(CLLocation*)first
to:(CLLocation*)second {
int radius = 6371; // 6371km is the radius of the earth
float dLat = second.coordinate.latitude-first.coordinate.latitude;
float dLon = second.coordinate.longitude-first.coordinate.longitude;
float a = pow(sin(dLat/2),2) + cos(first.coordinate.latitude)*cos(second.coordinate.latitude) * pow(sin(dLon/2),2);
float c = 2 * atan2(sqrt(a),sqrt(1-a));
float d = radius * c;
return d;
}
Another option is to pretend you are on cartesian coordinates (faster but not without error on long distances):
+(float)angleFromCoordinate:(CLLocationCoordinate2D)first
toCoordinate:(CLLocationCoordinate2D)second {
float deltaLongitude = second.longitude - first.longitude;
float deltaLatitude = second.latitude - first.latitude;
float angle = (M_PI * .5f) - atan(deltaLatitude / deltaLongitude);
if (deltaLongitude > 0) return angle;
else if (deltaLongitude < 0) return angle + M_PI;
else if (deltaLatitude < 0) return M_PI;
return 0.0f;
}
If you want the result in degrees instead radians, you have to apply the following conversion:
#define RADIANS_TO_DEGREES(radians) ((radians) * 180.0 / M_PI)
You are calculating the 'Bearing' from one point to another here. There's a whole bunch of formula for that, and lots of other geographic quantities like distance and cross-track error, on this web page:
http://www.movable-type.co.uk/scripts/latlong.html
the formulae are in several formats so you can easily convert to whatever language you need for your iPhone. There's also javascript calculators so you can test your code gets the same answers as theirs.
If the other solutions dont work for you try this:
- (int)getInitialBearingFrom:(CLLocation *)first
to:(CLLocation *)second
{
float lat1 = [self degreesToRad:first.coordinate.latitude];
float lat2 = [self degreesToRad:second.coordinate.latitude];
float lon1 = [self degreesToRad:first.coordinate.longitude];
float lon2 = [self degreesToRad:second.coordinate.longitude];
float dLon = lon2 - lon1;
float y = sin (dLon) * cos (lat2);
float x1 = cos (lat1) * sin (lat2);
float x2 = sin (lat1) * cos (lat2) * cos (dLon);
float x = x1 - x2;
float bearingRadRaw = atan2f (y, x);
float bearingDegRaw = bearingRadRaw * 180 / M_PI;
int bearing = ((int) bearingDegRaw + 360) % 360; // +- 180 deg to 360 deg
return bearing;
}
For final bearing, simply take the initial bearing from the end point to the start point and reverse it (using θ = (θ+180) % 360).
You need these 2 helpers:
-(float)radToDegrees:(float)radians
{
return radians * 180 / M_PI;
}
-(float)degreesToRad:(float)degrees
{
return degrees * M_PI /180;
}