zoukankan      html  css  js  c++  java
  • 自考新教材-p90_5(3)

    源程序:

    #include <iostream>
    #include <math.h>
    using namespace std;
    class Point
    {
    private:
    float x, y;
    public:
    Point(float a, float b);
    float getX();
    float getY();
    void print();
    };
    Point::Point(float a, float b)
    {
    x = a;
    y = b;
    }
    float Point::getX()
    {
    return x;
    }
    float Point::getY()
    {
    return y;
    }
    void Point::print()
    {
    cout << "(" << "," << y << ")" << endl;
    }
    class Line
    {
    private:
    Point p1, p2;
    public:
    Line(Point &, Point &);
    friend float distance(Line &p, Point &q);

    double xielv()
    {
    return abs((p1.getY() - p2.getY())) / abs((p1.getX() - p2.getX()));
    }
    };
    Line::Line(Point &_p1, Point &_p2) :p1(_p1), p2(_p2)
    {
    //p1 = _p1;
    //p2 = _p2;
    }

    float distance(Line &p, Point &q)
    {
    float x1 = p.p1.getX();
    float y1 = p.p1.getY();
    float x2 = p.p2.getX();
    float y2 = p.p2.getY();
    float x = q.getX();
    float y = q.getY();
    return ((x - x1)*(y2 - y1) - (x2 - x1)*(y - y1)) / sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
    }
    int main()
    {
    Point p1(2, 5);
    Point p2(7, 9);
    Point p(0, 0);
    Line L(p1, p2);
    cout << "直线的斜率为:"<<L.xielv() << endl;
    cout << "点p到直线的距离为:"<<distance(L, p) << endl;
    system("pause");
    return 1;
    }

    运行结果:

  • 相关阅读:
    BZOJ 3677 连珠线
    BZOJ 3676 回文串
    BZOJ 3675 序列分割
    BZOJ 4013 实验比较
    BZOJ 4011 落忆枫音
    使用Google BBR加速 VPS
    OSX编译安装Python3及虚拟开发环境Virtualenv
    OSX安装Mysql8.0
    OpenSSL编程之摘要
    OpenCA搭建
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12269779.html
Copyright © 2011-2022 走看看