先设计点类(Point),该类的数据成员包括一个点的x坐标和y坐标,成员函数实现的

2024-11-29 18:25:16
推荐回答(3个)
回答(1):

class Point
{
int x;
int y;

public:
Point(){}
Point(int xx,int yy){x = xx;y = yy;}
~Point(){}
int X(){return x;}
int Y(){return y;}

};

class Line :public Point
{
Point Pa;
Point Pb;

public:
Line(){}
Line(Point pa,Point pb){Pa = pa;Pb = pb;};
float GetLineLengh();//获取直线长度
float GetLineAscent();//获取直线斜率
};

float Line::GetLineLengh()
{
int Lx = (abs(Pb.X()-Pa.X()))*(abs(Pb.X()-Pa.X()));
int Ly = (abs(Pb.Y()-Pa.Y()))*(abs(Pb.Y()-Pa.Y()));
return sqrt((float)(Lx+Ly));
}

float Line::GetLineAscent()
{
if(Pa.X() == Pb.Y())
{
cout<<"斜率不存在"< }
else return (float) (Pb.Y()-Pa.Y())/(Pb.X()-Pa.Y());
}
int main()
{
Point Pa(1,2);
Point Pb(1,5);
Line ab(Pa,Pb);
cout<<"直线长度为:"<return 0;
}

回答(2):

你问题说得不全啊,给你个简单的java例子,计算两点间的距离。
public class Point
{
private int x;
private int y;
public Point(){x=0;y=0;}
public Point(int x, int y){this.x = x;this.y = y;}
public void setX(int x){this.x = x;}
public void setY(int y){this.y = y;}
public int getX(){return x;}
public int getY(){return y;}
public double Distance(Point p)
{
int diffx = p.getX() - this.x;
int diffy = p.getY() - this.y;
return Math.Sqrt(diffx * diffx + diffy * diffy);
}
}

回答(3):

不懂说什么