计算四个点所构成的两条直线的夹角

编写一个c++程序,计算四个点所构成的两条直线的夹角
setprecision(n)描述:可以控制输出流显示浮点数的数字个数。C++默认的输出流数值有效位是6。
平面上给定4个点的坐标A B C D,分别表示直线AB和CD。坐标均为绝对值不超过100的整数。计算出这两条直线所成的锐角是多少度。(保留两位小数)
注意:当两直线平行或者重合,答案为0。


测试举例:
测试输入:0 0 1 0 0 0 0 1
预期输出:90.00
测试输入:16 34 21 87 98 23 12 9
预期输出:75.36

方法1:

#include<iostream>
#include<cmath>
#include<iomanip>
#define pi 3.1415926;
using namespace std;
int main()
{    
    double x1,y1,x2,y2,x3,y3,x4,y4;
    long double x,y;
    cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
    x=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    y=(x3-x4)*(x3-x4)+(y3-y4)*(y3-y4);
    long double s1,s2;
    s1=sqrt(x);
    s2=sqrt(y);
    long double s3;
    s3=(x2-x1)*(x4-x3)+(y2-y1)*(y4-y3);
    long double s4;
    s4=abs(s3);
    long double ans,ans2;
    ans2=s4/(s1*s2);
    ans=acos(ans2);
    long  double ans3;
    ans3=(180*ans)/pi;
    cout<<fixed<<setprecision(2)<<(long double)ans3;
    return 0;
}
 

方法2

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

const double PI = 3.14159265358979323846;

struct Point {
    double x, y;
};

double angleBetween(const Point& O, const Point& A) {

    if (A.x == O.x)
    {
        if (A.y <= O.y)
        {
            return -PI / 2.0;
        }
        else
        {
            return PI / 2.0;
        }
    }

    double cross = (A.y - O.y) / (A.x - O.x);

    if (A.x <= O.x)
    {
        return PI + atan(cross);
    }
    return atan(cross);
}

int main()
{
    double x1, y1, x2, y2, x3, y3, x4, y4;
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
    Point A = { x1,y1 };
    Point B = { x2,y2 };
    Point C = { x3, y3 };
    Point D = { x4, y4 };
    double ans1 = angleBetween(A, B);
    double ans2 = angleBetween(C, D);
    double ans3 = abs(ans2 - ans1);
    double ans4 = (180 * ans3) / PI;
    if (ans4 > 90) {
        ans4 = 180 - ans4;
    }
    cout << fixed << setprecision(2) << (double)ans4;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,我可以帮你编写一个C++程序来算两条直线夹角。假设你有四个,分别为P1(x1, y1)、P2(x2, y2)、P3(x3, y3)和P4(x4, y4)。我们可以使用向量的夹角公式来计算这个夹角。 首先,我们需要计算两条直线的向量表示。假设直线1由P1和P2构成直线2由P3和P4构成。则直线1的向量为V1(x2 - x1, y2 - y1),直线2的向量为V2(x4 - x3, y4 - y3)。 接下来,我们可以使用向量的内积公式来计算两个向量的内积,即V1·V2 = |V1| * |V2| * cosθ,其中θ为两个向量的夹角。 最后,我们可以通过解方程cosθ = (V1·V2) / (|V1| * |V2|)来计算夹角θ,并将其转换为角度制。 下面是一个示例程序: ```cpp #include <iostream> #include <cmath> double calculateAngle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { // 计算向量V1和V2 double v1x = x2 - x1; double v1y = y2 - y1; double v2x = x4 - x3; double v2y = y4 - y3; // 计算V1和V2的内积 double dotProduct = v1x * v2x + v1y * v2y; // 计算V1和V2的模 double v1Norm = sqrt(v1x * v1x + v1y * v1y); double v2Norm = sqrt(v2x * v2x + v2y * v2y); // 计算夹角θ double cosTheta = dotProduct / (v1Norm * v2Norm); double theta = acos(cosTheta); // 将弧度转换为角度制 double angle = theta * 180.0 / M_PI; return angle; } int main() { double x1, y1, x2, y2, x3, y3, x4, y4; std::cout << "请输入四个坐标(x1, y1, x2, y2, x3, y3, x4, y4):" << std::endl; std::cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; double angle = calculateAngle(x1, y1, x2, y2, x3, y3, x4, y4); std::cout << "两条直线夹角为:" << angle << "度" << std::endl; return 0; } ``` 你可以按照提示输入四个坐标,程序将输出两条直线夹角。希望这能帮到你!如果有任何问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值