feisty meow concerns codebase  2.140
point.h
Go to the documentation of this file.
1 #ifndef POINT_CLASS
2 #define POINT_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : point *
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 "angle.h"
19 #include "math_bits.h"
20 #include "point.h"
21 
22 #include <basis/astring.h>
23 #include <basis/contracts.h>
24 #include <basis/functions.h>
26 
27 #include <math.h>
28 
30 
31 namespace geometric {
32 
34 
35 template <class numeric_type>
36 class point : public basis::packable, public virtual basis::root_object
37 {
38 public:
39  point(numeric_type x = 0, numeric_type y = 0);
40  point(numeric_type r, double_angle theta);
41 
43 
44  void set(numeric_type x, numeric_type y);
45  void set(numeric_type r, double_angle theta);
46 
47  numeric_type x() const { return _x; }
48  numeric_type y() const { return _y; }
49 
50  numeric_type r() const;
52 
53  point rotate(const double_angle &theta) const;
55 
60  numeric_type distance(const point &p2) const;
62 
63  point operator - () const { return point<numeric_type>(-_x, -_y); }
65 
66  numeric_type magnitude() const;
68 
69  point operator + (const point &arg2) const;
70  point operator - (const point &arg2) const;
71  point &operator += (const point &arg2);
72  point &operator -= (const point &arg2);
73  bool operator == (const point &arg2) const;
74 
77 
78  bool from_text(const basis::astring &text);
80 
81  virtual void pack(basis::byte_array &packed_form) const;
82  virtual bool unpack(basis::byte_array &packed_form);
83  int packed_size() const;
84 
85 private:
86  numeric_type _x;
87  numeric_type _y;
88 };
89 
91 
92 // implementations below...
93 
94 // notes:
95 //
96 // - there is an odd breaking up of the expressions where we're taking a
97 // square root because ms visual studio 7 has a bug of some sort that
98 // convinces it that angle<int> is being used in there, although it's not.
99 // these lines use a temporary named "sumsquar" to deconfuse the compiler.
100 
101 template <class numeric_type>
102 point<numeric_type>::point(numeric_type x, numeric_type y) { set(x, y); }
103 
104 template <class numeric_type>
106 { set(r, theta); }
107 
108 template <class numeric_type>
110 {
111  numeric_type temp = 0;
112  basis::astring specifier(numeric_specifier(temp));
113  basis::astring sprintf_template(basis::astring::SPRINTF, "(%s, %s)", specifier.s(), specifier.s());
114  return basis::astring(basis::astring::SPRINTF, sprintf_template.s(), x(), y());
115 }
116 
117 template <class numeric_type>
118 void point<numeric_type>::set(numeric_type x, numeric_type y)
119 { _x = x; _y = y; }
120 
121 template <class numeric_type>
122 numeric_type point<numeric_type>::r() const
123 {
124  const double sumsquar = basis::square(x()) + basis::square(y());
125  return numeric_type(sqrt(sumsquar));
126 }
127 
128 template <class numeric_type>
129 void point<numeric_type>::set(numeric_type r, double_angle theta)
130 { set(numeric_type(r * theta.cosine()), numeric_type(r * theta.sine())); }
131 
132 template <class numeric_type>
133 numeric_type point<numeric_type>::distance(const point &p2) const
134 {
135  const double sumsquar = basis::square(p2.x() - x()) + basis::square(p2.y() - y());
136  return numeric_type(sqrt(sumsquar));
137 }
138 
139 template <class numeric_type>
141 {
142  basis::outcome retval;
143  return double_angle::arctangent(y(), x(), retval);
144 }
145 
146 template <class contents>
148 {
149  basis::byte_array temp;
150 //hmmm: inefficient!
151  pack(temp);
152  return temp.length();
153 }
154 
155 template <class contents>
156 void point<contents>::pack(basis::byte_array &packed_form) const
157 {
158  structures::attach(packed_form, _x);
159  structures::attach(packed_form, _y);
160 }
161 
162 template <class contents>
164 {
165  if (!structures::detach(packed_form, _x)) return false;
166  if (!structures::detach(packed_form, _y)) return false;
167  return true;
168 }
169 
170 template <class numeric_type>
171 numeric_type point<numeric_type>::magnitude() const
172 {
173  const double sumsquar = basis::square(x()) + basis::square(y());
174  return numeric_type(sqrt(sumsquar));
175 }
176 
177 template <class numeric_type>
179 { return point<numeric_type>(x() + arg2.x(), y() + arg2.y()); }
180 
181 template <class numeric_type>
183 { return point<numeric_type>(x() - arg2.x(), y() - arg2.y()); }
184 
185 template <class numeric_type>
187 { _x += arg2.x(); _y += arg2.y(); return *this; }
188 
189 template <class numeric_type>
191 { _x -= arg2.x(); _y -= arg2.y(); return *this; }
192 
193 template <class numeric_type>
195 {
196 // this bit should be part of the floating point stuff...
197  double epsilon = 1e-10;
198  return (basis::absolute_value(x() - arg2.x()) <= epsilon)
199  && (basis::absolute_value(y() - arg2.y()) <= epsilon);
200 }
201 
202 template <class numeric_type>
204  (const double_angle &theta) const
205 {
206  numeric_type tempX = x();
207  numeric_type tempY = y();
208  numeric_type temp1 = numeric_type(tempX * theta.cosine()
209  - tempY * theta.sine());
210  numeric_type temp2 = numeric_type(tempX * theta.sine()
211  + tempY * theta.cosine());
212  return point<numeric_type>(temp1, temp2);
213 }
214 
215 template <class numeric_type>
217 {
218  numeric_type x = 0, y = 0;
219  basis::astring text(_text);
220  // chop junk off the front.
221  text = crop_non_numeric(text);
222  // scan the string for values.
223  x = text.convert(x);
224  // remove the number.
225  text = crop_numeric(text);
226  // chop off more junk.
227  text = crop_non_numeric(text);
228  // get the next number.
229  y = text.convert(y);
230  set(x, y);
231  return true;
232 }
233 
234 } // namespace.
235 
236 #endif
237 
int length() const
Returns the current reported length of the allocated C array.
Definition: array.h:115
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
Definition: astring.h:113
int convert(int default_value) const
Converts the string into a corresponding integer.
Definition: astring.cpp:757
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
Outcomes describe the state of completion for an operation.
Definition: outcome.h:31
A base class for objects that can pack into an array of bytes.
Definition: byte_array.h:87
contents cosine() const
returns the cos function of this angle.
Definition: angle.h:155
contents sine() const
returns the sin function of this angle.
Definition: angle.h:152
static angle arctangent(double opposite, double adjacent, basis::outcome &retval)
returns the atan of the angle.
Definition: angle.h:228
double_angle provides a non-templated class for forward declarations.
Definition: angle.h:97
Represents a geometric point.
Definition: point.h:37
void set(numeric_type r, double_angle theta)
Definition: point.h:129
point(numeric_type r, double_angle theta)
Definition: point.h:105
bool operator==(const point &arg2) const
Definition: point.h:194
virtual bool unpack(basis::byte_array &packed_form)
Restores the packable from the "packed_form".
Definition: point.h:163
point operator-() const
return the additive inverse of the vector
Definition: point.h:63
void set(numeric_type x, numeric_type y)
Definition: point.h:118
point operator+(const point &arg2) const
Definition: point.h:178
basis::astring text_form() const
Prints out the two values (x and y) held in the point.
Definition: point.h:109
bool from_text(const basis::astring &text)
returns true if the "text" is successfully pulled into this point.
Definition: point.h:216
numeric_type distance(const point &p2) const
Returns the distance between ‘this’ and the second point ‘p2’.
Definition: point.h:133
point & operator+=(const point &arg2)
Definition: point.h:186
numeric_type r() const
Definition: point.h:122
numeric_type x() const
Definition: point.h:47
point(numeric_type x=0, numeric_type y=0)
Definition: point.h:102
virtual void pack(basis::byte_array &packed_form) const
Creates a packed form of the packable object in "packed_form".
Definition: point.h:156
int packed_size() const
Estimates the space needed for the packed structure.
Definition: point.h:147
numeric_type magnitude() const
return the distance from the origin to this point.
Definition: point.h:171
numeric_type y() const
Definition: point.h:48
point & operator-=(const point &arg2)
Definition: point.h:190
DEFINE_CLASS_NAME("point")
double_angle theta() const
Definition: point.h:140
point rotate(const double_angle &theta) const
Rotates the point by the angle "theta".
Definition: point.h:204
Provides some fairly low-level math support.
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
basis::astring numeric_specifier(numeric_type t)
Guesses the formatting string needed for the type provided.
Definition: math_bits.h:69
astring crop_non_numeric(const astring &input)
Eats the non-numeric characters from the front of "input".
Definition: math_bits.cpp:37
astring crop_numeric(const astring &input)
Eats the numeric characters from the front of "input".
Definition: math_bits.cpp:23
void attach(byte_array &packed_form, const byte_array &to_attach)
Packs a byte_array "to_attach" into "packed_form".
void pack(basis::byte_array &packed_form, const set< contents > &to_pack)
provides a way to pack any set that stores packable objects.
Definition: set.h:131
bool detach(byte_array &packed_form, byte_array &to_detach)
Unpacks a byte_array "to_detach" from "packed_form".