| Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages   Search 
 meanvaluefunction.cpp00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 #include "meanvaluefunction.h"
00019 
00020 #include <qpainter.h>
00021 #include <qpoint.h>
00022 #include <qobject.h>
00023 
00024 #include "basicgraph.h"
00025 #include "fungparser.h"
00026 
00027 MeanValueFunction::MeanValueFunction(BasicGraph *graph,int expressions) : MathFunction(graph,expressions)
00028 {
00029 }
00030 MeanValueFunction::~MeanValueFunction()
00031 {
00032 }
00033 
00034 void MeanValueFunction::calculate_and_draw(QPainter* painter)
00035 {
00036     if ( lowerBounds.x() > upperBounds.x() ) 
00037     {
00038         QString s( QObject::tr("Invalid bounds") );
00039         setResult( s );
00040         return;
00041     }
00042 
00043     found = false;
00044     double slope = (toGraphYCoord(upperBounds.y()) - toGraphYCoord(lowerBounds.y())) / (toGraphXCoord(upperBounds.x()) - toGraphXCoord(lowerBounds.x()));
00045     for(int i=lowerBounds.x(); i<=upperBounds.x(); i++)
00046     {
00047         double x = toGraphXCoord(i);
00048         double x_next = toGraphXCoord(i+1);
00049 
00050         double d[] = {x,animatorValue()};
00051         double d_next[] = {x_next,animatorValue()};
00052 
00053         double y = expression->Eval(d);
00054         double y_next = expression->Eval(d_next);
00055 
00056         double slope_search = (y - y_next) / (x - x_next);
00057         if ( (slope_search <= slope + 0.1) && (slope_search >= slope - 0.1) )
00058         {
00059             point_x = x;
00060             point_y = y;
00061             found = true;
00062             break;
00063         }
00064     }
00065 
00066     double b = -(slope * point_x - point_y);
00067 
00068     double _x1 = toGraphXCoord(0);
00069     double _y1 = _x1*slope + b;
00070 
00071     double _x2 = toGraphXCoord(basicgraph->width());
00072     double _y2 = _x2*slope + b;
00073 
00074     x1 = toPixelXCoord(_x1);
00075     y1 = toPixelYCoord(_y1);
00076     x2 = toPixelXCoord(_x2);
00077     y2 = toPixelYCoord(_y2);
00078 
00079     if (found)
00080     {
00081         QString s = QString(QObject::tr("Mean Value = %1")).arg(point_x);
00082         setResult(s);
00083     }
00084     else
00085     {
00086         QString s = QObject::tr("None found");
00087         setResult(s);
00088     }
00089 
00090     draw_stored(painter);
00091 }
00092 
00093 void MeanValueFunction::draw_stored(QPainter* painter)
00094 {
00095     if (found)
00096     {
00097         painter->drawLine(x1,y1,x2,y2); 
00098         drawSelectedPoint(point_x,point_y,painter);
00099     }
00100 }
 |