home *** CD-ROM | disk | FTP | other *** search
- /*
- created March 10th 1998 by Katherine Smith
- */
-
- #include <math.h>
- #include <QuickDraw.h>
- #include <stdio.h>
- #include <DateTimeUtils.h>
- #include "UtilityFunctions.h"
-
-
- /* functions */
-
- /* returns a floating point between lowBound and highBound with uniform distribution. */
- float UtilRandomFloat (float lowBound, float highBound)
- {
- return (float)(Random() + 32768) / (65535.0 / (highBound - lowBound)) + lowBound;
- }
-
- long UtilRandomLong (long lowBound, long highBound)
- {
- return ((Random() + 32768) % (highBound - lowBound + 1)) + lowBound;
- }
-
- void UtilSeedRandom (long seed)
- {
- if (seed == 0) GetDateTime((unsigned long*)&qd.randSeed);
- else qd.randSeed = seed;
- printf("%d \n",qd.randSeed);
- }
-
- float UtilEuclideanDistance (float x1, float y1, float x2, float y2)
- {
- return sqrt ((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
- }
-
- void NormalizeVector (float *x, float *y)
- {
- float length;
-
- length = UtilEuclideanDistance (0,0,*x,*y);
-
- if (length != 0)
- {
- *x /= length;
- *y /= length;
- }
- }
-
- /* solves the Least Squares approximation to a linear fit of a set of points */
- void UtilLeastSquareLineApprox (float *x, float *y, long numPoints, float *slope, float *yIntercept)
- {
- float d;
- float sumXSq, sumX, sumY, sumXY;
- int i;
-
- sumXSq = 0; sumX = 0; sumY = 0; sumXY = 0;
-
- for (i = 0; i < numPoints; i++)
- {
- sumXSq += x[i] * x[i];
- sumX += x[i];
- sumY += y[i];
- sumXY += x[i] * y[i];
- }
-
- d = numPoints * sumXSq - (sumX * sumX);
-
- *slope = (1/d) * (numPoints * sumXY - sumX * sumY);
- *yIntercept = (1/d) * (sumXSq * sumY - sumX * sumXY);
- }