用c++计算直线定点到给定点直线距离

#include

#include

 

using namespace std;

 

class Point

{

 private:

  float x,y;

 public:

  Point(float,float);

  float getX();

  float getY();

};

 

Point::Point(float a,float b)

{

 x=a;

 y=b;

}

 

float Point::getX()

{

 return x;

}

 

float Point::getY()

{

 return y;

}

 

class MyLine

{

 private:

  float a,b;

 public:

  MyLine(float,float);

  void xielv(); //计算斜率 

  void ynInLine(Point); //判断点是否在线上

  void pointDistance(Point); //计算点到给定直线的距离 

};

 

MyLine::MyLine(float num1,float num2)

{

 a=num1;

 b=num2;

}

 

void MyLine::xielv()

{

 cout<<"斜率"<

}

 

void MyLine::ynInLine(Point p)

{

 float px,py;

 px=p.getX();

 py=p.getY();

 float ly;

 ly=a*px+b;

 if(py==ly) cout<<"给定点在线上"<

 else cout<<"给定点不在线上"<

}

 

void MyLine::pointDistance(Point p)

{

 /*

 若在坐标轴中,可以通过该点坐标(x0,y0)及直线解析式 ax+cy+b=0计算出距离

 ax+cy+b=0 → cy=-(ax+b) →c=-(ax+b)/y 

 计算公式为|ax0+cy0+b|/√(a^2+b^2) 

 */

 float distance,c;

 c=a*p.getX()+b;

 c=-c;

 c/=p.getY();

 distance=a*p.getX();

 distance+=c*p.getY();

 distance+=b;

 distance=abs(distance);

 distance/=sqrt(pow(a,2)+pow(b,2));

 cout<<"给定点到直线的距离为:"<

}

 

int main()

{

 Point point=Point(1,2);

 MyLine line=MyLine(1,1);

 line.xielv();

 line.ynInLine(point);

 line.pointDistance(point);

}

你可能感兴趣的:(c++)