feisty meow concerns codebase  2.140
rectangle.h
Go to the documentation of this file.
1 #ifndef RECTANGLE_CLASS
2 #define RECTANGLE_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : rectangle *
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 "point.h"
19 
20 #include <basis/functions.h>
21 
22 namespace geometric {
23 
25 
26 template <class numeric_type>
27 class rectangle : public basis::packable
28 {
29 public:
32  rectangle(numeric_type x_1 = 0, numeric_type y_1 = 0,
33  numeric_type x_2 = 0, numeric_type y_2 = 0);
34 
35  numeric_type height() const;
36  numeric_type width() const;
37 
38  rectangle order() const;
40 
49 
54  numeric_type minimum_x() const;
56  numeric_type minimum_y() const;
58  numeric_type maximum_x() const;
60  numeric_type maximum_y() const;
62 
65 
66  bool inside(const point<numeric_type> &to_check) const;
68 
69  bool operator == (const rectangle &to_compare) const;
71 
74  rectangle operator - (const point<numeric_type> &to_subtract) const;
76 
81 
82  void encompass(const rectangle &to_adjust_to);
84 
89  bool intersect(const rectangle &r2) const;
91 
92  bool disjoint(const rectangle &r2) const;
94 
95  bool join_intersecting(const rectangle &r2, rectangle &result);
97 
101  bool intersection(const rectangle &r2, rectangle &result);
103 
109 
110  bool from_text(const basis::astring &text);
112 
115 
116  void vertex_1(const point<numeric_type> &to_set);
117  void vertex_2(const point<numeric_type> &to_set);
118 
119  virtual int packed_size() const;
120  virtual void pack(basis::byte_array &packed_form) const;
121  virtual bool unpack(basis::byte_array &packed_form);
122 
123 protected:
126 };
127 
129 
131 
133 
135 
136 // implementations below...
137 
138 template <class numeric_type>
140 : _vertex_1(lb), _vertex_2(rt) {}
141 
142 template <class numeric_type>
143 rectangle<numeric_type>::rectangle(numeric_type left, numeric_type bottom, numeric_type right, numeric_type top)
144 : _vertex_1(point<numeric_type>(left, bottom)),
145  _vertex_2(point<numeric_type>(right, top)) {}
146 
147 template <class numeric_type>
149 { return _vertex_1; }
150 
151 template <class numeric_type>
153 { return _vertex_2; }
154 
155 template <class numeric_type>
157 { _vertex_1 = to_set; }
158 
159 template <class numeric_type>
161 { _vertex_2 = to_set; }
162 
163 template <class numeric_type>
165 { return absolute_value(_vertex_2.y() - _vertex_1.y()); }
166 
167 template <class numeric_type>
168 numeric_type rectangle<numeric_type>::width() const
169 { return absolute_value(_vertex_2.x() - _vertex_1.x()); }
170 
171 template <class numeric_type>
173 { return basis::minimum(_vertex_1.x(), _vertex_2.x()); }
174 
175 template <class numeric_type>
177 { return basis::minimum(_vertex_1.y(), _vertex_2.y()); }
178 
179 template <class numeric_type>
181 { return basis::maximum(_vertex_1.x(), _vertex_2.x()); }
182 
183 template <class numeric_type>
185 { return basis::maximum(_vertex_1.y(), _vertex_2.y()); }
186 
187 template <class numeric_type>
189 {
190  numeric_type x1 = _vertex_1.x();
191  numeric_type x2 = _vertex_2.x();
192  numeric_type y1 = _vertex_1.y();
193  numeric_type y2 = _vertex_2.y();
194  basis::flip_increasing(x1, x2);
195  basis::flip_increasing(y1, y2);
196  return rectangle<numeric_type>(x1, y1, x2, y2);
197 }
198 
199 template <class numeric_type>
201 {
202  rectangle temp(order());
203  return point<numeric_type>(temp.vertex_1().x(), temp.vertex_2().y());
204 }
205 
206 template <class numeric_type>
208 {
209  rectangle temp(order());
210  return point<numeric_type>(temp.vertex_1().x(), temp.vertex_1().y());
211 }
212 
213 template <class numeric_type>
215 {
216  rectangle temp(order());
217  return point<numeric_type>(temp.vertex_2().x(), temp.vertex_2().y());
218 }
219 
220 template <class numeric_type>
222 {
223  rectangle temp(order());
224  return point<numeric_type>(temp.vertex_2().x(), temp.vertex_1().y());
225 }
226 
227 template <class numeric_type>
229 {
230  return point<numeric_type>(numeric_type((_vertex_1.x()
231  + _vertex_2.x()) / 2.0), numeric_type((_vertex_1.y()
232  + _vertex_2.y()) / 2.0));
233 }
234 
235 template <class numeric_type>
237 {
238  rectangle<numeric_type> ordered_me = this->order();
239  return bool( (to_check.x() >= ordered_me._vertex_1.x())
240  && (to_check.x() <= ordered_me._vertex_2.x())
241  && (to_check.y() >= ordered_me._vertex_1.y())
242  && (to_check.y() <= ordered_me._vertex_2.y()) );
243 }
244 
245 template <class numeric_type>
247 {
248  point<numeric_type> min1(minimum_x(), minimum_y());
249  point<numeric_type> max1(maximum_x(), maximum_y());
250  point<numeric_type> min2(to_compare.minimum_x(), to_compare.minimum_y());
251  point<numeric_type> max2(to_compare.maximum_x(), to_compare.maximum_y());
252  if ( (min1 == min2) && (max1 == max2) ) return true;
253  else return false;
254 }
255 
256 template <class numeric_type>
258 { _vertex_1 += p; _vertex_2 += p; return *this; }
259 
260 template <class numeric_type>
262 { _vertex_1 -= p; _vertex_2 -= p; return *this; }
263 
264 template <class numeric_type>
266 {
267  rectangle to_return(*this);
268  to_return += p;
269  return to_return;
270 }
271 
272 template <class contents>
274 {
275  basis::byte_array temp;
276 //hmmm: inefficient!
277  pack(temp);
278  return temp.length();
279 }
280 
281 template <class contents>
283 {
284  _vertex_1.pack(packed_form);
285  _vertex_2.pack(packed_form);
286 }
287 
288 template <class contents>
290 {
291  if (!_vertex_1.unpack(packed_form)) return false;
292  if (!_vertex_2.unpack(packed_form)) return false;
293  return true;
294 }
295 
296 template <class numeric_type>
298 {
299  rectangle to_return(*this);
300  to_return -= p;
301  return to_return;
302 }
303 
304 template <class numeric_type>
306 {
307  if (to_adjust_to._vertex_1.x() < _vertex_1.x())
308  _vertex_1.set(to_adjust_to._vertex_1.x(), _vertex_1.y());
309  if (to_adjust_to._vertex_1.y() < _vertex_1.y())
310  _vertex_1.set(_vertex_1.x(), to_adjust_to._vertex_1.y());
311  if (to_adjust_to._vertex_2.x() > _vertex_2.x())
312  _vertex_2.set(to_adjust_to._vertex_2.x(), _vertex_2.y());
313  if (to_adjust_to._vertex_2.y() > _vertex_2.y())
314  _vertex_2.set(_vertex_2.x(), to_adjust_to._vertex_2.y());
315 }
316 
317 template <class numeric_type>
319 {
320  if ( (maximum_x() < r2.minimum_x())
321  || (minimum_x() > r2.maximum_x())
322  || (maximum_y() < r2.minimum_y())
323  || (minimum_y() > r2.maximum_y()) ) return true;
324  else return false;
325 }
326 
327 template <class numeric_type>
329 { return bool(!disjoint(r2)); }
330 
331 template <class numeric_type>
333 {
334  if (disjoint(r2)) return false;
335  result = *this;
336  result.encompass(r2);
337  return true;
338 }
339 
340 template <class numeric_type>
342 {
343  if (disjoint(r2)) return false;
344  result = rectangle<numeric_type>(basis::maximum(minimum_x(), r2.minimum_x()),
345  basis::maximum(minimum_y(), r2.minimum_y()),
346  basis::minimum(maximum_x(), r2.maximum_x()),
347  basis::minimum(maximum_y(), r2.maximum_y()));
348  return true;
349 }
350 
351 template <class numeric_type>
353 {
354  return basis::astring("[") + _vertex_1.text_form() + basis::astring(" ")
355  + _vertex_2.text_form() + basis::astring("]");
356 }
357 
358 template <class numeric_type>
360 {
361  numeric_type nums[4] = { 0, 0, 0, 0 };
362  // setup the scanning specifier.
363  basis::astring spec(numeric_specifier(nums[0]));
364  // scan the string for values.
365  basis::astring text(_text);
366  for (int i = 0; i < 4; i++) {
367  text = crop_non_numeric(text);
368  nums[i] = text.convert(nums[i]);
369  text = crop_numeric(text);
370  }
371  vertex_1(point<numeric_type>(nums[0], nums[1]));
372  vertex_2(point<numeric_type>(nums[2], nums[3]));
373  return true;
374 }
375 
376 } // namespace.
377 
378 #endif
379 
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
int convert(int default_value) const
Converts the string into a corresponding integer.
Definition: astring.cpp:757
virtual void text_form(base_string &state_fill) const
Provides a text view of all the important info owned by this object.
Definition: astring.cpp:130
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
A base class for objects that can pack into an array of bytes.
Definition: byte_array.h:87
Represents a geometric point.
Definition: point.h:37
numeric_type x() const
Definition: point.h:47
numeric_type y() const
Definition: point.h:48
Represents a geometric rectangle.
Definition: rectangle.h:28
point< numeric_type > top_left() const
Definition: rectangle.h:200
numeric_type minimum_y() const
Return the smallest y from the points in the rectangle.
Definition: rectangle.h:176
numeric_type height() const
Definition: rectangle.h:164
bool intersect(const rectangle &r2) const
Returns true if ‘this’ & ‘r2’ cover any common points.
Definition: rectangle.h:328
numeric_type width() const
Definition: rectangle.h:168
bool inside(const point< numeric_type > &to_check) const
Returns true if ‘to_check’ is inside ‘this’ rectangle.
Definition: rectangle.h:236
void vertex_1(const point< numeric_type > &to_set)
Definition: rectangle.h:156
numeric_type maximum_y() const
Return the largest y from the points in the rectangle.
Definition: rectangle.h:184
basis::astring text_form() const
Prints out the contents of the rectangle.
Definition: rectangle.h:352
void vertex_2(const point< numeric_type > &to_set)
Definition: rectangle.h:160
virtual void pack(basis::byte_array &packed_form) const
Creates a packed form of the packable object in "packed_form".
Definition: rectangle.h:282
point< numeric_type > center() const
Returns the point at the center of the rectangle.
Definition: rectangle.h:228
point< numeric_type > vertex_1() const
Definition: rectangle.h:148
virtual bool unpack(basis::byte_array &packed_form)
Restores the packable from the "packed_form".
Definition: rectangle.h:289
bool join_intersecting(const rectangle &r2, rectangle &result)
Sets "result" to encompass this and "r2" if they intersect.
Definition: rectangle.h:332
bool from_text(const basis::astring &text)
Returns true if the "text" is parsed into this rectangle.
Definition: rectangle.h:359
rectangle operator+(const point< numeric_type > &to_add) const
Returns the rectangle resulting from adding a point to its vertices.
Definition: rectangle.h:265
bool disjoint(const rectangle &r2) const
Returns true if ‘this’ & ‘r2’ have mutually exclusive extents.
Definition: rectangle.h:318
point< numeric_type > bottom_left() const
Definition: rectangle.h:207
numeric_type minimum_x() const
Return the smallest x from the points in the rectangle.
Definition: rectangle.h:172
rectangle operator-(const point< numeric_type > &to_subtract) const
Returns the rectangle resulting from subtracting "to_subtract".
Definition: rectangle.h:297
point< numeric_type > vertex_2() const
Definition: rectangle.h:152
rectangle & operator+=(const point< numeric_type > &to_add)
Adds the point "to_add" to our vertices.
Definition: rectangle.h:257
void encompass(const rectangle &to_adjust_to)
Finds the largest dimension needed to contain all rectangles passed in.
Definition: rectangle.h:305
rectangle & operator-=(const point< numeric_type > &to_subtract)
Subtracts the point "to_add" to our vertices.
Definition: rectangle.h:261
point< numeric_type > bottom_right() const
returns the appropriate point as represented by our rectangle.
Definition: rectangle.h:221
rectangle order() const
Re-orders the vertices of the line to be increasing.
Definition: rectangle.h:188
virtual int packed_size() const
Estimates the space needed for the packed structure.
Definition: rectangle.h:273
point< numeric_type > top_right() const
Definition: rectangle.h:214
point< numeric_type > _vertex_2
Definition: rectangle.h:125
rectangle(numeric_type x_1=0, numeric_type y_1=0, numeric_type x_2=0, numeric_type y_2=0)
Definition: rectangle.h:143
bool intersection(const rectangle &r2, rectangle &result)
Sets "result" to the intersection of this and "r2".
Definition: rectangle.h:341
rectangle(const point< numeric_type > &vertex_1, const point< numeric_type > &vertex_2)
Definition: rectangle.h:139
point< numeric_type > _vertex_1
Definition: rectangle.h:124
numeric_type maximum_x() const
Return the largest x from the points in the rectangle.
Definition: rectangle.h:180
bool operator==(const rectangle &to_compare) const
Returns true if ‘to_compare’ has vertices equal to ‘this’.
Definition: rectangle.h:246
type maximum(type a, type b)
minimum returns the lesser of two values.
Definition: functions.h:26
void flip_increasing(type &a, type &b)
Makes sure that two values are in increasing order (a < b).
Definition: functions.h:95
type minimum(type a, type b)
maximum returns the greater of two values.
Definition: functions.h:29
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
rectangle< int > int_rectangle
< A commonly used rectangle of integers.
Definition: rectangle.h:132
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 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