ellipse.cpp

Go to the documentation of this file.
00001 #ifndef ELLIPSE_IMPLEMENTATION_FILE
00002 #define ELLIPSE_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : ellipse                                                           *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "cartesian_objects.h"
00019 #include "ellipse.h"
00020 #include "line.cpp"
00021 #include "rectangle.cpp"
00022 
00023 #include <basis/function.h>
00024 
00025 #include <math.h>
00026 
00027 namespace geometric {
00028 
00029 ellipse::ellipse()
00030 : _center(new cartesian_point(origin())),
00031   _width_from_center(1),
00032   _height_from_center(1)
00033 {}
00034 
00035 ellipse::ellipse(const cartesian_point &a_center, double a_width_from_center,
00036     double a_height_from_center)
00037 : _center(new cartesian_point(a_center)),
00038   _width_from_center(a_width_from_center),
00039   _height_from_center(a_height_from_center)
00040 {}
00041 
00042 ellipse::ellipse(const ellipse &to_copy)
00043 : _center(new cartesian_point()),
00044   _width_from_center(0),
00045   _height_from_center(0)
00046 { *this = to_copy; }
00047 
00048 ellipse::~ellipse() { WHACK(_center); }
00049 
00050 ellipse &ellipse::operator = (const ellipse &to_copy)
00051 {
00052   if (this == &to_copy) return *this;
00053   *_center = *to_copy._center;
00054   _width_from_center = to_copy._width_from_center;
00055   _height_from_center = to_copy._height_from_center;
00056   return *this;
00057 }
00058 
00059 double ellipse::area() const
00060 { return absolute_value(PI_APPROX * _width_from_center * _height_from_center); }
00061 
00062 double ellipse::perimeter() const
00063 {
00064   double w = _width_from_center;
00065   double h = _height_from_center;
00066   double perim_temp = sqrt(square(h) + square(w)) / 2;
00067   return 2.0 * PI_APPROX * perim_temp;
00068 }
00069 
00070 cartesian_point ellipse::location(const double_angle &where) const
00071 {
00072   double a = _width_from_center;
00073   double b = _height_from_center;
00074   double a_multiplier = square(where.tangent());
00075   double denom = sqrt(square(b) + square(a) * a_multiplier);
00076   double ab = a * b;
00077   double tango = where.tangent();
00078   cartesian_point to_return(ab / denom, ab * tango / denom);
00079 
00080   // the following negates the x component if the angle is in the appropriate
00081   // part of the ellipse.
00082   int ang = int(where.get(double_angle::DEGREES));
00083   double adjustment = where.get(double_angle::DEGREES) - double(ang);
00084   ang %= 360;
00085   double adjusted_ang = ang + adjustment;
00086   if ( (adjusted_ang < 270.0) && (adjusted_ang > 90.0) )
00087      to_return.set(to_return.x() * -1.0, to_return.y());
00088   to_return += *_center;
00089   return to_return;
00090 }
00091 
00092 bool ellipse::inside(const cartesian_point &where) const
00093 {
00094   double dist = where.distance(*_center);
00095   double_angle to_point = double_angle(asin(where.y() / dist),
00096       double_angle::RADIANS);
00097   cartesian_point intersector = location(to_point);
00098   return dist <= intersector.distance(*_center)? true : false;
00099 }
00100 
00101 cartesian_point ellipse::center() const { return *_center; }
00102 
00103 double ellipse::width_from_center() const { return _width_from_center; }
00104 
00105 double ellipse::height_from_center() const { return _height_from_center; }
00106 
00107 void ellipse::center(const cartesian_point &to_set) { *_center = to_set; }
00108 
00109 void ellipse::width_from_center(double to_set)
00110 { _width_from_center = to_set; }
00111 
00112 void ellipse::height_from_center(double to_set)
00113 { _height_from_center = to_set; }
00114   
00115 } // namespace.
00116 
00117 
00118 #endif //ELLIPSE_IMPLEMENTATION_FILE
00119 

Generated on Thu Nov 20 04:28:57 2008 for HOOPLE Libraries by  doxygen 1.5.1