先设计点类(Point),该类的数据成员包括一个点的x坐标和y坐标,成员函数实现的
创建一个点类(Point),数据成员包括x和y坐标,成员函数包括用来给数据成员初始化的构造函数,设置和获取x,y坐标的函数,将坐标移动到指定点的函数,并在主类中对其进行测...
创建一个点类(Point),数据成员包括x和y坐标,成员函数包括用来给数据成员初始化的构造函数,设置和获取x,y坐标的函数,将坐标移动到指定点的函数,并在主类中对其进行测试。 as的 谢谢
展开
3个回答
展开全部
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<<"斜率不存在"<<endl;
}
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<<"直线长度为:"<<ab.GetLineLengh()<<"\t斜率为:"<<ab.GetLineAscent()<<endl;
return 0;
}
{
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<<"斜率不存在"<<endl;
}
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<<"直线长度为:"<<ab.GetLineLengh()<<"\t斜率为:"<<ab.GetLineAscent()<<endl;
return 0;
}
展开全部
你问题说得不全啊,给你个简单的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);
}
}
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);
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询