ellipse.cpp

Go to the documentation of this file.
00001 
00002 
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.h"
00021 #include "rectangle.h"
00022 
00023 #include <basis/functions.h>
00024 
00025 #include <math.h>
00026 
00027 using namespace basis;
00028 
00029 namespace geometric {
00030 
00031 ellipse::ellipse()
00032 : _center(cartesian_point::origin()),
00033   _width_from_center(1),
00034   _height_from_center(1)
00035 {}
00036 
00037 ellipse::ellipse(const cartesian_point &a_center, double a_width_from_center,
00038     double a_height_from_center)
00039 : _center(a_center),
00040   _width_from_center(a_width_from_center),
00041   _height_from_center(a_height_from_center)
00042 {}
00043 
00044 ellipse::ellipse(const ellipse &to_copy)
00045 : _center(),
00046   _width_from_center(0),
00047   _height_from_center(0)
00048 { *this = to_copy; }
00049 
00050 ellipse::~ellipse() {}
00051 
00052 ellipse &ellipse::operator = (const ellipse &to_copy)
00053 {
00054   if (this == &to_copy) return *this;
00055   _center = to_copy._center;
00056   _width_from_center = to_copy._width_from_center;
00057   _height_from_center = to_copy._height_from_center;
00058   return *this;
00059 }
00060 
00061 double ellipse::area() const
00062 { return absolute_value(PI_APPROX * _width_from_center * _height_from_center); }
00063 
00064 double ellipse::perimeter() const
00065 {
00066   double w = _width_from_center;
00067   double h = _height_from_center;
00068   double perim_temp = sqrt(square(h) + square(w)) / 2;
00069   return 2.0 * PI_APPROX * perim_temp;
00070 }
00071 
00072 cartesian_point ellipse::location(const double_angle &where) const
00073 {
00074   double a = _width_from_center;
00075   double b = _height_from_center;
00076   double a_multiplier = square(where.tangent());
00077   double denom = sqrt(square(b) + square(a) * a_multiplier);
00078   double ab = a * b;
00079   double tango = where.tangent();
00080   cartesian_point to_return(ab / denom, ab * tango / denom);
00081 
00082   // the following negates the x component if the angle is in the appropriate
00083   // part of the ellipse.
00084   int ang = int(where.get(DEGREES));
00085   double adjustment = where.get(DEGREES) - double(ang);
00086   ang %= 360;
00087   double adjusted_ang = ang + adjustment;
00088   if ( (adjusted_ang < 270.0) && (adjusted_ang > 90.0) )
00089      to_return.set(to_return.x() * -1.0, to_return.y());
00090   to_return += _center;
00091   return to_return;
00092 }
00093 
00094 bool ellipse::inside(const cartesian_point &where) const
00095 {
00096   double dist = where.distance(_center);
00097   double_angle to_point = double_angle(asin(where.y() / dist), RADIANS);
00098   cartesian_point intersector = location(to_point);
00099   return dist <= intersector.distance(_center)? true : false;
00100 }
00101 
00102 cartesian_point ellipse::center() const { return _center; }
00103 
00104 double ellipse::width_from_center() const { return _width_from_center; }
00105 
00106 double ellipse::height_from_center() const { return _height_from_center; }
00107 
00108 void ellipse::center(const cartesian_point &to_set) { _center = to_set; }
00109 
00110 void ellipse::width_from_center(double to_set)
00111 { _width_from_center = to_set; }
00112 
00113 void ellipse::height_from_center(double to_set)
00114 { _height_from_center = to_set; }
00115   
00116 } // namespace.
00117 
00118 
00119 
00120 
Generated on Sat Jan 28 04:22:41 2012 for hoople2 project by  doxygen 1.6.3