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
31namespace geometric {
32
34
35template <class numeric_type>
36class point : public basis::packable, public virtual basis::root_object
37{
38public:
41
43
46
47 numeric_type x() const { return _x; }
48 numeric_type y() const { return _y; }
49
50 numeric_type r() const;
52
55
62
63 point operator - () const { return point<numeric_type>(-_x, -_y); }
65
68
69 point operator + (const point &arg2) const;
70 point operator - (const point &arg2) const;
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;
83 int packed_size() const;
84
85private:
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
101template <class numeric_type>
103
104template <class numeric_type>
107
108template <class numeric_type>
116
117template <class numeric_type>
119{ _x = x; _y = y; }
120
121template <class numeric_type>
123{
124 const double sumsquar = basis::square(x()) + basis::square(y());
125 return numeric_type(sqrt(sumsquar));
126}
127
128template <class numeric_type>
131
132template <class numeric_type>
134{
135 const double sumsquar = basis::square(p2.x() - x()) + basis::square(p2.y() - y());
136 return numeric_type(sqrt(sumsquar));
137}
138
139template <class numeric_type>
145
146template <class contents>
148{
150//hmmm: inefficient!
151 pack(temp);
152 return temp.length();
153}
154
155template <class contents>
161
162template <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
170template <class numeric_type>
172{
173 const double sumsquar = basis::square(x()) + basis::square(y());
174 return numeric_type(sqrt(sumsquar));
175}
176
177template <class numeric_type>
180
181template <class numeric_type>
184
185template <class numeric_type>
187{ _x += arg2.x(); _y += arg2.y(); return *this; }
188
189template <class numeric_type>
191{ _x -= arg2.x(); _y -= arg2.y(); return *this; }
192
193template <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
202template <class numeric_type>
204 (const double_angle &theta) const
205{
206 numeric_type tempX = x();
207 numeric_type tempY = y();
209 - tempY * theta.sine());
211 + tempY * theta.cosine());
213}
214
215template <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
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
int convert(int default_value) const
Converts the string into a corresponding integer.
Definition astring.cpp:760
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
virtual void pack(basis::byte_array &packed_form) const
packs the angle for shipping in bytes.
Definition angle.h:170
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
void set(contents a, angular_units unit)
sets the angle to a new rotation "a" in the "unit".
Definition angle.h:198
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".
bool detach(byte_array &packed_form, byte_array &to_detach)
Unpacks a byte_array "to_detach" from "packed_form".