04737 c++ 自学考试2019版 第二章课后程序设计题 3

/*
*	04737 c++ 自学考试2019版 第二章课后练习
*	程序设计题 3
*	需求:设计并实现一个类MyLine 
*/
//标准流 
#include<iostream>
//科学计算函数 
#include<cmath>
using namespace std;

class MyLine
{
	private:
		//点1 
		double p1_x,p1_y;
		//点2 
		double p2_x,p2_y;
	public:
		//construtor
		MyLine(double p1_x,double p1_y,double p2_x,double p2_y); 
		//直线的斜率 
		double slope();
		//点是否在线上
		bool isOnLine(double x,double y);
		//点到直线的距离
		double distance(double x,double y); 
};
MyLine::MyLine(double x1,double y1,double x2,double y2)
{
	p1_x = x1;
	p1_y = y1;
	p2_x = x2;
	p2_y = y2;
}
//斜率 公式:k=(y2-y1)/(x2-x1) 
double MyLine::slope()
{
	return (p2_y - p1_y)/(p2_x - p1_x);
}
//点是否在线上  y-beginY =k * (x - beginX)
bool MyLine::isOnLine(double x,double y)
{
	return (y-p1_y) ==(slope()*(x-p1_x));
}
//点到直线的距离 d = [AX0 + BY0 + C的绝对值]/[(A^2 + B^2)的算术平方根]
double MyLine::distance(double x,double y)
{
	double a = p2_y-p1_y;
	double b = p1_x-p2_x;
	double c = (p2_x*p1_y) - (p1_x*p2_y);
	return abs(a*x+b*y+c) / sqrt(a*a+b*b);
}

int main()
{
	MyLine test(3,3,8,8);
	cout<<test.slope()<<endl;
	cout<<test.isOnLine(6,6)<<endl;
	cout<<test.distance(6,6)<<endl;
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值