4 /*****************************************************************************\
7 * Author : Chris Koeritz *
9 *******************************************************************************
10 * Copyright (c) 1992-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
18 #include "cartesian_objects.h"
21 #include "rectangle.h"
23 #include <basis/functions.h>
27 using namespace basis;
32 : _center(cartesian_point::origin()),
33 _width_from_center(1),
34 _height_from_center(1)
37 ellipse::ellipse(const cartesian_point &a_center, double a_width_from_center,
38 double a_height_from_center)
40 _width_from_center(a_width_from_center),
41 _height_from_center(a_height_from_center)
44 ellipse::ellipse(const ellipse &to_copy)
46 _width_from_center(0),
47 _height_from_center(0)
50 ellipse::~ellipse() {}
52 ellipse &ellipse::operator = (const ellipse &to_copy)
54 if (this == &to_copy) return *this;
55 _center = to_copy._center;
56 _width_from_center = to_copy._width_from_center;
57 _height_from_center = to_copy._height_from_center;
61 double ellipse::area() const
62 { return absolute_value(PI_APPROX * _width_from_center * _height_from_center); }
64 double ellipse::perimeter() const
66 double w = _width_from_center;
67 double h = _height_from_center;
68 double perim_temp = sqrt(square(h) + square(w)) / 2;
69 return 2.0 * PI_APPROX * perim_temp;
72 cartesian_point ellipse::location(const double_angle &where) const
74 double a = _width_from_center;
75 double b = _height_from_center;
76 double a_multiplier = square(where.tangent());
77 double denom = sqrt(square(b) + square(a) * a_multiplier);
79 double tango = where.tangent();
80 cartesian_point to_return(ab / denom, ab * tango / denom);
82 // the following negates the x component if the angle is in the appropriate
83 // part of the ellipse.
84 int ang = int(where.get(DEGREES));
85 double adjustment = where.get(DEGREES) - double(ang);
87 double adjusted_ang = ang + adjustment;
88 if ( (adjusted_ang < 270.0) && (adjusted_ang > 90.0) )
89 to_return.set(to_return.x() * -1.0, to_return.y());
94 bool ellipse::inside(const cartesian_point &where) const
96 double dist = where.distance(_center);
97 double_angle to_point = double_angle(asin(where.y() / dist), RADIANS);
98 cartesian_point intersector = location(to_point);
99 return dist <= intersector.distance(_center)? true : false;
102 cartesian_point ellipse::center() const { return _center; }
104 double ellipse::width_from_center() const { return _width_from_center; }
106 double ellipse::height_from_center() const { return _height_from_center; }
108 void ellipse::center(const cartesian_point &to_set) { _center = to_set; }
110 void ellipse::width_from_center(double to_set)
111 { _width_from_center = to_set; }
113 void ellipse::height_from_center(double to_set)
114 { _height_from_center = to_set; }