Home

Download

Features

Screenshots

Handbook

Browse Source

Authors

SourceForge.net Logo
Hosted by SourceForge.net

OSI Certified


Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages   Search  

definiteintegralfunction.cpp

00001 /***************************************************************************
00002                           definiteintegralfunction.cpp  -  description
00003                              -------------------
00004     begin                : Mon Nov 11 2002
00005     copyright            : (C) 2002 by Fungmeista
00006     email                : mizunoami44@sourceforge.net
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "definiteintegralfunction.h"
00019 
00020 #include <cmath>
00021 
00022 #include <qpainter.h>
00023 #include <qpointarray.h>
00024 #include <qobject.h>
00025 
00026 #include "fungparser.h"
00027 #include "fungmath.h"
00028 
00029 DefiniteIntegralFunction::DefiniteIntegralFunction(BasicGraph *graph, int expressions, double _precision) : MathFunction(graph,expressions), precision(_precision)
00030 {
00031     pointsStorage = new QPointArray;
00032 }
00033 
00034 DefiniteIntegralFunction::~DefiniteIntegralFunction()
00035 {
00036     delete pointsStorage;
00037 }
00038 
00039 void DefiniteIntegralFunction::calculate_and_draw(QPainter *painter)
00040 {
00041     if ( lowerBounds.x() > upperBounds.x() ) //bounds check
00042     {
00043         QString s( QObject::tr("Invalid bounds") );
00044         setResult( s );
00045         return;
00046     }
00047 
00048     pointsStorage->resize(upperBounds.x()-lowerBounds.x()+4); //add 1 so that both lower and upper bounds are included and another 2 so that the base of the figure is included
00049 
00050     double area = 0;
00051     (*pointsStorage)[upperBounds.x()-lowerBounds.x()+3] = QPoint(upperBounds.x(),toPixelYCoord(0));
00052     for (int i=lowerBounds.x(); i<=upperBounds.x(); i++)
00053     {
00054         //use traprule to calculate definite integral
00055         double height = precision; //smaller height the more precise the answer
00056         double alpha[] = {toGraphXCoord(i),animatorValue()};
00057         double alpha2[] = {toGraphXCoord(i+height),animatorValue()};
00058 
00059         double baseone = expression->Eval(alpha);
00060         double basetwo = expression->Eval(alpha2);
00061         area += 0.5*(baseone+basetwo)*(toGraphXCoord(height)-toGraphXCoord(0));
00062 
00063         (*pointsStorage)[i+1-lowerBounds.x()]=QPoint(i,toPixelYCoord(baseone));
00064         (*pointsStorage)[i+2-lowerBounds.x()]=QPoint((int)(ROUND(i+height)),toPixelYCoord(basetwo));
00065     }
00066     (*pointsStorage)[0] = QPoint(lowerBounds.x(),toPixelYCoord(0));
00067     draw_stored(painter);
00068     
00069     QString s = QString("Definite Integral = %1").arg(area);
00070     setResult(s);
00071 }
00072 
00073 void DefiniteIntegralFunction::draw_stored(QPainter *painter)
00074 {
00075     painter->save();
00076     painter->setBrush(QBrush(QColor(255,0,0),Qt::Dense4Pattern));
00077     painter->drawPolygon(*pointsStorage);
00078     painter->restore();
00079 }
00080 
00081