1 #ifndef RECTANGLE_CLASS
2 #define RECTANGLE_CLASS
26 template <
class numeric_type>
32 rectangle(numeric_type x_1 = 0, numeric_type y_1 = 0,
33 numeric_type x_2 = 0, numeric_type y_2 = 0);
138 template <
class numeric_type>
140 : _vertex_1(lb), _vertex_2(rt) {}
142 template <
class numeric_type>
144 : _vertex_1(
point<numeric_type>(left, bottom)),
145 _vertex_2(
point<numeric_type>(right, top)) {}
147 template <
class numeric_type>
149 {
return _vertex_1; }
151 template <
class numeric_type>
153 {
return _vertex_2; }
155 template <
class numeric_type>
157 { _vertex_1 = to_set; }
159 template <
class numeric_type>
161 { _vertex_2 = to_set; }
163 template <
class numeric_type>
167 template <
class numeric_type>
171 template <
class numeric_type>
175 template <
class numeric_type>
179 template <
class numeric_type>
183 template <
class numeric_type>
187 template <
class numeric_type>
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();
199 template <
class numeric_type>
206 template <
class numeric_type>
213 template <
class numeric_type>
220 template <
class numeric_type>
227 template <
class numeric_type>
231 + _vertex_2.x()) / 2.0), numeric_type((_vertex_1.y()
232 + _vertex_2.y()) / 2.0));
235 template <
class numeric_type>
239 return bool( (to_check.
x() >= ordered_me.
_vertex_1.x())
242 && (to_check.
y() <= ordered_me.
_vertex_2.y()) );
245 template <
class numeric_type>
252 if ( (min1 == min2) && (max1 == max2) )
return true;
256 template <
class numeric_type>
258 { _vertex_1 += p; _vertex_2 += p;
return *
this; }
260 template <
class numeric_type>
262 { _vertex_1 -= p; _vertex_2 -= p;
return *
this; }
264 template <
class numeric_type>
272 template <
class contents>
281 template <
class contents>
284 _vertex_1.pack(packed_form);
285 _vertex_2.pack(packed_form);
288 template <
class contents>
291 if (!_vertex_1.unpack(packed_form))
return false;
292 if (!_vertex_2.unpack(packed_form))
return false;
296 template <
class numeric_type>
304 template <
class numeric_type>
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());
317 template <
class numeric_type>
323 || (minimum_y() > r2.
maximum_y()) )
return true;
327 template <
class numeric_type>
329 {
return bool(!disjoint(r2)); }
331 template <
class numeric_type>
334 if (disjoint(r2))
return false;
340 template <
class numeric_type>
343 if (disjoint(r2))
return false;
351 template <
class numeric_type>
358 template <
class numeric_type>
361 numeric_type nums[4] = { 0, 0, 0, 0 };
366 for (
int i = 0; i < 4; i++) {
368 nums[i] = text.
convert(nums[i]);
int length() const
Returns the current reported length of the allocated C array.
Provides a dynamically resizable ASCII character string.
int convert(int default_value) const
Converts the string into a corresponding integer.
virtual void text_form(base_string &state_fill) const
Provides a text view of all the important info owned by this object.
A very common template for a dynamic array of bytes.
A base class for objects that can pack into an array of bytes.
Represents a geometric point.
Represents a geometric rectangle.
point< numeric_type > top_left() const
numeric_type minimum_y() const
Return the smallest y from the points in the rectangle.
numeric_type height() const
bool intersect(const rectangle &r2) const
Returns true if ‘this’ & ‘r2’ cover any common points.
numeric_type width() const
bool inside(const point< numeric_type > &to_check) const
Returns true if ‘to_check’ is inside ‘this’ rectangle.
void vertex_1(const point< numeric_type > &to_set)
numeric_type maximum_y() const
Return the largest y from the points in the rectangle.
basis::astring text_form() const
Prints out the contents of the rectangle.
void vertex_2(const point< numeric_type > &to_set)
virtual void pack(basis::byte_array &packed_form) const
Creates a packed form of the packable object in "packed_form".
point< numeric_type > center() const
Returns the point at the center of the rectangle.
point< numeric_type > vertex_1() const
virtual bool unpack(basis::byte_array &packed_form)
Restores the packable from the "packed_form".
bool join_intersecting(const rectangle &r2, rectangle &result)
Sets "result" to encompass this and "r2" if they intersect.
bool from_text(const basis::astring &text)
Returns true if the "text" is parsed into this rectangle.
rectangle operator+(const point< numeric_type > &to_add) const
Returns the rectangle resulting from adding a point to its vertices.
bool disjoint(const rectangle &r2) const
Returns true if ‘this’ & ‘r2’ have mutually exclusive extents.
point< numeric_type > bottom_left() const
numeric_type minimum_x() const
Return the smallest x from the points in the rectangle.
rectangle operator-(const point< numeric_type > &to_subtract) const
Returns the rectangle resulting from subtracting "to_subtract".
point< numeric_type > vertex_2() const
rectangle & operator+=(const point< numeric_type > &to_add)
Adds the point "to_add" to our vertices.
void encompass(const rectangle &to_adjust_to)
Finds the largest dimension needed to contain all rectangles passed in.
rectangle & operator-=(const point< numeric_type > &to_subtract)
Subtracts the point "to_add" to our vertices.
point< numeric_type > bottom_right() const
returns the appropriate point as represented by our rectangle.
rectangle order() const
Re-orders the vertices of the line to be increasing.
virtual int packed_size() const
Estimates the space needed for the packed structure.
point< numeric_type > top_right() const
point< numeric_type > _vertex_2
rectangle(numeric_type x_1=0, numeric_type y_1=0, numeric_type x_2=0, numeric_type y_2=0)
bool intersection(const rectangle &r2, rectangle &result)
Sets "result" to the intersection of this and "r2".
rectangle(const point< numeric_type > &vertex_1, const point< numeric_type > &vertex_2)
point< numeric_type > _vertex_1
numeric_type maximum_x() const
Return the largest x from the points in the rectangle.
bool operator==(const rectangle &to_compare) const
Returns true if ‘to_compare’ has vertices equal to ‘this’.
type maximum(type a, type b)
minimum returns the lesser of two values.
void flip_increasing(type &a, type &b)
Makes sure that two values are in increasing order (a < b).
type minimum(type a, type b)
maximum returns the greater of two values.
type absolute_value(type a)
Returns a if a is non-negative, and returns -a otherwise.
Contains all of our objects for geometry and avoids name clashes.
rectangle< int > int_rectangle
< A commonly used rectangle of integers.
basis::astring numeric_specifier(numeric_type t)
Guesses the formatting string needed for the type provided.
astring crop_non_numeric(const astring &input)
Eats the non-numeric characters from the front of "input".
astring crop_numeric(const astring &input)
Eats the numeric characters from the front of "input".
void pack(basis::byte_array &packed_form, const set< contents > &to_pack)
provides a way to pack any set that stores packable objects.