计算点到直线的距离。首先设计一个点类Point,它有2 个私有数据成员x和y,表示点的坐标。另一个类为直线类Line,它有3 个私有数据成员a,b和c,表示直线方程ax+by+c= 0。这两个类中都说明了一个友元函数dist,用于计算一个点到直线的距离。点(x.y)到直线ax+by+c=0的距离d的计算公式如下:
语法要点: 友元函数的特点。 这是一个编程题模板。请在这里写题目描述。例如:本题目要求读入2个整数A和B,然后输出它们的和。
输入格式:
输入两行,第一行输入两个整数,表示点坐标x,y的值 在第二行中输入直线三个参数,表示直线方程的三个洗漱a,b,c.
输出格式:
计算点到直线的距离保留两位小数。
输入样例:
在这里给出一组输入。例如:
5 5
2 4 3
输出样例:
在这里给出相应的输出。例如:
The distance is: 7.38
代码实现:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
class Point;
class line;
class Point
{
double x;
double y;
public:
Point(double x0=0, double y0=0)
{
x = x0;
y = y0;
}
friend double dist(Point &P,line &L);
};
class line
{
double a, b, c;
public:
line(double a0=1, double b0=1, double c0=1)
{
a = a0;
b = b0;
c = c0;
}
friend double dist(Point &P,line &L);
};
double dist(Point &P,line &L)
{
double d;
d = abs(P.x*L.a + P.y*L.b + L.c)/sqrt(L.a*L.a + L.b*L.b);
return d;
}
int main()
{
double x, y;
double a, b, c;
cin >> x >> y;
cin >> a >> b >> c;
Point P(x, y);
line L(a, b ,c);
if ((dist(P,L)==0)) cout << "The distance is: 0";
else cout << "The distance is: "<< fixed << setprecision(2) << dist(P,L);
return 0;
}