C++实现类,输入三个点坐标,生成一元二次方程(含推理过程)

今天实现一个类,通过三个点得到一个一元二次方程。

主要实现三阶求逆,然后就可以很方便求得一元二次方程的三个系数。

求得一元二次方程解的原理很简单,推导过程如下图:





主要实现在init()函数里面,类代码如下:

//  Created by cslzy on 16/5/10.
//  Copyright © 2016年 CY. All rights reserved.
//

#include "heads.hpp"


//class parabolaObject ben//
class parabolaObject
{
private:
    float a = 0.0;
    float b = 0.0;
    float c = 0.0;
    bool check(); // check three initial points is ok; check initialization is ok
    bool check_is_ok = false;
public:
    parabolaObject(float , float , float );
    parabolaObject(Point2f , Point2f , Point2f );
    parabolaObject();
    void setParameters(float , float , float );
    void init(Point2f, Point2f, Point2f);
    float value(float x); // return y, where y = a*x^2 + b*x + c
    void test();
};

void parabolaObject::test()
{
    Point2f p1(0.0, 0.0);
    Point2f p2(127.5, 255.0);
    Point2f p3(255.0, 0.0);
    cout<<"--!Infor: test() function setdefault points:"<<p1<<p2<<p3<<"--"<<endl;
    init(p1, p2, p3);
}


void parabolaObject::init(Point2f p1, Point2f p2, Point2f p3)
{
//    cout<<"--Infor:input points are:"<<p1<<p2<<p3<<"--"<<endl;
    // get 3x3 matrix
    float x1 = p1.x;
    float x1_2 = x1 * x1;
    float y1 = p1.y;
    
    float x2 = p2.x;
    float x2_2 = x2 * x2;
    float y2 = p2.y;
     
    float x3 = p3.x;
    float x3_2 = x3 * x3;
    float y3 = p3.y;
    
    // is this matrix reversible?利用行列式来判断一下是否可逆。
    float matrix = (x1_2*x2*1 + x2_2*x3*1 + x3_2*x1*1) - (x1_2*x3*1 + x2_2*x1*1 + x3_2*x2*1);
    // test
//    cout<<"Matrix value:"<<matrix<<endl;
    if(matrix==0.0)
    {
        cout<<"--Error:input point is wrong!--"<<endl;
        exit(-1);
    }
    
    // get inverse 
    float a11 =  (x2 - x3)/matrix;
    float a12 = -(x2_2 - x3_2)/matrix;
    float a13 =  (x2_2*x3 - x3_2*x2)/matrix;
    float a21 = -(x1 - x3)/matrix;
    float a22 =  (x1_2 - x3_2)/matrix;
    float a23 = -(x1_2*x3 - x3_2*x1)/matrix;
    float a31 =  (x1 - x2)/matrix;
    float a32 = -(x1_2 - x2_2)/matrix;
    float a33 =  (x1_2*x2 - x2_2*x1)/matrix;
    
//下面可以用来验证结果,在线验证网址:http://matrix.reshish.com/inverCalculation.php
//    cout<<a11<<" "<<a12<<" "<<a13<<" "<<endl;
//    cout<<a21<<" "<<a22<<" "<<a23<<" "<<endl;
//    cout<<a31<<" "<<a32<<" "<<a33<<" "<<endl;
    // get a, b, c
    a = a11*y1 + a21*y2 + a31*y3;
    b = a12*y1 + a22*y2 + a32*y3;
    c = a13*y1 + a23*y2 + a33*y3;
    cout<<"Y = "<<a<<"x^2 + "<<b<<"x + "<<c<<";"<<endl;
}

parabolaObject::parabolaObject(float x1, float x2, float x3)
{
    a = x1;
    b = x2;
    c = x3;
}

parabolaObject::parabolaObject(){};

parabolaObject::parabolaObject(Point2f p1, Point2f p2, Point2f p3)
{
    init(p1, p2, p3);
}

void parabolaObject::setParameters(float x1, float x2, float x3)
{
    a = x1;
    b = x2;
    c = x3;
}

float parabolaObject::value(float x)
{
    return a*x*x + b*x + c;
}
//class parabolaObject end//
void test_easyMath() // 测试函数
{
    Point2f p1(0.0, 0.0);
    Point2f p2(127.5, 255.0);
    Point2f p3(255.0, 0.0);
    
    parabolaObject test_parabola;
//    test_parabola.test();
    test_parabola.init(p1, p2, p3);
    float x = 200.0;
    cout<<"x = "<<x<<" y = "<<test_parabola.value(x)<<endl;
    cout<<"Input a value:";
    cin >> x;
    do{
        cout<<"x = "<<x<<" y = "<<test_parabola.value(x)<<endl;
        cout<<"Input a value:";
        cin >> x;
    }while (x!=2.0);
}


验证逆矩阵:http://matrix.reshish.com/inverCalculation.php

求逆参考:http://mathworld.wolfram.com/MatrixInverse.html

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值