00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "KeosRectangle.h"
00022
00023 namespace Keos
00024 {
00025
00026
00027
00028
00029
00030
00031 CRectangle::CRectangle(const TVector2I& Start, const TVector2I& Size) :
00032 Origin(Start),
00033 End (Start + Size)
00034 {}
00035
00036
00037 CRectangle::CRectangle(int nLeft, int nTop, int nWidth, int nHeight) :
00038 Origin(nLeft, nTop),
00039 End (nLeft + nWidth, nTop + nHeight)
00040 {}
00041
00042
00043 void CRectangle::Set(int nLeft, int nTop, int nWidth, int nHeight)
00044 {
00045 Origin.Set(nLeft, nTop);
00046 End.Set(nLeft + nWidth, nTop + nHeight);
00047 }
00048
00049
00050 int CRectangle::Left() const
00051 {
00052 return Origin.x;
00053 }
00054
00055
00056 int CRectangle::Right() const
00057 {
00058 return End.x;
00059 }
00060
00061
00062 int CRectangle::Top() const
00063 {
00064 return Origin.y;
00065 }
00066
00067
00068 int CRectangle::Bottom() const
00069 {
00070 return End.y;
00071 }
00072
00073
00074 int CRectangle::Width() const
00075 {
00076 return End.x - Origin.x;
00077 }
00078
00079
00080 int CRectangle::Height() const
00081 {
00082 return End.y - Origin.y;
00083 }
00084
00085
00086 TVector2I CRectangle::Size() const
00087 {
00088 return End - Origin;
00089 }
00090
00091
00092 TIntersection CRectangle::Intersects(const TVector2I& Point) const
00093 {
00094 if ((Point.x >= Origin.x) && (Point.y >= Origin.y) && (Point.x <= End.x) && (Point.y <= End.y))
00095 return INT_IN;
00096 else
00097 return INT_OUT;
00098 }
00099
00100
00101 TIntersection CRectangle::Intersects(const CRectangle& Rect) const
00102 {
00103
00104 TVector2I Start(std::max(Origin.x, Rect.Origin.x), std::max(Origin.y, Rect.Origin.y));
00105 TVector2I End(std::min(End.x, Rect.End.x), std::min(End.y, Rect.End.y));
00106 CRectangle Overlap(Start, End - Start);
00107
00108 if ((Start.x > End.x) || (Start.y > End.y))
00109 return INT_OUT;
00110 else if ((Overlap == *this) || (Overlap == Rect))
00111 return INT_IN;
00112 else
00113 return INT_INTERSECTS;
00114 }
00115
00116
00117 bool CRectangle::operator ==(const CRectangle& Rect) const
00118 {
00119 return (Origin == Rect.Origin) && (End == Rect.End);
00120 }
00121
00122
00123 bool CRectangle::operator !=(const CRectangle& Rect) const
00124 {
00125 return !(*this == Rect);
00126 }
00127
00128
00129 std::istream& operator >>(std::istream& Stream, CRectangle& Rect)
00130 {
00131 return Stream >> Rect.Origin >> Rect.End;
00132 }
00133
00134
00135 std::ostream& operator <<(std::ostream& Stream, const CRectangle& Rect)
00136 {
00137 return Stream << Rect.Origin << " " << Rect.End;
00138 }
00139
00140 }