Sau*_*ava 2 c c++ math atan2 computational-geometry
我在第一象限有两个点 A(X,Y) 和 B(P,Q)。还有一点C(L,M)。如何在顺时针方向找到 CA 和 CB 之间的角度?
我搜索了很多,所有的解决方案都使用了 atan2() 但它找到了相对于 x 轴的原点角度。
可以假设 C 和 A 是固定的。B 可以在第一象限的任何地方。角度必须是顺时针方向且在 0-360(或 0 到 360-1)范围内。
我在 C/C++ 中这样做。
编辑:为每个请求添加代码。这有点不同,因为我陷入了一个概念并需要对其进行澄清。如果点 x,y 位于 50,50 和 P 之间,此函数应该返回。 P 是相对于 CA 的角度。
bool isInsideAngle(long double x,long double y, long double p)
{
if((atan2(y,x) >= atan2(50,100)) && (atan2(y,x) <= (p * PI / 50)))
{
// cout<<"YES!";
// cout<<" atan2(y,x) = " <<atan2(y,x)*180/PI<<endl;
// cout<<" atan2(50,50) = " <<atan2(50,100)*180/PI<<endl;
// cout<<" (p * PI / 50) = "<<(p * PI / 50)*180/PI<<endl;
return true;
}
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
如何在顺时针方向找到 CA 和 CB 之间的角度?
// The atan2 functions return arctan y/x in the interval [?? , +?] radians
double Dir_C_to_A = atan2(Ay - Cy, Ax - Cx);
double Dir_C_to_B = atan2(By - Cy, Bx - Cx);
double Angle_ACB = Dir_C_to_A - Dir_C_to_B;
// Handle wrap around
const double Pi = acos(-1); // or use some ? constant
if (Angle_ACB > Pi) Angle_ACB -= 2*Pi;
else if (Angle_ACB < -Pi) Angle_ACB += 2*Pi;
// Answer is in the range of [-pi...pi]
return Angle_ACB;
Run Code Online (Sandbox Code Playgroud)