根据四个点求两条直线的交点

linear.h头文件的内容

#ifndef LINEAREQUATION_H
#define LINEAREQUATION_H

#include<iostream>
using namespace std;

class LinearEquation
{
private:
    double a, b, c, d, e, f;//six variables for linear equation

public:
    //constructor for the six variables
    LinearEquation(double theA, double theB, double theC, double theD, double theE, double theF)
    {
        a = theA;
        b = theB;
        c = theC;
        d = theD;
        e = theE;
        f = theF;
    }

    //constructor for intersection
    LinearEquation(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
    {
        //For first line
        a = y2 - y1;
        b = x1 - x2;
        e = (a * x1) + (b * y1);
        //for second line
        c = y4 - y3;
        d = x3 - x4;
        f = (c * x3) + (d * y3);
    }

    //six "getter" functions, one for each variable
    double getA() { return a; }
    double getB() { return b; }
    double getC() { return c; }
    double getD() { return d; }
    double getE() { return e; }
    double getF() { return f; }

    //tells whether or not the equation is solvable
    bool isSolvable()
    {
        if ((a * d) == (b * c))
        {
            return false;
        }

        else
        {
            return true;
        }

    };

    //"getter" function that returns x
    double getX()
    {
        double first = e * d;
        double second = b * f;
        double sub = first - second;
        double third = a * d;
        double fourth = b * c;
        double subTwo = third - fourth;
        double x = sub / subTwo;
        //double x = ((e * d) - (b * f)) / ((a * d) - (b * c));
        return x;
    }

    //"getter" function that returns y
    double getY()
    {
        double y = ((a * f) - (e * c)) / ((a * d) - (b * c));

        return y;
    }
};

#endif

交点jiaodian.cpp文件:

#include<iostream>
#include"linear.h"
using namespace std;

//分为两组,第一组为x1,y1与x2,y2;另一组为x3y3,x4y4
int main()
{
    double x1 = 1, y1 = 2, x2 = 4, y2 = 2, x3 = 2, y3 = 1, x4 = 2, y4 = 4;
    LinearEquation twoLines(x1, y1, x2, y2, x3, y3, x4, y4);
    if (twoLines.isSolvable())
    {
        cout << "The intersection is " << twoLines.getX() << "," << twoLines.getY() << endl;
    }
    else
    {
        cout << "These lines do not intersect." << endl;
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值