类的组合:点,线,三角形

类的组合:点,线,三角形

在这里插入图片描述
要求输入三点的坐标求三点各两点所围成线段长度和所围成三角形的周长和面积

#include<math.h>
#include    <iostream>
using    namespace    std;

class    myPoint
{
public:
    myPoint();
    myPoint(double    x, double    y);
    double    getX();
    double    getY();
private:
    double    mX, mY;
};
myPoint::myPoint()
{
    this->mX = 0;
    this->mY = 0;
}
myPoint::myPoint(double x, double y)
{
    this->mX = x;
    this->mY = y;
}
double myPoint::getX()
{
    return mX;
}
double myPoint::getY()
{
    return mY;
}


class Line
{
public:
    Line(myPoint& a,myPoint& b)
    {
        A = a; B = b;
    }
    Line(double x, double y)
    {
        this->x = x;
        this->y = y;
    }
    Line()
    {
        this->x = 0;
        this->y = 0;
    }
    double GetDistance()
    {
        double dx = A.getX() - B.getX();
        double dy = A.getY() - B.getY();
        return sqrt(dx * dx + dy * dy);
    }
private:
    myPoint A, B;
    double x=0, y=0;
};

class  Triangle
{
public:
    Triangle(myPoint& a, myPoint& b, myPoint& c):A(a, b), B(a, c), C(b, c)
    {
        
    }
    double getGirth()
    {
        double len;
        len = A.GetDistance() + B.GetDistance() + C.GetDistance();
        return len;
    }
    double getArea()
    {
        double len,s,t;
        len = A.GetDistance() + B.GetDistance() + C.GetDistance();
        s = len / 2.0;
        t = sqrt(s * (s - A.GetDistance()) * (s - B.GetDistance()) * (s - C.GetDistance()));
        return t;
    }
private:
    myPoint X, Y, Z;
    Line A, B, C;
};

int    main()
{
    double    x1, x2, x3, y1, y2, y3;
    cout << "请输入点1的x的值:";
    cin >> x1;
    cout << "请输入点1的y的值:";
    cin >> y1;
    cout << "请输入点2的x的值:";
    cin >> x2;
    cout << "请输入点2的y的值:";
    cin >> y2;
    cout << "请输入点3的x的值:";
    cin >> x3;
    cout << "请输入点3的y的值:";
    cin >> y3;
    cout << "点1的坐标为:(" << x1 << "," << y1 << ")" << endl;
    cout << "点2的坐标为:(" << x2 << "," << y2 << ")" << endl;
    cout << "点3的坐标为:(" << x3 << "," << y3 << ")" << endl;
    myPoint  p1(x1, y1), p2(x2, y2), p3(x3, y3);
    Line  line1(p1, p2);
    Line  line2(p2, p3);
    Line  line3(p3, p1);
    cout << "线1长度:" << line1.GetDistance() << endl;
    cout << "线2长度:" << line2.GetDistance() << endl;
    cout << "线3长度:" << line3.GetDistance() << endl;
    Triangle    t(p1, p2, p3);
    cout << "该三角形的周长为:" << t.getGirth() << endl;
    cout << "该三角形的面积为:" << t.getArea() << endl;
    return    0;
}

看完点个赞吧呜呜呜

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SHkko

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值