赞
踩
最近都在摆烂,没有怎么好好学习,今天水一篇学校的作业来丢一下脸= =
明天重新做人!
编写一个程序,以友元函数方式计算一个点到一条直线的距离。
点到直线的距离d公式如下(不会打绝对值直接截图摆烂= =):
代码如下:
#include<iostream>
#include<math.h>
using namespace std;
class Point
{
public:
double x, y;//点的坐标
Point(double xx,double yy)
{
x = xx; y = yy;
}
};
class Line
{
friend void dist(Point p, Line l);//友元函数声明
private:
double a, b, c;//私有成员a b c表示直线方程ax+by+c=0
public:
Line(double aa, double bb, double cc)//公有函数调用私有成员
{
a = aa; b = bb; c = cc;
}
};
void dist(Point p, Line l)
{
double ans = abs((l.a * p.x + l.b * p.y + l.c) / sqrt(l.a * l.a + l.b * l.b));
cout << "点到直线的距离为:" <<ans<<endl;
}
int main()
{
double x, y, a, b, c;
cout << "请依次输入x,y,a,b,c的值:" << endl;
cin >> x >> y >> a >> b >> c;
Point p(x, y);
Line l(a, b, c);
dist(p, l);
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。