用c++计算两个坐标的距离

该代码定义了一个Point类,包含横纵坐标属性,并实现了输入两点坐标及计算两点间距离的功能。通过使用库中的sqrt()和pow()函数,计算两点(x1,y1)和(x2,y2)之间的距离公式sqrt((x2-x1)^2+(y2-y1)^2)。
摘要由CSDN通过智能技术生成

#define _CRT_SECURE_NO_WARNINGS
/*4、编写程序sys13 - 4.c,请编写一个Point类,要求类中属性为:横坐标x,纵坐标y;行为为:从键盘输入两个点的坐标;求两点之间的距离。


class Point
{
private:float x, y;
public:
    void set_xy(float  a, float  b);
    float distance_xy(Point  &p1);
};
①    点A(x1, y1)和点B(x2, y2)两点间的距离公式:
②    distance_xy()函数中可用<cmath>头文件中的sqrt()函数和pow()函数。
sqrt(参数x)  //表示求参数x的的平方根;
pow(参数x, 参数y)  //表示求参数x的y次方的值。
}*/
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
public :
    void setxy(float a, float b);
    float distancexy(Point &p1,Point &p2);
private:float x, y;
};
int main()
{
    float x1,y1,x2,y2;
    Point p1, p2;
    cout << "please input first point:" << endl;
    cin >> x1 >> y1;
    cout << "please input second point:" << endl;
    cin >> x2 >> y2;
    p1.setxy(x1,y1);
    p2.setxy(x2,y2);
    float dis;
    dis = p1.distancexy(p1, p2);
    cout << "两点之间的距离是:" << dis << endl;
    return 0;
}
void Point::setxy(float a,float b)
{
    x = a, y = b;
}
float Point::distancexy(Point &p1, Point &p2)
{
    return  sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}
 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

曼诺尔雷迪亚兹

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

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

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

打赏作者

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

抵扣说明:

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

余额充值