geometry2D.cpp

Go to the documentation of this file.
00001 
00015 #include <dlrNumeric/geometry2D.h>
00016 #include <dlrNumeric/utilities.h>
00017 
00018 namespace dlr {
00019 
00020   namespace numeric {
00021     
00022     bool
00023     bilaterate(const Vector2D& point0, const Vector2D& point1,
00024                double range0, double range1,
00025                Vector2D& intersection0, Vector2D& intersection1)
00026     {
00027       // We use the traditional approach of transforming the points so
00028       // that point0 is at the origin and point1 is on the X axis,
00029       // solving this new bilateration, and then transforming back to
00030       // the original coordinate system.
00031 
00032       Vector2D baselineVector(point1 - point0);
00033     
00034       // Coordinates for the translated input points will be (0, 0), and
00035       // (x0, 0);
00036       double x0 = magnitude(baselineVector);
00037     
00038       // Compute direction vectors for the new X and Y axis.
00039       Vector2D e0(baselineVector / x0);
00040       Vector2D e1(-e0.y(), e0.x());
00041 
00042       // In our new coordinate system, we can write x^2 + y^2 = r0^2,
00043       // and (x - x0)^2 + y^2 = r1^2.
00044       //
00045       // Subtracting these two equations gives 2*x0*x - x0^2 = r0^2 -
00046       // r1^2, which gives us x = (r0^2 - r1^2 + x0^2) / (2*x0).
00047       double r0Squared = range0 * range0;
00048       double xValue = (r0Squared - range1 * range1 + x0 * x0) / (2 * x0);
00049 
00050       // Substituting this into x^2 + y^2 = r0^2, we have y = sqrt(r0^2 - x^2).
00051       double r0SquaredMinusXSquared = r0Squared - xValue * xValue;
00052       if(r0SquaredMinusXSquared < 0.0) {
00053         return false;
00054       }
00055       double yValue = std::sqrt(r0SquaredMinusXSquared);
00056 
00057       // Now we tranform back into the original coordinate system.
00058       Vector2D xComponent = xValue * e0;
00059       Vector2D yComponent = yValue * e1;
00060       intersection0 = point0 + xComponent + yComponent;
00061       intersection1 = point0 + xComponent - yComponent;
00062       return true;
00063     }
00064   
00065   } // namespace numeric
00066 
00067 } // namespace dlr

Generated on Wed Nov 25 00:42:42 2009 for dlrUtilities Utility Library by  doxygen 1.5.8