类和对象-练习:7-1 计算点到线的距离(友元)

ThisisatechnicalarticleaboutimplementingthedistancecalculationfromapointtoalineusingC++friendfunctionsandclasses,withthreedifferentapproaches.
摘要由CSDN通过智能技术生成

计算点到直线的距离。类定义的基本要求:

  1. 定义一个点类Point,包含有2 个私有数据成员x和y,表示点的坐标;一个构造函数。
  2. 定义一个直线类Line,包含有3 个私有数据成员a,b和c,表示直线方程ax+by+c= 0;一个构造函数。

说明:

计算点(x,y)到直线ax+by+c=0的距离d的计算公式如下:

00.bmp

实现要求:

应用友元函数/类完成。要求分别用如下三种方式完成:

方法一:定义全局函数(非成员函数)dist,并将其声明为Point和Line的友元函数,通过调用dist函数完成题目计算点到线距离的要求。
方法二:为Line定义成员函数dist,并将其声明为类Point的友元函数,通过Line的dist成员函数完成题目计算点到线距离的要求。
方法三:为Line定义成员函数dist,并声明Line类为Point类的友元类,通过Line的dist成员函数完成题目计算点到线距离的要求。

输入格式:

输入两行
第一行输入两个实数,表示点坐标x,y的值;
在第二行中三个实数,表示直线方程的三个系数a,b,c,题目保证a和b不为0.

输出格式:

输出点到直线的距离(保留两位小数)。

输入样例:

在这里给出一组输入。例如:

1.1 2.3
2 3.4 5.6

输出样例:

在这里给出相应的输出。例如:

The distance is: 3.96

方法一: 

#include<iostream>
#include <cmath> //使用abs(绝对值)函数和sqrt(平方根)函数的头文件
#include <iomanip> //使用 << fixed << setprecision(2) << 来保留两位小数
using namespace std;
class Point; //提前声明Point类和Line类
class Line;
double dist(const Point& p, const Line& L);
class Point
{
private:
    double _x;
    double _y;
public:
    void setxy(const double& x, const double& y)
    {
        _x = x;
        _y = y;
    }
    friend double dist(const Point& p, const Line& L);
};
class Line
{
private:
    double _a;
    double _b;
    double _c;
public:
    void setabc(const double& a, const double& b, const double& c)
    {
        _a = a;
        _b = b;
        _c = c;
    }
    friend double dist(const Point& p, const Line& L);
};
double dist(const Point& p, const Line& L)
{
    return (abs((L._a * p._x) + (L._b * p._y) + L._c) / sqrt(L._a * L._a + L._b * L._b));

}
int main()
{
    double x, y;
    double a, b, c;
    cin >> x >> y;
    cin >> a >> b >> c;
    Point p;
    Line L;
    p.setxy(x, y);
    L.setabc(a, b, c);
    cout << "The distance is: " << fixed << setprecision(2) << dist(p, L) << endl;
}

方法二:

 

#include<iostream>
#include <cmath>
#include <iomanip>
using namespace std;
class Point;//在Point类中声明Line类的友元,需要提前声明Point类
class Line
{
private:
    double _a;
    double _b;
    double _c;
public:
    void setabc(const double& a, const double& b, const double& c)
    {
        _a = a;
        _b = b;
        _c = c;
    }
    double dist(const Point& p);
};
class Point
{
private:
    double _x;
    double _y;
public:
    void setxy(const double& x, const double& y)
    {
        _x = x;
        _y = y;
    }
    friend double Line::dist(const Point& p);
};
double Line :: dist(const Point& p)
{
    return (abs((_a * p._x) + (_b * p._y) + _c) / sqrt(_a * _a + _b * _b));
}
int main()
{
    double x, y;
    double a, b, c;
    cin >> x >> y;
    cin >> a >> b >> c;
    Point p;
    Line L;
    p.setxy(x, y);
    L.setabc(a, b, c);
    cout << "The distance is: " << fixed << setprecision(2) << L.dist(p) << endl;
}

方法三和方法二差不多。 

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值