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  

arclengthfunction.cpp

00001 /***************************************************************************
00002                           arclengthfunction.cpp  -  description
00003                              -------------------
00004     begin                : Mon Nov 11 2002
00005     copyright            : (C) 2002 by Fungmeista
00006     email                : mizunoami44@users.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 "arclengthfunction.h"
00019 
00020 #include <cmath>
00021 
00022 #include <qpointarray.h>
00023 #include <qpainter.h>
00024 #include <qobject.h>
00025 
00026 #include "fungmath.h"
00027 #include "fungparser.h"
00028 
00029 ArcLengthFunction::ArcLengthFunction(BasicGraph *graph, int expressions) : MathFunction(graph,expressions)
00030 {
00031     pointsStorage = new QPointArray();
00032 }
00033 ArcLengthFunction::~ArcLengthFunction()
00034 {
00035     delete pointsStorage;
00036 }
00037 
00038 void ArcLengthFunction::calculate_and_draw(QPainter *painter)
00039 {
00040     if ( expression2 ) /* parametric graph */
00041     {
00042         if ( arg1 > arg2 ) //bounds check
00043         {
00044             QString s( QObject::tr("Invalid bounds") );
00045             setResult( s );
00046             return;
00047         }
00048     
00049         double step = 0.1;
00050         qDebug("arg1: %f, arg2: %f",arg1,arg2);
00051         pointsStorage->resize( static_cast<int>(ROUND((arg2-arg1)/step) + 2) );
00052         double distance = 0;
00053         int i = 0;
00054         for (double t=arg1; t<arg2; t+=step)
00055         {
00056             double alpha[] = {t,animatorValue()};
00057             double alpha2[] = {t+step,animatorValue()};
00058 
00059             double valuex = expression->Eval(alpha);
00060             double valuex2 = expression->Eval(alpha2);
00061 
00062             double valuey = expression2->Eval(alpha);
00063             double valuey2 = expression2->Eval(alpha2);
00064 
00065             (*pointsStorage)[i]=QPoint(toPixelXCoord(valuex),toPixelYCoord(valuey));
00066             (*pointsStorage)[i+1]=QPoint(toPixelXCoord(valuex2),toPixelYCoord(valuey2));
00067 
00068             distance += sqrt(
00069                 ((valuex-valuex2)*(valuex-valuex2))+
00070                 ((valuey-valuey2)*(valuey-valuey2))
00071                 );
00072 
00073             painter->drawLine(toPixelXCoord(valuex),toPixelYCoord(valuey),toPixelXCoord(valuex2),toPixelYCoord(valuey2));
00074             
00075             i++;
00076         }
00077         QString s = QString("Arc Length = %1").arg(distance);
00078         setResult(s);
00079     }
00080     else
00081     {
00082         if ( lowerBounds.x() > upperBounds.x() ) //bounds check
00083         {
00084             QString s( QObject::tr("Invalid bounds") );
00085             setResult( s );
00086             return;
00087         }
00088     
00089         pointsStorage->resize(upperBounds.x()-lowerBounds.x() + 1);
00090         double distance = 0;
00091         for (int i=lowerBounds.x(); i<upperBounds.x(); i++)
00092         {
00093             double alpha[] = {toGraphXCoord(i),animatorValue()};
00094             double alpha2[] = {toGraphXCoord(i+1),animatorValue()};
00095 
00096             double value = expression->Eval(alpha);
00097             double value2 = expression->Eval(alpha2);
00098 
00099             (*pointsStorage)[i-lowerBounds.x()]=QPoint(i,toPixelYCoord(value));
00100             (*pointsStorage)[i+1-lowerBounds.x()]=QPoint(i+1,toPixelYCoord(value2));
00101 
00102             distance += sqrt(
00103                 ((alpha[0]-alpha2[0])*(alpha[0]-alpha2[0]))+
00104                 ((value-value2)*(value-value2))
00105                 );
00106 
00107             painter->drawLine(i,toPixelYCoord(value),i+1,toPixelYCoord(value2));
00108         }
00109         QString s = QString("Arc Length = %1").arg(distance);
00110         setResult(s);
00111     }
00112 }
00113 
00114 void ArcLengthFunction::draw_stored(QPainter *painter)
00115 {
00116     painter->drawPolyline(*pointsStorage);
00117 }
00118 
00119