1
votes

I'm trying math.sin and math.cos to do a moving edge line in my program. I get all the math and it seems to add up properly (I use mouse location from a click and a moving mouse variable to determine the draw areas. I then use a width value to give it the edge offset.

I use math.cos to find how far in to offset the points in the X and Y locations. While doing this, the value of cos(Angle) seems to go absolutely crazy, from +1 to -1. Here's an example.

Angle = 29.153788462442

Cos as per window calculator = 0.8733152733324487151754721490934

Cos as per Math.Cos(Angle) = -0.637571130025255

I tried converting Angle to a float and an int in case the decimal place was causing it an issue, no avail.

This is the code I am using to bring up those answers.

lblInOpts.Text = Math.Cos(Angle).ToString() + " " + Angle.ToString();

The document is in degrees, switched over from radians and being use the same way MSDN seems to suggest.

4
lol.. cos on my calc 0,89696219391932871227674218328204 in decimal format.. cos on rad is -0,63757142570313453628037603161478, so you are looking at the wrong calc?gbianchi

4 Answers

8
votes

You need to convert from degrees to radians:

Math.Cos(Angle * Math.PI / 180.0)
2
votes

You are passing the angle in degrees, Math.Cos expects an angle in radians

2
votes

The result from the Windows Calculator is the cosine of the angle interpreted as degrees.

The result from your C# code is the cosine of the angle interpreted as radians, which is the equivalent of 1670.389... degrees.

To convert degrees to radians in your code, use

public static double ToRadians(double x) {
    return x * Math.PI / 180.0;
}
1
votes

-0.637571130025255 is the cosine of 29.153788462442 radians (1670.38903571 degrees). Check the documentation of Math.Cos() - does it take an argument in radians or degrees?