Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

rect.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2002 Xavier Trochu
00003 
00004 This software is provided 'as-is', without any express or implied warranty. In no event
00005 will the authors be held liable for any damages arising from the use of this software.
00006 
00007 Permission is granted to anyone to use this software for any purpose, including commercial
00008 applications, and to alter it and redistribute it freely, subject to the following
00009 restrictions:
00010 
00011 1. The origin of this software must not be misrepresented; you must not claim that you
00012    wrote the original software. If you use this software in a product, an acknowledgment
00013    in the product documentation would be appreciated but is not required.
00014 
00015 2. Altered source versions must be plainly marked as such, and must not be misrepresented 
00016    as being the original software.
00017 
00018 3. This notice may not be removed or altered from any source distribution.
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 //~ bool rect::is_inside(const rect & r) const throw() {
00061     //~ return _topleft.get_x() >= r._topleft.get_x() && _bottomright.get_x() <= r._bottomright.get_x() && _topleft.get_y() >= r._topleft.get_y() && _bottomright.get_y() <= r._bottomright.get_y();
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 //~ bool rect::clip(rect & r) const throw(exception) {
00073     //~ // clip r in this, return true if clip performed
00074     //~ // throw an exception if the rect is invalid after the clip
00075     //~ if(!r.is_inside(*this)) {
00076         //~ ostringstream oss;
00077         //~ oss << "Invalid clip of " << r << " by " << *this;
00078         //~ throw logic_error(oss.str());
00079     //~ }
00080     //~ bool changed = false;
00081     //~ if(_topleft.get_x() > r._topleft.get_x()) { r._topleft.get_x() = _topleft.get_x(); changed = true; }
00082     //~ if(_topleft.get_y() > r._topleft.get_y()) { r._topleft.get_y() = _topleft.get_y(); changed = true; }
00083     //~ if(_bottomright.get_x() < r._bottomright.get_x()) { r._bottomright.get_x() = _bottomright.get_x(); changed = true; }
00084     //~ if(_bottomright.get_y() < r._bottomright.get_y()) { r._bottomright.get_y() = _bottomright.get_y(); changed = true; }
00085     //~ return changed;
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 

Generated on Fri Aug 9 22:54:29 2002 for san_player by doxygen1.2.16