码迷,mamicode.com
首页 > 其他好文 > 详细

输入三个点坐标,生成一元二次方程

时间:2016-05-12 14:13:59      阅读:480      评论:0      收藏:0      [点我收藏+]

标签:

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

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

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

技术分享

技术分享



主要实现在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

输入三个点坐标,生成一元二次方程

标签:

原文地址:http://blog.csdn.net/cy_tec/article/details/51367619

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!