include "math.h"
class point
{
public:
float x;
float y;
//建構函式
point(float x,float y)
this->x=x;
this->y=y;
}
};
class line
float a;
float b;
float c;
line(float a,float b,float c)
this->a=a;
this->b=b;
this->c=c;
float getDistance(point p)
float x,y;
x=p.x;
y=p.y;
return fabs(a*x+b*y+c)/sqrt(a*a+b*b);
void main()
cout<<"請輸入點的X座標:";
cin>>x;
cout<<"請輸入點的Y座標:";
cin>>y;
point p(x,y);
float a,b,c;
cout<<"請輸入a:";
cin>>a;
cout<<"請輸入b:";
cin>>b;
cout<<"請輸入c:";
cin>>c;
line l(a,b,c);
cout<<"點到直線的距離為:"<<l.getDistance(p)<<endl;
include "math.h"
class point
{
public:
float x;
float y;
//建構函式
point(float x,float y)
{
this->x=x;
this->y=y;
}
};
class line
{
float a;
float b;
float c;
public:
//建構函式
line(float a,float b,float c)
{
this->a=a;
this->b=b;
this->c=c;
}
float getDistance(point p)
{
float x,y;
x=p.x;
y=p.y;
return fabs(a*x+b*y+c)/sqrt(a*a+b*b);
}
};
void main()
{
float x,y;
cout<<"請輸入點的X座標:";
cin>>x;
cout<<"請輸入點的Y座標:";
cin>>y;
point p(x,y);
float a,b,c;
cout<<"請輸入a:";
cin>>a;
cout<<"請輸入b:";
cin>>b;
cout<<"請輸入c:";
cin>>c;
line l(a,b,c);
cout<<"點到直線的距離為:"<<l.getDistance(p)<<endl;
}