Is there away to Find the angle in 360ْ dgree?

时间:2018-03-23 00:38:25

标签: c#

I want to find an angle, similar to the one in the image. I have the point of the beginning of the line. I'm trying to find the angle form this formula: angle=Atan|(m2-m1)/(1+m1*m2)|, but I'm getting the wrong result

Here's my code:

float slope1 = (259 - 177) / (518 - 482);
float slope2 = (700 - 259) / (1244 - 518);

double abs = Math.Abs((slope2 - slope1) / (1 + slope2 * slope1));
double Thita = Math.Atan(abs);

enter image description here

2 个答案:

答案 0 :(得分:1)

如果你使用double而不是float,它会更好。像这样,

double slope1 = (259 - 177) / (double)(518 - 482);
double slope2 = (700 - 259) / (double)(1244 - 518);

double abs = Math.Abs((slope2 - slope1) / (1 + slope2 * slope1));
double Thita = Math.Atan(abs);

答案 1 :(得分:0)

在除数处添加(浮点数),你正在进行int div截断。 print slope1,slope2 to debug。

float slope1 = (259 - 177) / (float)(518-482); float slope2 = (700 - 259) / (float)(1244 - 518);

相关问题