Convert from Radians to Degrees Wrong - objective-c

I need to convert radians to degrees. I have tried this by x*180/Pi the problem is my output angles are in degrees minutes seconds so the small error in conversion leads to big problems.
For example: (aCos(0)*180)/Pi = 90.00000250
In the base 10 universe it should by 90.00000000

Use the following macro in your code to convert radians to degrees:
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
Example:
NSLog(#"Output radians as degrees: %f", RADIANS_TO_DEGREES(0.785398));
via this link.

The right solution is to import GLKit
Then you can use the following 2 functions already implemented by GLKit :
GLKMathDegreesToRadians()
GLKMathRadiansToDegrees()
With GLKit the values returned by the functions are good !
Objective-C :
import <GLKit.h>
float angleInRadian = GLKMathDegreesToRadians(360.0);
// Return 6.2831854820
float angleInDegrees = GLKMathRadiansToDegrees(angleInRadian);
// Return 360.0000000000
Swift :
import GLKit
let angleInRadian: Float = GLKMathDegreesToRadians(360.0)
// Return 6.2831855
let angleInDegrees: Float = GLKMathRadiansToDegrees(angleInRadian)
// Return 360.0

The problem was I was using acosf(float) when I should have been using aces(double)

This worked for me. You can also #import <GLKit/GLKit.h>
and use GLKMathDegreesToRadians() or GLKMathRadiansToDegrees().
-(CGFloat) degreesToRadians:(CGFloat) degrees {
return degrees == 0.0 ? 0: degrees * M_PI / 180;
}
-(CGFloat) radiansToDegrees:(CGFloat) radians {
return radians == 0.0 ? 0: radians * 180 / M_PI;
}

Related

Swift let computed property to Objective C syntax

I have this code in a Swift application and was curious of what its equivalent syntax would be in Objective C
typealias Signal = (Float) -> (Float)
static let sine: Signal = { (time: Float) -> Float in
return amplitude * sin(2.0 * Float.pi * frequency * time)
}
I believe I would declare Signal as follows:
typedef float (^Signal)(float);
but I am not sure how I would setup a similar way of setting up the syntax to retrieve the value. I thought about a class method but the didn't quite work out.
Thank you
This is not a computed property. This is a “closure”.
So this defines a type alias for a closure that takes a Float as a parameter and returns a Float:
typealias Signal = (Float) -> (Float)
You can create an instance of this Signal closure like so:
let doubler: Signal = { $0 * 2 }
And you can call that closure like so:
print(doubler(21)) // 42
The equivalent Objective-C syntax to define the type for a “block”:
typedef float (^Signal)(float);
To create an instance of a Signal block:
Signal doubler = ^(float input) {
return input * 2;
};
And to call it:
NSLog(#"%f", doubler(21)); // 42

What is the Modulo function for negative decimals Objective-C?

I need to calculate modulos with decimals that can be negative as well
for example: fmod( -5.2, 3 );
while mod() works with integers, and fmod() (or fmodf()) works well with decimals, fmod() returns wrong results with negative decimals:
ex:
double modulo = fmod (5.2, 3);
NSLog (#"==> %f", modulo);
==> 2.2 // This is correct !!
double modulo = fmod (-5.2, 3);
NSLog (#"==> %f", modulo);
==> -2.2 // This is wrong !! should be 0.8
Is there another mod() in the library or should i write my own decimal negative mod function ?
something like :
if (res = fmod(x,m) < 0) {
res+=m;
}
Thx !
-2.2 is correct and is also -5.2 mod 3. The fmod function is a C function (and therefore also Objective C), so you can find more detail about it by typing man fmod into terminal. When doing fmod it will preserve the sign of the value that you are moding. So to get the mod you want, you will need to check the sign (of either the result, or the value you are passing in) and if it is negative you will need to add the modulo base, in this case 3.
This is the definition of the fmod function:
double
fmod(double x, double y);
Specifically, the functions return the value x-i*y, for some integer i such that, if y is non-zero, the result has the same sign as x and magnitude less than the magnitude of y.
from the OS X man page.
For your purposes, you can do something like this:
#include <math.h>
float f_mod(float a, float n) {
return a - n * floor(a / n);
}
Of course, be careful to check n>0.
f_mod(-5.2f, 2.0f) = 0.8
f_mod(5.2f, 2.0f) = 2.2
Thank you so i ended up writing a wrapper... What i was hopping i could avoid. This works great for me, and, in my opinion, represents the correct mathematical definition of the modulo (not the C implementation). I am sure this function can be optimized,but for clarity i leave it this way:
//--
//-- Modulo
//--
double calcModulo ( double x, double m) {
double res = INFINITY;
if (m==0)
return res ;
double posMod, negMod, posM, posX;
posM = m < 0 ? -m:m;
posX = x < 0 ? -x:x;
posMod = fmod (posX, posM);
negMod = fmod (-posX,posM) + posM;
// pick up the correct res
if ( x >= 0 ){
if (m > 0) {
res = posMod;
} else {
res = -negMod;
}
}else{
if (m > 0) {
res= negMod;
} else{
res= -posMod;
}
}
return res;
}

Distance between point and finite line in objective-c

I've looked up some formulas relating to finding the distance a point and a line. On this page, I used example 14
http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
I have a method that has turned into this:
+(bool) checkPointNearBetweenPointsWithPointA:(CGPoint)pointA withPointB:(CGPoint)pointB withPointC:(CGPoint)pointC withLimit:(float)limit {
float A = pointB.x - pointA.x;
float B = pointA.y - pointC.y;
float C = pointA.x - pointC.x;
float D = pointB.y - pointA.y;
float dividend = fabs( A * B ) - ( C * D );
float divisor = sqrt(pow(A,2) + pow(D,2));
float distanceBetweenPointAndLine = dividend / divisor;
if(distanceBetweenPointAndLine < limit){
NSLog(#"distanceBetweenPointAndLine = %f",distanceBetweenPointAndLine);
return YES;
}
return NO;
}
The problem is that it still returns YES if I'm passed point B, if the line segment is drawn like B----A. Other screwed up things happen to depending on which angle the line is drawn. Just wondering if I need to consider anything else if testing to see if a point is near a finite line. Most examples I see online deal with lines of infinite length.
try my code below. line is considered to exist between points A & B (regardless of how you draw it B->A or A->B ) and point C is the point in consideration to measure the distance.
+ (bool) checkPointNearBetweenPointsWithPointA:(CGPoint)pointA
withPointB:(CGPoint)pointB
withPointC:(CGPoint)pointC
withLimit:(float)limit
{
CGFloat slopeLine = atan((pointB.y-pointA.y)/(pointB.x-pointA.x) );
CGFloat slopePointToPointA = -1 *atan((pointC.y-pointA.y)/(pointC.x-pointA.x));
CGFloat innerAngle = slopeLine + slopePointToPointA;
CGFloat distanceAC = sqrtf(pow(pointC.y-pointA.y,2) + pow(pointC.x-pointA.x,2));
CGFloat distanceBetweenPointAndLine = fabs(distanceAC * sin(innerAngle));
NSLog(#"distanceBetweenPointAndLine = %f",distanceBetweenPointAndLine);
NSLog(#"is exceeding limit ? %#",distanceBetweenPointAndLine > limit ? #"YES":#"NO");
if(distanceBetweenPointAndLine < limit)
{
return YES;
}
return NO;
}

Get the GPS coordinate given the current location, bearing and distance

I'm trying to get a GPS coordinate given the current location, bearing and distance.
But I can't find any related to cocoa-touch.
I've found the formula here Get lat/long given current point, distance and bearing
Is there an easier method for iOS?
Thanks in advance.
Converted the code in the link to Objective-C.
- (double)degreesToRadians:(double)degrees {
return degrees * M_PI / 180;
}
- (double)radiansToDegrees:(double)radians {
return radians * 180/ M_PI;
}
- (CLLocationCoordinate2D)remoteCoordinate:(CLLocationCoordinate2D)localCoordinate withDistance:(double)distance withBearing:(double)bearing {
double earthRadius = 6378.1; // Radius of Earth in kilometres.
double rLat1 = [self degreesToRadians:localCoordinate.latitude]; // Convert latitude to radians
double rLon1 = [self degreesToRadians:localCoordinate.longitude]; // Convert longitude to radians
double rLat2 = asinl( sinl(rLat1) * cosl(distance / earthRadius) + cosl(rLat1) * sinl(distance / earthRadius) * cosl(bearing) );
double rLon2 = rLon1 + atan2l( sinl(bearing) * sinl(distance/earthRadius) * cosl(rLat1), cosl(distance/earthRadius) - sinl(rLat1) * sinl(rLat2) );
double dLat2 = [self radiansToDegrees:rLat2]; // Convert latitude to degrees
double dLon2 = [self radiansToDegrees:rLon2]; // Convert longuitude to degrees
return CLLocationCoordinate2DMake(dLat2, dLon2);
}

CGFloat substraction problem

_screen.brightness = _screen.brightness - 0.1;
This line of code gives me an unexpected result.
When I call the NSLog(#"%.2f", _screen.brightness - 0.1); command, then it prints the
-0.00 value. When I test to this if (_screen.brightness == 0), it gives me NO.
Why this happens? Is there any conversion problem?
Here's my accessor methods in the class of _screen's object:
- (CGFloat)brightness {
return 1 - _dimmingView.alpha;
}
- (void)setBrightness:(CGFloat)brightness {
if (brightness < self.minValue || brightness > self.maxValue) {
return;
}
_dimmingView.alpha = 1 - brightness;
}
Floating point arithmetic doesn't necessarily give you the precise answers you're looking for. Better men than I have explained it here: C# float bug? 0.1 - 0.1 = 1.490116E-08. For a different language but the point remains the same.