简体   繁体   English

坐标 C 的三角形角度

[英]Angle in triangle by coordinates C

i have to find angle ∠BAC in triangle ABC with given coordinates.我必须在给定坐标的三角形 ABC 中找到角度 ∠BAC。 I'm trying to find cos, then use function acos(cos) and get degrees(not radian) anwser.我正在尝试查找 cos,然后使用 function acos(cos) 并获取度数(不是弧度)anwser。 The input is Ax,Ay,Bx,By,Cx,Cy coordinates输入为 Ax,Ay,Bx,By,Cx,Cy 坐标

#include <math.h>

int main(){
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n;
    int ax,ay,bx,by,cx,cy;
    scanf("%d",&n);
    for(int i = 0; i<n; i ++){
        scanf("%d %d %d %d %d %d",&ax,&ay,&bx,&by,&cx,&cy);
        double cosab = (ax*bx+ay*by)/ (sqrt(ax*ax+ay*ay)* sqrt(bx*bx+by*by));
        cosab =180* acos(cosab)/M_PI;
        printf("cosab is %0.20g\n",cosab);
    }
    return 0;
}

With input有输入

8
2 1 2 3 5 5
2 1 4 3 2 3
3 1 3 5 2 3
0 0 1 0 10 0
0 0 1 0 -10 1
7 4 3 3 3 3
0 0 1 0 1 1e-5
0 0 1 0 1e-5 1

I get我明白了

cosab is 29.744881296942211
cosab is 10.304846468766044
cosab is 40.601294645004479
cosab is -1.#IND
cosab is -1.#IND
cosab is 15.255118703057764
cosab is -1.#IND
cosab is -1.#IND

But the anwser should be但答案应该是

36.869897645844019962
45
26.565051177077990019
0
174.28940686250035697
0
0.00057295779511172474814
89.999427042204885652

What is "-1.#IND" and what's wrong with my code?什么是“-1.#IND”,我的代码有什么问题?

As others have pointed out, the input coordinates are floating point, so let us read them into double s:正如其他人所指出的,输入坐标是浮点数,所以让我们将它们读入double s:

        double ax,ay,bx,by,cx,cy;
        /* Read coordinates of A, B, and C. */
        scanf("%lf%lf%lf%lf%lf%lf",&ax,&ay,&bx,&by,&cx,&cy);

∠BAC is the angle between lines AB and AC. ∠BAC 是 AB 线和 AC 线之间的夹角。 Let us consider those as vectors AB and AC represented by their x and y components:让我们将它们视为由xy分量表示的向量ABAC

        /* Convert to vectors AB and AC (as x and y components). */
        double abx = bx - ax;   /* x component of vector AB */
        double aby = by - ay;   /* y component of vector AB */
        double acx = cx - ax;   /* x component of vector AC */
        double acy = cy - ay;   /* y component of vector AC */

Now there is a nice formula using ATAN2 on the vector components to get the angle of rotation from one 2D vector to another 1 :现在有一个很好的公式,在向量分量上使用 ATAN2 来获得从一个 2D 向量到另一个1的旋转角度:

        /*
         * Get angle of rotation from vector AB to vector AC
         * in the range [-M_PI, +M_PI].
         */
        double rot_ab_ac = atan2(acy*abx - acx*aby, acx*abx + acy*aby);

We want the absolute angle, not a signed angle:我们想要绝对角度,而不是有符号角度:

        /* Convert to absolute angle BAC. */
        double bac = fabs(rot_ab_ac);

Finally, we want the angle in degrees, not radians:最后,我们想要以度为单位的角度,而不是弧度:

        /* Convert to degrees. */
        double bac_deg = bac * 180.0 / M_PI;

Note: M_PI is not defined in the C standard, but can be easily defined by a macro if necessary:注意: M_PI未在 C 标准中定义,但如果需要,可以通过宏轻松定义:

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

1 Angle Between Two Vectors 2D Formula 1两个向量之间的角度 2D 公式

Input is not only integers输入不仅是整数

1e-5 is like 0.00001 . 1e-5就像0.00001 Read that into a double .将其读入double

// int ax,ay,bx,by,cx,cy;
double ax,ay,bx,by,cx,cy;
...
    //scanf("%d %d %d %d %d %d",&ax,&ay,&bx,&by,&cx,&cy);
    scanf("%lf %lf %lf %lf %lf %lf",&ax,&ay,&bx,&by,&cx,&cy);

Be careful with acos()小心acos()

The |x| |x| in acos(x) may be just a tad larger than 1.0 due to computationally issues.由于计算问题, acos(x)中的值可能仅比 1.0 大一点。 Best to check最好检查

// add
if (cosab > 1.0) cosab = 1.0;
else if (cosab < -1.0) cosab = -1.0;

cosab = 180 * acos(cosab) / M_PI;

Perhaps better precision也许精度更高

// sqrt(ax*ax+ay*ay)
hypot(ax, ay)

Other issues may exist too其他问题也可能存在

Worth reviewing if proper formula is used: Law of cosines如果使用适当的公式,值得回顾:余弦定律

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM