00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <sstream>
00021 #include <stdexcept>
00022 #include "rect.h"
00023 #include <iostream>
00024
00025 using namespace std;
00026
00027 rect::rect() throw() : _topleft(0, 0), _bottomright(0,0) {
00028 }
00029
00030 rect::rect(int x, int y) throw(exception) : _topleft(0, 0), _bottomright(x, y) {
00031 check();
00032 }
00033
00034 rect::rect(int x1, int y1, int x2, int y2) throw(exception) : _topleft(x1, y1), _bottomright(x2, y2) {
00035 check();
00036 }
00037
00038 rect::rect(const rect & r) throw() : _topleft(r._topleft), _bottomright(r._bottomright) {
00039 }
00040
00041 rect::rect & rect::operator=(const rect & r) throw() {
00042 _topleft = r._topleft;
00043 _bottomright = r._bottomright;
00044 return *this;
00045 }
00046
00047 bool rect::operator==(const rect & r) const throw() {
00048 return _topleft == r._topleft && _bottomright == r._bottomright;
00049 }
00050
00051 void rect::check() throw(exception) {
00052 if ((_topleft.get_x() < 0) || (_bottomright.get_x() < _topleft.get_x()) || (_topleft.get_y() < 0) || (_bottomright.get_y() < _topleft.get_y()))
00053 {
00054 ostringstream oss;
00055 oss << "Invalid rect : " << *this;
00056 throw logic_error(oss.str());
00057 }
00058 }
00059
00060
00061
00062
00063
00064 bool rect::is_inside(int x, int y) const throw() {
00065 return _topleft.get_x() >= x && _bottomright.get_x() < x && _topleft.get_y() >= y && _bottomright.get_y() < y;
00066 }
00067
00068 bool rect::is_inside(const point & p) const throw() {
00069 return (left() <= p.get_x()) && (right() > p.get_x()) && (top() <= p.get_y()) && (bottom() > p.get_y());
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 ostream & operator<<(ostream & o, const rect & r) {
00089 o << "[" << r.left() << "-" << r.top() << ";" << r.right() << "-" << r.bottom() <<"]";
00090 return o;
00091 }
00092
00093 ostream & operator<<(ostream & o, const point & p) {
00094 o << "(" << p.get_x() << "-" << p.get_y() <<")";
00095 return o;
00096 }
00097
00098