00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
#include "koRect.h"
00020
00021
KoRect KoRect::normalize()
const
00022
{
00023
KoRect r;
00024
if ( right() < left() ) {
00025 r.
m_tl.
setX( right() );
00026 r.
m_br.
setX( left() );
00027 }
else {
00028 r.
m_tl.
setX( left() );
00029 r.
m_br.
setX( right() );
00030 }
00031
if ( bottom() < top() ) {
00032 r.
m_tl.
setY( bottom() );
00033 r.
m_br.
setY( top() );
00034 }
else {
00035 r.
m_tl.
setY( top() );
00036 r.
m_br.
setY( bottom() );
00037 }
00038
return r;
00039 }
00040
00041
KoPoint KoRect::center()
const
00042
{
00043
return KoPoint((left() + right()) / 2, (top() + bottom()) / 2);
00044 }
00045
00046
void KoRect::moveTopLeft(
const KoPoint &topleft)
00047 {
00048 m_br.
rx() += topleft.
x() - m_tl.
x();
00049 m_br.
ry() += topleft.
y() - m_tl.
y();
00050 m_tl = topleft;
00051 }
00052
00053
void KoRect::moveBottomRight(
const KoPoint &bottomright)
00054 {
00055 m_tl.
rx() += bottomright.
x() - m_br.
x();
00056 m_tl.
ry() += bottomright.
y() - m_br.
y();
00057 m_br = bottomright;
00058 }
00059
00060
void KoRect::moveTopRight(
const KoPoint &topright)
00061 {
00062 m_tl.
rx() += topright.
x() - m_br.
x();
00063 m_br.
ry() += topright.
y() - m_tl.
y();
00064 m_br.
rx() = topright.
x();
00065 m_tl.
ry() = topright.
y();
00066 }
00067
00068
void KoRect::moveBottomLeft(
const KoPoint &bottomleft)
00069 {
00070 m_br.
rx() += bottomleft.
x() - m_tl.
x();
00071 m_tl.
ry() += bottomleft.
y() - m_br.
y();
00072 m_tl.
rx() = bottomleft.
x();
00073 m_br.
ry() = bottomleft.
y();
00074 }
00075
00076
void KoRect::moveBy(
const double &dx,
const double &dy)
00077 {
00078 m_tl.
rx() += dx;
00079 m_tl.
ry() += dy;
00080 m_br.
rx() += dx;
00081 m_br.
ry() += dy;
00082 }
00083
00084
void KoRect::setRect(
const double &x,
const double &y,
const double &width,
const double &height)
00085 {
00086 m_tl.
setCoords( x, y );
00087 m_br.
setCoords( x + width, y + height );
00088 }
00089
00090
void KoRect::setRect(
const KoRect &rect)
00091 {
00092 m_tl = rect.
m_tl;
00093 m_br = rect.
m_br;
00094 }
00095
00096
void KoRect::setCoords(
const double &x1,
const double &y1,
const double &x2,
const double &y2)
00097 {
00098 m_tl.
setCoords( x1, y1 );
00099 m_br.
setCoords( x2, y2 );
00100 }
00101
00102
void KoRect::setSize(
const KoSize &size)
00103 {
00104 setWidth(size.
width());
00105 setHeight(size.
height());
00106 }
00107
00108
KoSize KoRect::size()
const
00109
{
00110
return KoSize(width(), height());
00111 }
00112
00113
KoRect &KoRect::operator|=(
const KoRect &rhs) {
00114
00115
if(rhs.
isEmpty())
00116
return *
this;
00117
if(isEmpty())
00118 {
00119 *
this = rhs;
00120
return *
this;
00121 }
00122
if(m_tl.
x() > rhs.
left())
00123 m_tl.
setX(rhs.
left());
00124
if(m_tl.
y() > rhs.
top())
00125 m_tl.
setY(rhs.
top());
00126
if(m_br.
x() < rhs.
right())
00127 m_br.
setX(rhs.
right());
00128
if(m_br.
y() < rhs.
bottom())
00129 m_br.
setY(rhs.
bottom());
00130
return *
this;
00131 }
00132
00133
KoRect &KoRect::operator&=(
const KoRect &rhs) {
00134
00135
if(m_tl.
x() < rhs.
left())
00136 m_tl.
setX(rhs.
left());
00137
if(m_tl.
y() < rhs.
top())
00138 m_tl.
setY(rhs.
top());
00139
if(m_br.
x() > rhs.
right())
00140 m_br.
setX(rhs.
right());
00141
if(m_br.
y() > rhs.
bottom())
00142 m_br.
setY(rhs.
bottom());
00143
return *
this;
00144 }
00145
00146
bool KoRect::contains(
const KoPoint &p,
bool proper)
const {
00147
00148
if(proper)
00149
return (p.
x() > m_tl.
x() && p.
x() < m_br.
x() && p.
y() > m_tl.
y() && p.
y() < m_br.
y());
00150
else
00151
return (p.
x() >= m_tl.
x() && p.
x() <= m_br.
x() && p.
y() >= m_tl.
y() && p.
y() <= m_br.
y());
00152 }
00153
00154
bool KoRect::contains(
const double &x,
const double &y,
bool proper)
const {
00155
00156
if(proper)
00157
return (x > m_tl.
x() && x < m_br.
x() && y > m_tl.
y() && y < m_br.
y());
00158
else
00159
return (x >= m_tl.
x() && x <= m_br.
x() && y >= m_tl.
y() && y <= m_br.
y());
00160 }
00161
00162
bool KoRect::contains(
const KoRect &r,
bool proper)
const {
00163
00164
if(proper)
00165
return (r.
left() > m_tl.
x() && r.
right() < m_br.
x() && r.
top() > m_tl.
y() && r.
bottom() < m_br.
y());
00166
else
00167
return (r.
left() >= m_tl.
x() && r.
right() <= m_br.
x() && r.
top() >= m_tl.
y() && r.
bottom() <= m_br.
y());
00168 }
00169
00170
00171
KoRect KoRect::unite(
const KoRect &r)
const {
00172
return *
this | r;
00173 }
00174
00175
KoRect KoRect::intersect(
const KoRect &r)
const {
00176
return *
this & r;
00177 }
00178
00179
bool KoRect::intersects(
const KoRect &r)
const {
00180
return ( QMAX(m_tl.
x(), r.
left()) <= QMIN(m_br.
x(), r.
right()) &&
00181 QMAX(m_tl.
y(), r.
top()) <= QMIN(m_br.
y(), r.
bottom()) );
00182 }
00183
00184
KoRect operator|(
const KoRect &lhs,
const KoRect &rhs) {
00185
00186
if(lhs.
isEmpty())
00187
return rhs;
00188
if(rhs.
isEmpty())
00189
return lhs;
00190
KoRect tmp;
00191 tmp.
setCoords( (lhs.
left() < rhs.
left() ? lhs.
left() : rhs.left()),
00192 (lhs.top() < rhs.top() ? lhs.top() : rhs.top()),
00193 (lhs.right() > rhs.right() ? lhs.right() : rhs.right()),
00194 (lhs.bottom() > rhs.bottom() ? lhs.bottom() : rhs.bottom()) );
00195
return tmp;
00196 }
00197
00198
KoRect operator&(
const KoRect &lhs,
const KoRect &rhs) {
00199
00200
KoRect tmp;
00201 tmp.
setCoords( (lhs.
left() > rhs.
left() ? lhs.
left() : rhs.left()),
00202 (lhs.top() > rhs.top() ? lhs.top() : rhs.top()),
00203 (lhs.right() < rhs.right() ? lhs.right() : rhs.right()),
00204 (lhs.bottom() < rhs.bottom() ? lhs.bottom() : rhs.bottom()) );
00205
return tmp;
00206 }
00207
00208
bool operator==(
const KoRect &lhs,
const KoRect &rhs) {
00209
return ( lhs.
topLeft()==rhs.
topLeft() &&
00210 lhs.
bottomRight()==rhs.
bottomRight() );
00211 }
00212
00213
bool operator!=(
const KoRect &lhs,
const KoRect &rhs) {
00214
return ( lhs.
topLeft()!=rhs.
topLeft() ||
00215 lhs.
bottomRight()!=rhs.
bottomRight() );
00216 }
00217
00218
KoRect KoRect::transform(
const QWMatrix &m)
const
00219
{
00220
KoRect result;
00221
if(m.m12() == 0.0F && m.m21() == 0.0F)
00222 {
00223 result =
KoRect(topLeft().transform(m), bottomRight().transform(m));
00224 }
00225
else
00226 {
00227
int i;
00228
KoPoint p[4] = {
KoPoint(m_tl.
x(), m_tl.
y()),
KoPoint(m_tl.
x(), m_br.
x()),
00229
KoPoint(m_br.
x(), m_br.
x()),
KoPoint(m_br.
x(), m_tl.
y()) };
00230
for(i = 0; i < 4; i++)
00231 p[i] = p[i].
transform(m);
00232
00233 result.
setLeft(p[0].x());
00234 result.
setTop(p[0].y());
00235 result.
setRight(p[0].x());
00236 result.
setBottom(p[0].y());
00237
00238
for(
int i = 1; i < 4; i++)
00239 {
00240 result.
setLeft(QMIN(p[i].x(), result.
left()));
00241 result.
setTop(QMIN(p[i].y(), result.
top()));
00242 result.
setRight(QMAX(p[i].x(), result.
right()));
00243 result.
setBottom(QMAX(p[i].y(), result.
bottom()));
00244 }
00245 }
00246
return result;
00247 }
00248
00249 KoRect KoRect::translate(
double dx,
double dy)
const
00250
{
00251
return KoRect(left() + dx, top() + dy, width(), height());
00252 }
00253
00254
QRect KoRect::toQRect()
const
00255
{
00256
return QRect( qRound( left() ), qRound( top() ), qRound( width() ), qRound( height() ) );
00257 }
00258
00259
00260 KoRect KoRect::fromQRect(
const QRect &rect )
00261 {
00262
return KoRect( rect.left(), rect.top(), rect.width(), rect.height() );
00263 }