点类的实现(V1.0版)

                                                                          平面直角坐标系中的点类的实现

1:二维空间的点Point类:

class Point//点类的声明
{
protected:
    int x, y;//定义点(整型)
public:
    Point(int a=0, int b=0);
    void ShowPoint();
    int GetX();//对外接口函数
    int GetY();//对外接口函数
    void SetXY(int a,int b);//设置点坐标
};

2:定义直线类Line(由点生成,所以可以继承点的东西):

class Line: public Point
{
private:
    Point p; //线段的一个端点
    int a,b,c;
public:
    Line(Point &,Point &);
    double Length();//线段的长度
    void ShowLine();//(显示直线)(只能显示从哪个点到哪个点)
    double DistanceToPoint(Point&);//点到直线的距离
    void SetLine( Point &,  Point &);//设置直线(任意给定两点)
    bool Judge_point(Point &);//判断点是否在直线上
    Point intersection(Line &);//求两直线的交点
    double distance_lines(Line &);//求两直线间的距离
};


运行结果:



代码:

#include <iostream>
#include <cmath>
using namespace std;
class Point
{
protected:
    int x, y;
public:
    Point(int a=0, int b=0);
    void ShowPoint();
    int GetX();
    int GetY();
    void SetXY(int a,int b);
};
Point::Point(int a, int b)
{
    x=a;
    y=b;
}
void Point::ShowPoint( )
{
    cout<<"Point:("<<x<<','<<y<<")";
}
int Point::GetX( )
{
    return x;
}
int Point::GetY( )
{
    return y;
}
void Point::SetXY(int a, int b)
{
    x=a;
    y=b;
}
class Line: public Point
{
private:
    Point p; //线段的一个端点
    int a,b,c;
public:
    Line(Point &,Point &);
    double Length();//线段的长度
    void ShowLine();
    double DistanceToPoint(Point&);
    void SetLine( Point &,  Point &);
    bool Judge_point(Point &);
    Point intersection(Line &);
    double distance_lines(Line &);
};
Line::Line(Point &p1, Point &p2):Point(p1),p(p2)
{
    int dx=p2.GetX()-p1.GetX();
    int dy=p2.GetY()-p1.GetY();
    a=-dy;
    b=dx;
    c=p1.GetX() *dy - p1.GetY()*dx;
}
double Line::Length()
{
    int dx=p.GetX()-this->x;
    int dy=p.GetY()-this->y;
    return sqrt(dx*dx+dy*dy);
}
void Line::ShowLine()
{
    cout<<"From ";
    ShowPoint();
    cout<<" to ";
    p.ShowPoint();
}
double Line::DistanceToPoint(Point& p)
{
    return abs(a*p.GetX()+b*p.GetY()+c)/(sqrt(a*a+b*b));
}
void Line::SetLine( Point & p1,  Point & p2)
{
    this->x = p1.GetX();
    this->y = p1.GetY();
    p = p2;
    int dx=p2.GetX()-p1.GetX();
    int dy=p2.GetY()-p1.GetY();
    a=-dy;
    b=dx;
    c=p1.GetX() *dy - p1.GetY()*dx;
}
bool Line::Judge_point(Point &p)
{
    int y=a*p.GetX()+b*p.GetY()+c;
    if(y==0)
        return true;
    else
        return false;
}
double Line::distance_lines(Line &l)
{
    return double(fabs(this->c-l.c))/(sqrt(a*a+b*b+0.0));
}
Point Line::intersection(Line &l)
{
    if((this->a*l.b==this->b*l.a)&&(this->b*l.c==this->c*l.b))
    {
        cout<<"两直线平行,无交点(默认为(0,0)点)"<<endl;
        return Point(0,0);
    }
    else
    {
        return  Point((this->a*l.c-l.a*this->c)/(l.a*this->b-this->a*l.b),(l.c*this->b-l.b*this->c)/(l.b*this->a-l.a*this->b));
    }
}
int main()
{
    Point p1(0,0),p2(0,30),p3(40,40),p4(100,100),p5;
    Line li1(p2,p3),li2(p1,p4);
    cout<<"point1: ";
    p1.ShowPoint();
    cout<<endl;
    cout<<"line1: ";
    li1.ShowLine();
    cout<<endl;
    cout<<"line2: ";
    li2.ShowLine();
    cout<<endl;
    cout<<"Distance of point1 to line1: ";
    cout<<li1.DistanceToPoint(p1)<<endl;
    p5=li1.intersection(li2);
    cout<<"The intersection of line1 and line2's : ";
    p5.ShowPoint();
    return 0;
}


注:由于代码未经严格测试,所以发现bug请转告与我,或者有更好的设计也请告知我,共同交流下,谢谢。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值