X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Frect.cpp;h=c38c0741e08d2d08b0568afbd4e4034c45151c97;hb=3c890e51a9763ffcee49e15753453a7da248272b;hp=8207f234a3f397915c25344649462d02896971ac;hpb=e78daf62eb771ee29a59035d16cf63c1e6ebe144;p=architektonas diff --git a/src/rect.cpp b/src/rect.cpp index 8207f23..c38c074 100644 --- a/src/rect.cpp +++ b/src/rect.cpp @@ -12,27 +12,28 @@ // JLH 11/10/2016 Created this file // -#include "rect.h" -//#include +#include "rect.h" +#include Rect::Rect(): l(0), r(0), t(0), b(0) { } - -Rect::Rect(double ll, double rr, double tt, double bb): +Rect::Rect(double tt, double ll, double bb, double rr): l(ll), r(rr), t(tt), b(bb) { Normalize(); } - Rect::Rect(Point tl, Point br): l(tl.x), r(br.x), t(tl.y), b(br.y) { Normalize(); } +Rect::Rect(Point p): l(p.x), r(p.x), t(p.y), b(p.y) +{ +} Rect & Rect::operator*=(double scale) { @@ -40,10 +41,10 @@ Rect & Rect::operator*=(double scale) r *= scale; t *= scale; b *= scale; + return *this; } - Rect & Rect::operator|=(Rect r2) { //printf("operatore|=\nthis = (%lf, %lf, %lf, %lf), r = (%lf, %lf, %lf, %lf)\n", l, t, r, b, r2.l, r2.t, r2.r, r2.b); @@ -62,6 +63,44 @@ Rect & Rect::operator|=(Rect r2) return *this; } +Rect & Rect::operator+=(Point p) +{ + if (p.x < l) + l = p.x; + + if (p.x > r) + r = p.x; + + if (p.y < b) + b = p.y; + + if (p.y > t) + t = p.y; + + return *this; +} + +// +// We use this to give a rect an array-like access, which allows access of the +// rect in TLBR order (from 0 to 3). Also, values greater than 3 are treated +// as mod 4. +// +double & Rect::operator[](int idx) +{ + idx = idx % 4; + + switch (idx) + { + case 0: + return t; + case 1: + return l; + case 2: + return b; + } + + return r; +} void Rect::Normalize(void) { @@ -80,7 +119,6 @@ void Rect::Normalize(void) } } - void Rect::Translate(Point p) { l += p.x; @@ -89,7 +127,6 @@ void Rect::Translate(Point p) b += p.y; } - void Rect::Expand(double amt) { l -= amt; @@ -98,3 +135,42 @@ void Rect::Expand(double amt) b -= amt; } +double Rect::Width(void) +{ + return fabs(r - l); +} + +double Rect::Height(void) +{ + return fabs(t - b); +} + +bool Rect::Contains(Point p) +{ + return ((p.x >= l) && (p.x <= r) && (p.y >= b) && (p.y <= t) ? true : false); +} + +bool Rect::Contains(Rect rect) +{ + return ((rect.l >= l) && (rect.r <= r) && (rect.b >= b) && (rect.t <= t) ? true : false); +} + +Point Rect::TopLeft(void) +{ + return Point(l, t); +} + +Point Rect::TopRight(void) +{ + return Point(r, t); +} + +Point Rect::BottomLeft(void) +{ + return Point(l, b); +} + +Point Rect::BottomRight(void) +{ + return Point(r, b); +}