I have the following calculations:
let sinX = sin(150.0) //returns -0,71487
let cosY = cos(150.0) // returns 0,699250
But the real values for sinX = 0,5 and for cosY = -0,86.
Does anybody know where is the error?
The calculation is correct. However sin and cos take their param in radians, not degrees.
In calculus and most other branches of mathematics beyond practical
geometry, angles are universally measured in radians. One radian is
equal to 180/π degrees.
To convert from radians to degrees, multiply by 180/π.
https://en.wikipedia.org/wiki/Radian
And are you sure the sin & cos methods haven't been redefined by creating or overriding the default methods. That happens in programming. If so, you might want to re-check your operation.
Related
i need to calculate some expression for all angles from 0 to 90 degrees increments 10 degrees (of cause expression depends on some trigonometrical function).
It looks like:
for alpha = 0:10:90
func(alpha) = c * sin(alpha)
end
Who know how to work with degrees, tell, please
It should be:
for 0:pi/18:pi/2
I have ocean currents data (going towards). what would be the conversion I can use?
270-(atan2(zonal,meridional)(180/pi)) or 270-(atan2(meridional,zonal)(180/)) or anything entirely different?
I have gone through [this link][1] and also [eol][2] website. I still have no idea.
using unit circle and arctan function I tried to do, like for first quadrant zonal component(x) towards east-west is positive and meridioanl component(y) north-south is positive I used arctan(x,y) to find the direction.
then for 2nd quadrant 90+arctan(x,y) ???
3rd quadrant 180+arctan(x,y) ??
4th quadrant 270+arctan(x,y) ??
please correct me if I am wrong...
Not sure what are you exactly asking, but the function atan2 already gives you the correct quadrant, so no special tricks must be done adding or substracting angles to the result.
If you write help atan2 you will see that it expects two parameters: y and x (in this order) and returns you an angle in radians with a range [-pi,+pi]
Eg.
rad2deg(atan2(1,1)) gives you 45 as result
rad2deg(atan2(-1,1)) gives you -45 as result
rad2deg(atan2(1,-1)) gives you 135 as result
rad2deg(atan2(-1,-1)) gives you -135 as result
EDIT:
if you want only positive angles, just do:
angle = atan2(Y, X);
if (angle < 0)
angle = angle + 2*pi;
end
I write this code i Swifts Playground, but the result is wrong:
import UIKit
var degree:Double = 60
var result = cos(degree)
--
The result shall be 0.5 but Playground get me the answer = -0.9524129804151563.
If I choose 30 degrees the result will be = 0.154251449887584
What is wrong??
Trigonometric functions that take angles treat values as if they are expressed in radians, not degrees. When you pass 60, you get back cosine of 60 radians, not 60 degrees. To convert degrees to radians, multiply the value by π, and divide by 180.
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
In my cocos2d game, I have my player sprite and I want to have him move in the direction I tilt my iPhone. I can deal with that, the hardest bit which I can't work out is:
How do I make my sprite rotate to point in the direction I am tilting? This is represented very well in the 'Tilt to Live' game on the app store. I want controls just like that.
My sprite(for those unfamiliar with cocos2d) does have a rotation value if that helps.
Thanks.
If you don't like the above, here's a simpler way to get a reasonable result!
Hold the iPad in front of you, let LR be the left / right tilt, TA the towards you / away from you tilt. So LR runs from -90 to 90, TA from -90 to 90. (TA negative is leaning towards your belly.)
Display both those numbers on your screen, and move the device around, so you are certain you have that right to begin with. You won't be able to do anything until that is working.
The solutionAngle will be like a clock hand, clockwise, with 12 distant from you.
Go through this decision chain:
If both LR and TA is zero, the machine is flat. Act appropriately.
If LR is flat (0), the answer is either 0 or 180, depending on the sign of TA.
If TA is flat (0), the answer is either 90 or 270, depending on the sign of LR.
Otherwise:
adjustmentAngle = arctan( sin(TA) / sin(LR) )
// (NB, that should run from -90 to +90)
if ( LR > 0 ) finalResult = 90 - adjustmentAngle
if ( LR < 0 ), finalResult = 270 + adjustmentAngle
I think that will do it! Hope it helps!
IMO...... be sure to smooth the result over time, for a good feel.
.
setting the angle...
"the only thing I am unsure of currently (concerning your own idea) is how do I apply it to my player? Do I merely make the player rotation value equal to the adjustmentAngle?" .. hi Josh, yes simply set the rotation to the final angle you calculate using the above! Fortunately it's that simple.
If you ever have to convert back/fore between degrees and radians, just paste in these lines of code that everyone uses:
#include <math.h>
static inline float degreestoradians (double degrees) {return degrees * M_PI/180;}
static inline float radianstodegrees (double degrees) {return degrees * 180/M_PI;}
.
where are the axes?...
PS, here's the incredibly handy diagram you may want to bookmark:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAcceleration_Class/Reference/UIAcceleration.html
.
converting from accelerometer to angles...
"the accelerometer doesn't provide the raw data in angles. How do get from the raw data"
Quite right, I forgot to mention it sorry. This is an everyday problem...
distanceFactor = square root of (x^2 + y^2 + z^2)
angle X axis = acos ( x / distanceFactor )
angle y axis = acos ( y / distanceFactor )
angle z axis = acos ( z / distanceFactor) )
You must TEST this by writing the three angles on the screen and then moving it around, in other words "physically unit test" that section you write, before you proceed!
here is one of many answers from SO: UIAccelerationValue angle
BTW as you can probably see, you can get a rough result by taking the ratio of simply the raw x by raw y value, rather than the ratio of the two sines, in the 'adjustmentAngle' expression ... but anyway don't worry about that for now.
And finally!
IMPORTANT Readers should note that the amazing new Core Motion system, handles a lot of this for you, depending on your needs. Check it out!!!!!
Hope it helps!