Game Accessibility Library logo SourceForge.net Logo
Game Accessibility Suite: CAT/CATLineFit.cpp Source File

CATLineFit.cpp

Go to the documentation of this file.
00001 /// \file CATLineFit.cpp
00002 /// \brief Regression to fit a line to a set of points.
00003 /// \ingroup CAT
00004 ///
00005 /// Copyright (c) 2002-2008 by Michael Ellison.
00006 /// See COPYING.txt for the \ref gaslicense License (MIT License).
00007 ///
00008 // $Author: mikeellison $
00009 // $Date: 2008-01-19 19:19:35 -0600 (Sat, 19 Jan 2008) $
00010 // $Revision:   $
00011 // $NoKeywords: $
00012 
00013 #include "CATLineFit.h"
00014 
00015 CATLineFit::CATLineFit()
00016 {
00017     fDirty          = true;
00018     fSlope          = 0.0;
00019     fIntercept      = 0.0;
00020     fSumX           = 0.0;
00021     fSumY           = 0.0;
00022     fSumXY          = 0.0;
00023     fSumXSquared    = 0.0;
00024 }
00025 
00026 CATLineFit::~CATLineFit()
00027 {
00028     Clear();
00029 }
00030 
00031 bool CATLineFit::AddPoint( CATFloat64 x, CATFloat64 y)
00032 {
00033     // Push both points onto Lists and set dirty flag
00034     fDirty = true;
00035     fPointList.push_back(CATPoint(x,y));
00036     // Keep vars up to date to minimize calculations later
00037     fSumX += x;
00038     fSumY += y;
00039     fSumXY += x*y;
00040     fSumXSquared += x*x;
00041 
00042     return true;
00043 }
00044 
00045 bool CATLineFit::Clear()
00046 {
00047     
00048     // Clear all the Lists...
00049     fPointList.clear();
00050 
00051     // Set dirty flag
00052     fDirty = true;
00053 
00054     fSlope          = 0.0;
00055     fIntercept      = 0.0;
00056     fSumX               = 0.0;
00057     fSumY               = 0.0;
00058     fSumXY          = 0.0;
00059     fSumXSquared    = 0.0;
00060 
00061     return true;
00062 }
00063 
00064 
00065 CATUInt32 CATLineFit::GetNumPoints()
00066 {   
00067     return (CATUInt32)fPointList.size();
00068 }
00069 
00070 bool CATLineFit::GetDataPoint( CATUInt32 n, CATFloat64& x, CATFloat64& y)
00071 {   
00072     if (n >= fPointList.size())
00073     {
00074         return false;
00075     }
00076 
00077     CATPoint point = fPointList[n];
00078 
00079     x = point.x;
00080     y = point.y;
00081 
00082     return true;
00083 }
00084 
00085 bool CATLineFit::GetCurrentErr(CATFloat64 &err)
00086 {
00087     err = 0.0;
00088     // Recalc if the coefficient table is dirty
00089     if (fDirty)
00090     {
00091         if (!CalcFit())
00092         {
00093             return false;
00094         }
00095     }
00096     
00097     // Return the last calc'd error
00098     err = fLastErr;
00099 
00100     return true;
00101 }
00102 
00103 
00104 
00105 bool CATLineFit::CalcYVal( CATFloat64 x, CATFloat64& y)
00106 {
00107     y = 0.0;
00108     // Recalc if the coefficient table is dirty
00109     if (fDirty)
00110     {
00111         if (!CalcFit())
00112         {
00113             return false;
00114         }
00115     }
00116 
00117     // Calculate Y val from X and calculated coefficients....
00118     y = this->fSlope * x + this->fIntercept;
00119 
00120     return true;
00121 }
00122 
00123 
00124 bool CATLineFit::CalcFit()
00125 {
00126     // Return false if we don't have any points to calculate....
00127     CATUInt32 numPoints = GetNumPoints();
00128     
00129     if (numPoints < 2)
00130     {
00131         return false;
00132     }
00133 
00134     fSlope      = 0.0;
00135     fIntercept  = 0.0;
00136 
00137     fSlope      = ((numPoints * fSumXY) - (fSumX * fSumY))  / ((numPoints * fSumXSquared) - (fSumX * fSumX));
00138     fIntercept  = (fSumY - (fSlope * fSumX)) / numPoints;
00139 
00140     // Reset dirty flag
00141     fDirty = false;
00142     return true;
00143 }
00144 
00145 bool CATLineFit::GetMinMax( CATFloat64& minX, CATFloat64& minY, CATFloat64& maxX, CATFloat64& maxY)
00146 {
00147     CATUInt32 numPoints = GetNumPoints();
00148     if (numPoints == 0)
00149     {
00150         return false;
00151     }
00152     
00153 
00154     for (CATUInt32 i=0; i<numPoints; i++)
00155     {
00156         CATPoint curPoint = fPointList[i];
00157 
00158         if (i == 0)
00159         {
00160             minX = maxX = curPoint.x;
00161             minY = maxY = curPoint.y;
00162         }
00163         else
00164         {
00165             minX = min(curPoint.x, minX);
00166             maxX = max(curPoint.x, maxX);
00167             minY = min(curPoint.y, minY);
00168             maxY = max(curPoint.y, maxY);
00169         }
00170     }
00171 
00172     return true;
00173 }
00174 
00175 bool CATLineFit::Slope( CATFloat64& slope)
00176 {
00177     if (fDirty)
00178     {
00179         if (!CalcFit())
00180         {
00181             return false;
00182         }
00183     }
00184 
00185     slope = fSlope;
00186 
00187     return true;
00188 }
00189 
00190 bool CATLineFit::Intercept(CATFloat64& intercept)
00191 {
00192     if (fDirty)
00193     {
00194         if (!CalcFit())
00195         {
00196             return false;
00197         }
00198     }
00199     intercept = fIntercept;
00200     return true;
00201 }

Generated on Mon Feb 11 04:09:44 2008 for Game Accessibility Suite by doxygen 1.5.4