如何求两直线夹角,用C++代码表示
时间: 2024-04-29 13:25:22 浏览: 116
我可以回答这个问题。两直线夹角可以通过向量的点乘公式求得。具体的C代码表示如下:
```
#include <stdio.h>
#include <math.h>
int main() {
double x1, y1, x2, y2, x3, y3, x4, y4;
printf("请输入两条直线的端点坐标(按照顺序输入):\n");
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
double vector1_x = x2 - x1;
double vector1_y = y2 - y1;
double vector2_x = x4 - x3;
double vector2_y = y4 - y3;
double dot_product = vector1_x * vector2_x + vector1_y * vector2_y;
double norm1 = sqrt(vector1_x * vector1_x + vector1_y * vector1_y);
double norm2 = sqrt(vector2_x * vector2_x + vector2_y * vector2_y);
double cos_angle = dot_product / (norm1 * norm2);
double angle = acos(cos_angle) * 180 / M_PI;
printf("两条直线夹角为:%.2lf度\n", angle);
return 0;
}
```
以上代码会先要求用户输入两条直线的端点坐标,然后利用向量的点乘公式计算两直线的夹角,并输出结果。
阅读全文