My calculations show 75 * Cos(90) as zero. However, I get -22.46325...
I can confirm that it is NOT in radians because the radians value is -33.6055...
Here is what I have:
private static double RadianToDegree(double angle)
{
return angle * (180.0 / Math.PI);
}
static void Main(string[] args)
{
Console.WriteLine((75 * Math.Cos(RadianToDegree(90))).ToString());
}
Why am I getting a value of -22.46325 instead of 0?
input is taken as radians so you need to convert degrees to radians
Math.Cos expects inputs in radians, not degrees:
public static double Cos(double d)
Parameters
d
An angle, measured in radians.
You said
My calculations show 75 * Cos(90) as zero
so presumably you are working in degrees.
You therefore need to use
private static double DegreeToRadian(double angle)
{
return angle / (180.0 / Math.PI);
}
instead of
private static double RadianToDegree(double angle)
{
return angle * (180.0 / Math.PI);
}
to convert the angle into radians as Math.Cos expects.
See this link to an ideone.com snippet to confirm it.
The result is 4.59227382683391E-15 but since you're dealing with floating point numbers, the result of Math.Cos(x) will never be zero (see, e.g. this StackOverflow question).
You're passing in an argument to Math.Cos that your code suggests should be in degrees, while Math.Cos takes Radians.
See: https://msdn.microsoft.com/en-us/library/system.math.cos%28v=vs.110%29.aspx
You've got the conversion backwards:
private static double DegreesToRadians(double angle)
{
return angle * (Math.PI / 180.0 );
}
static void Main(string[] args)
{
Console.WriteLine((75 * Math.Cos(DegreeToRadians(90))).ToString());
}
Your RadianToDegree method given the argument of 90 returns value of 5156.62015617741. Which is passed to Math.Cos. Math.Cos expects the angle in radians as an argument. That 5156.62015617741 radians in degrees would be:
5156.62015617741 / PI * 180 = 295452.571501057 = 252.571501057
cos of 252.571501057 degrees is -0.299515394, times 75 in your code gives -22.463654606 that you see (plus-minus floating point error).
As others already mentioned, you got your transformation backwards.
Related
I'm trying to calculate distance of cannon shot using velocity and angle. I'm testing results using utility tests. The formula for range should be someting like v^2 * sin2a aka velocity squared * sin2alpha . As far as i know, sin2a is supposed to be 2*sina*cosa, but i may be wrong.
Anyway, whatever i do, i get wrong results, because it doesn't seem to be calculating sin.
Here's the code
Cannon.cs
public int CalculateDistance(int angle, int velocity)
{
int distance = 0;
double radian_angle = (Math.PI / 180) * angle;
distance_of_shot = (Math.Pow(velocity, 2)) * (2 * Math.Sin(radian_angle) * Math.Cos(radian_angle));
distance = (int)distance_of_shot;
return distance;
}
CannonAttackTest.cs
[TestMethod]
public void Calculations()
{
Canon new_canon = new Canon();
var data = new_canon.CalculateDistance(45, 450);
Assert.AreEqual(20682, data);
}
The results is suppose to be 20682, but i get 202500, which is exactly a number of squared 450...whichs points to sin not being calculated.
Any help is appreciated!
Thank you!
Check your units, you need to divide by the value of "g" because velocity is m/s and your "distance of shot" is in m^2/s^2.
distance_of_shot = (Math.Pow(velocity, 2)) * (2 * Math.Sin(radian_angle) * Math.Cos(radian_angle))/9.81;
You have a mistake sin 0.70710678118654746 and cos 0.70710678118654757 but after
(2 * Math.Sin(radian_angle) * Math.Cos(radian_angle)) result coming 1
Maths is not my strong suit and I think I have something mixed up here but I cannot figure out what.
I'm just trying to populate 2 new coordinates given a number of variables and constants.
if I make Origin coordinate 5,5 and Destination coordinate 10,5, I can work out that distance =5 and that the bearing from Origin to Destination is 90 using these two functions:
private static double GetDistance(PointF point1, PointF point2)
{
double a = (double)(point2.X - point1.X);
double b = (double)(point2.Y - point1.Y);
return Math.Sqrt(a * a + b * b);
}
public static double GetBearing(PointF coord1, PointF coord2)
{
double result = 0.0;
result = Math.Atan2(coord2.X - coord1.X, coord2.Y - coord1.Y) * (180 / Math.PI); //- Math.Atan2(coord4.y - coord3.y, coord4.x - coord3.x))
if (result < 0)
{
result = result + 360;
}
return result;
}
What I want to be able to do given an offset Distance of xd=1 and an offset bearing of 180(ie directly opposite direction to the destination) is plot the location 4,5. I'd also like to be able to feed a different offset bearing in of say 90 and plot 5,6.
Here's what I've tried but I get completely nonsensical values.
public static PointF CalculateCoordinate(double Angle, double Distance)
{
PointF coord = new PointF(Convert.ToSingle(Distance * Math.Cos(Angle)), Convert.ToSingle(Distance * Math.Sin(Angle)));
return coord;
}
and CalculateCoordinate(GetBearing(Destination, Origin),1) to reverse the bearing directly 180. I've tried this CalculateCoordinate(90,1) to calculate an offset to the side but that's not working either.
Where have I gone wrong, I'm sure it's something pretty stupid and simple.
There's two mistakes that I can see. First, Atan2 takes the Y value for the first parameter and the X value for the second:
Math.Atan2(coord2.Y - coord1.Y, coord2.X - coord1.X) * (180 / Math.PI);
Secondly, you're converting from radians to degrees in GetBearing, but you're not converting Angle from degrees to radians inside CalculateCoordinate e.g:
Math.Cos(Angle * (Math.PI / 180))
I have the following code:
double x = sw.bonePos[0, (int)Bones.HipCenter].x;
double z = sw.bonePos[0, (int)Bones.HipCenter].z;
double hypotenusePower2 = Math.Pow(x, 2) + Math.Pow(z, 2);
double hypotenuse = Math.Sqrt(hypotenusePower2);
double angle = Math.Asin(z / hypotenuse);
I know that x,z, hypotenuse are correct and z / hypotenuse is correct because its always between -1 and 1. So I want to find the angle using the ArcSin like this but when I am printing for example Math.Asin(1) the result is 1.5707...
Am I using the wrong function? Is there any function in C# that returns the angle?
Example of input/output:
x: -0.000844396417960525
z: 0.857428431510925
hypotenuse: 0.857428847292063
angle: 1.5698115260652
x: 0.0198930986225605
z: 0.849016189575195
hypotenus: 0.849249212854266
angle: 1.54736984845028
The result you get is correct - asin of 1 is half of π, or approximately 1.5707 radians.
Functions returning angles usually return the results in radians. If you need the result in degrees, you need to convert the result as follows:
double degrees = angle * ( 180 / Math.Pi );
That's the right answer. The resulting angle is measured in radians. Math.Asin(1) should therefore be equal to π/2 ≈ 1.5707 radians, which matches your result.
If you wanted the value in degrees, multiply by 180/π. In this case, π/2 * 180/π would give you 90 degrees:
double degrees = radians * (180 / Math.Pi);
I'm doing a (probably simple) task, in which i want to make a drawed object move to a user-controlled (drawed too). All i have is the players X and Y coördinate, defined as respectively Xp and Yp. The object that has to move (after trigger, not included in code down here) to the 'player-object' has its coördinates defined in this.X and this.Y.
int xDirection = Xp - this.X;
int yDirection = Yp - this.Y;
int angleInDegrees = (int)Math.Atan2(xDirection, yDirection);
double radians = (Math.PI / 180) * angleInDegrees;
double xTmp = 3 * Math.Cos(radians);
int xSpeed = (int)xTmp;
double yTmp = 3 * Math.Sin(radians);
int ySpeed = (int)yTmp;
Console.WriteLine(xDirection);
Console.WriteLine(yDirection);
Console.WriteLine(xSpeed);
Console.WriteLine(ySpeed);
Console.ReadLine();
This doesn't give me the right figures, so i was wondering what may be wrong.
The toughest bit about this probably the fact that the object that has to move to the playerobject may be approached from all the sides (360 degrees) but there's no angle of approach available.
I hope to be complete with my question,
Tim
I'm betting the main problem you're seeing is this line:
int angleInDegrees = (int)Math.Atan2(xDirection, yDirection);
As #catflier mentioned, Math.Atan2 returns the angle in radians (so a number ranging from 0 to 2pi). However, you perform a cast to int which will truncate the decimal places. So if your angle was at 45 degrees, that's actually returning ~0.785398 radians. A cast to int will turn it into 0. Similarly, at 90 degrees, that's ~1.570796 radians, a cast to int will result in 1. That's significant round-off error. As I mentioned in my comment, consider changing all your types to doubles and only perform integer casts at the last point possible (I suppose your objects are positioned based on integers).
Math.Atan2 returns a value in radians, so are other c# trigonometric functions.
double angle = Math.Atan2(yDirection, xDirection);
Also make sure to force type casts to decimals:
3.0 * Math.Cos(radians);
at http://www.teacherschoice.com.au/Maths_Library/Trigonometry/solve_trig_SSS.htm
there is
"Find the inverse cos of -0.25 using a scientific calculator...C = cos-1(-0.25)= 104.478º "
and
"Find the inverse sin of 0.484123 using a scientific calculator...A = sin-1(0.484123)= 28.955º "
I am trying to do this in c# , so i am trying the following
double mycalc = Math.Asin(0.484123) ;
double mycalc2 = Math.Acos(-0.25);
double mycalc99 = Math.Pow(Math.Acos(-0.25), -1); // or Math.Cos
double mycalc66 = Math.Pow(Math.Asin(0.484123), -1) ; // or Math.Sin
What steps am I missing?
Should I use DegreeToRadian function?
Using calculator net scientific-calculator.html
0.484123 asin does equal 28.955029723
-0.25 acos does equal 104.47751219
So what is missing?
cos-1 means the inverse function of cos. It does not mean cos raised to the power -1. (similar thing with sin) (more info)
Asin and Acos return the angle in Radians, you have to convert it to Degrees.
You should use :
double mycalcInRadians = Math.Asin(0.484123);
double mycalcInDegrees = mycalcInRadians * 180 / Math.PI;
According to the documentation, Asin and Acos definitely return in radians.
Multiply the return value by 180/Math.PI to convert from radians to
degrees.
Yes, After a sleep it became obvious.
I needed RadianToDegree.
private double DegreeToRadian(double angle)
{
return Math.PI * angle / 180.0;
}
private double RadianToDegree(double angle)
{
return angle * (180.0 / Math.PI);
}