feisty meow concerns codebase  2.140
ellipse.cpp
Go to the documentation of this file.
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 
51 
53 {
54  if (this == &to_copy) return *this;
55  _center = to_copy._center;
58  return *this;
59 }
60 
61 double ellipse::area() const
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 
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 
103 
105 
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 
contents get(angular_units unit) const
retrieves the current angular measure.
Definition: angle.h:202
contents tangent() const
returns the tan function of this angle.
Definition: angle.h:158
Provides a geometric point that use double floating points numbers.
double_angle provides a non-templated class for forward declarations.
Definition: angle.h:97
Represents a geometric ellipse.
Definition: ellipse.h:28
cartesian_point _center
Definition: ellipse.h:64
double area() const
Returns the area occupied by the ellipse.
Definition: ellipse.cpp:61
double width_from_center() const
Definition: ellipse.cpp:104
cartesian_point location(const double_angle &where) const
Describes the locus of the ellipse (basically, where it lives).
Definition: ellipse.cpp:72
cartesian_point center() const
Definition: ellipse.cpp:102
bool inside(const cartesian_point &where) const
Returns true if the point is inside the ellipse.
Definition: ellipse.cpp:94
ellipse & operator=(const ellipse &to_copy)
Definition: ellipse.cpp:52
double _height_from_center
Definition: ellipse.h:66
double height_from_center() const
Definition: ellipse.cpp:106
double perimeter() const
Returns the perimeter for the ellipse.
Definition: ellipse.cpp:64
double _width_from_center
Definition: ellipse.h:65
void set(numeric_type x, numeric_type y)
Definition: point.h:118
numeric_type distance(const point &p2) const
Returns the distance between ‘this’ and the second point ‘p2’.
Definition: point.h:133
numeric_type x() const
Definition: point.h:47
numeric_type y() const
Definition: point.h:48
#define PI_APPROX
An approximation of the fundamental circular constant.
Definition: definitions.h:41
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
type square(const type &a)
Returns the square of the object (which is a * a).
Definition: functions.h:92
type absolute_value(type a)
Returns a if a is non-negative, and returns -a otherwise.
Definition: functions.h:33
Contains all of our objects for geometry and avoids name clashes.
Definition: angle.h:25
@ DEGREES
Definition: angle.h:30
@ RADIANS
Definition: angle.h:30