first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / graphiq / library / geometric / ellipse.cpp
1
2
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : ellipse                                                           *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
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 \*****************************************************************************/
17
18 #include "cartesian_objects.h"
19 #include "ellipse.h"
20 #include "line.h"
21 #include "rectangle.h"
22
23 #include <basis/functions.h>
24
25 #include <math.h>
26
27 using namespace basis;
28
29 namespace geometric {
30
31 ellipse::ellipse()
32 : _center(cartesian_point::origin()),
33   _width_from_center(1),
34   _height_from_center(1)
35 {}
36
37 ellipse::ellipse(const cartesian_point &a_center, double a_width_from_center,
38     double a_height_from_center)
39 : _center(a_center),
40   _width_from_center(a_width_from_center),
41   _height_from_center(a_height_from_center)
42 {}
43
44 ellipse::ellipse(const ellipse &to_copy)
45 : _center(),
46   _width_from_center(0),
47   _height_from_center(0)
48 { *this = to_copy; }
49
50 ellipse::~ellipse() {}
51
52 ellipse &ellipse::operator = (const ellipse &to_copy)
53 {
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;
58   return *this;
59 }
60
61 double ellipse::area() const
62 { return absolute_value(PI_APPROX * _width_from_center * _height_from_center); }
63
64 double ellipse::perimeter() const
65 {
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;
70 }
71
72 cartesian_point ellipse::location(const double_angle &where) const
73 {
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);
78   double ab = a * b;
79   double tango = where.tangent();
80   cartesian_point to_return(ab / denom, ab * tango / denom);
81
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);
86   ang %= 360;
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());
90   to_return += _center;
91   return to_return;
92 }
93
94 bool ellipse::inside(const cartesian_point &where) const
95 {
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;
100 }
101
102 cartesian_point ellipse::center() const { return _center; }
103
104 double ellipse::width_from_center() const { return _width_from_center; }
105
106 double ellipse::height_from_center() const { return _height_from_center; }
107
108 void ellipse::center(const cartesian_point &to_set) { _center = to_set; }
109
110 void ellipse::width_from_center(double to_set)
111 { _width_from_center = to_set; }
112
113 void ellipse::height_from_center(double to_set)
114 { _height_from_center = to_set; }
115   
116 } // namespace.
117
118
119
120