You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
6.2 KiB
C++

#include "MLRect.h"
#include "MLPoint.h"
#include "MLPlNode.h"
#include "MLPline.h"
#include "MLFuncom.h"
MLRect::MLRect(void)
{
memset(m_name, 0, 32);
m_left = 0.0;
m_top = 0.0;
m_right = 0.0;
m_bottom = 0.0;
m_valid = false;
}
MLRect::MLRect( double left, double top, double right, double bottom )
{
memset(m_name, 0, 32);
m_left = left;
m_top = top;
m_right = right;
m_bottom = bottom;
m_valid = true;
}
MLRect::MLRect( const MLVector& c1, const MLVector& c2 )
{
memset(m_name, 0, 32);
m_left = MIN(c1.x, c2.x);
m_right= MAX(c1.x, c2.x);
m_bottom = MIN(c1.y, c2.y);
m_top = MAX(c1.y, c2.y);
m_valid = true;
}
MLRect::MLRect( const MLPoint& c1, const MLPoint& c2 )
{
memset(m_name, 0, 32);
m_left = MIN(c1.x, c2.x);
m_right= MAX(c1.x, c2.x);
m_bottom = MIN(c1.y, c2.y);
m_top = MAX(c1.y, c2.y);
m_valid = true;
}
MLRect::MLRect( const MLPlNode& c1, const MLPlNode& c2 )
{
memset(m_name, 0, 32);
m_left = MIN(c1.x, c2.x);
m_right= MAX(c1.x, c2.x);
m_bottom = MIN(c1.y, c2.y);
m_top = MAX(c1.y, c2.y);
m_valid = true;
}
MLRect::MLRect(const MLRect& dRect)
{
memcpy(m_name, dRect.name, 32);
m_left = dRect.left;
m_right= dRect.right;
m_bottom = dRect.bottom;
m_top = dRect.top;
m_valid = true;
}
MLRect::MLRect( const QRectF& rect )
{
memset(m_name, 0, 32);
m_left = rect.left();
m_right= rect.right();
m_bottom = MIN(rect.top(), rect.bottom());
m_top = MAX(rect.top(), rect.bottom());
m_valid = true;
}
MLRect::MLRect( const QPointF& point1, const QPointF& point2 )
{
memset(m_name, 0, 32);
m_left = MIN(point1.x(), point2.x());
m_right= MAX(point1.x(), point2.x());
m_bottom = MIN(point1.y(), point2.y());
m_top = MAX(point1.y(), point2.y());
m_valid = true;
}
void MLRect::setRect(double left, double top, double right, double bottom)
{
m_left = left;
m_top = top;
m_right = right;
m_bottom = bottom;
m_valid = true;
}
void MLRect::setRect(const MLPoint& dPt1, const MLPoint& dPt2)
{
m_left = MIN(dPt1.x, dPt2.x);
m_right= MAX(dPt1.x, dPt2.x);
m_bottom = MIN(dPt1.y, dPt2.y);
m_top = MAX(dPt1.y, dPt2.y);
}
MLRect::~MLRect(void)
{
}
const char* MLRect::getName() const
{
return m_name;
}
void MLRect::setName( char* name )
{
memcpy(m_name, name, 32);
}
void MLRect::setTop( double top )
{
m_top = top;
}
double MLRect::getTop() const
{
return m_top;
}
double& MLRect::getTop()
{
return m_top;
}
void MLRect::setLeft( double left )
{
m_left = left;
}
double MLRect::getLeft() const
{
return m_left;
}
void MLRect::setBottom( double bottom )
{
m_bottom = bottom;
}
double MLRect::getBottom() const
{
return m_bottom;
}
double& MLRect::getBottom()
{
return m_bottom;
}
void MLRect::setRight( double right )
{
m_right = right;
}
double MLRect::getRight() const
{
return m_right;
}
void MLRect::setValid(bool valid)
{
m_valid = valid;
}
bool MLRect::getValid() const
{
return m_valid;
}
QRectF MLRect::toQRectF() const
{
return QRectF(m_left, m_bottom, m_right-m_left, m_top - m_bottom);
}
QRect MLRect::toQRect() const
{
return QRect(m_left, m_bottom, m_right-m_left, m_top - m_bottom);
}
double MLRect::width()const
{
return fabs(m_right-m_left);
}
double MLRect::height()const
{
return fabs(m_top-m_bottom);
}
MLPoint MLRect::topLeft()
{
MLPoint point;
point.x = left;
point.y = top;
return point;
}
MLPoint MLRect::bottomRight()
{
MLPoint point;
point.x = right;
point.y = bottom;
return point;
}
MLPoint MLRect::rightTop()
{
MLPoint point;
point.x = right;
point.y = top;
return point;
}
MLPoint MLRect::leftBottom()
{
MLPoint point;
point.x = left;
point.y = bottom;
return point;
}
MLPoint MLRect::centerPoint()
{
MLPoint dPoint;
dPoint.x = left + (right - left)/2.;
dPoint.y = bottom +(top - bottom)/2.;
return dPoint;
}
/**
* == operator
*/
bool MLRect::operator ==(const MLRect& other) const
{
return m_left==other.m_left && m_right==other.m_right && m_top==other.m_top && m_bottom==other.m_bottom;
}
bool MLRect::operator !=(const MLRect& other) const
{
return !operator==(other);
}
void MLRect::setEmpty()
{
m_left = 0.;
m_right = 0.;
m_top = 0;
m_bottom = 0;
memset(m_name, 0, 32);
}
bool MLRect::isEmpty() const
{
if((m_right - m_left)< 1e-10 && (m_top-m_bottom)<1e-10)
return true;
return false;
}
void MLRect::normalizeRect()
{
if(m_left > m_right)
swap(m_left, m_right);
if(m_bottom > m_top)
swap(m_bottom, m_top);
}
MLPline MLRect::toPline()const
{
MLPline pline;
pline.addTailPt(m_left, m_bottom);
pline.addTailPt(m_left, m_top);
pline.addTailPt(m_right, m_top);
pline.addTailPt(m_right, m_bottom);
pline.addTailPt(m_left, m_bottom);
return pline;
}
bool MLRect::contains(const MLPoint& point)
{
if(point.x > left&& point.x <right && point.y > bottom && point.y <top)
return true;
return false;
}
bool MLRect::contains(double dbx,double dby)const
{
if(dbx > left&& dbx <right)
{
if(dby > bottom && dby <top)
return true;
}
return false;
}
bool MLRect::contains(const MLPline& pline)
{
int nCount = pline.getCount();
for(int i=0;i<nCount;i++)
{
MLPlNode dpt = pline.getAtPt(i);
if(!ptInRect(*this, dpt.x,dpt.y))
return false;
}
return true;
}
void MLRect::offset(double dx, double dy)
{
m_left += dx;
m_right += dx;
m_top += dy;
m_bottom += dy;
}
void MLRect::offset(const MLPoint& point)
{
m_left += point.x;
m_right += point.x;
m_top += point.y;
m_bottom += point.y;
}
void MLRect::moveTo(const MLPoint& point)
{
double w = width();
double h = height();
m_left = point.x;
m_right = m_left + w;
if (m_top < m_bottom)
{
m_top = point.y;
m_bottom = m_top + h;
}
else
{
m_bottom = point.y;
m_top = m_bottom + h;
}
}
void MLRect::moveTo(const QPointF& pointF)
{
double w = width();
double h = height();
m_left = pointF.x();
m_right = m_left + w;
if (m_top < m_bottom)
{
m_top = pointF.y();
m_bottom = m_top + h;
}
else
{
m_bottom = pointF.y();
m_top = m_bottom + h;
}
}
void MLRect::readRect(QDataStream& in)
{
in.readRawData(m_name, 32);
in >> m_top;
in >> m_left;
in >> m_bottom;
in >> m_right;
}
void MLRect::readRect(FILE* fr)
{
}
void MLRect::writeRect(QDataStream& out)
{
out.writeRawData(m_name, 32);
out << m_top;
out << m_left;
out << m_bottom;
out << m_right;
}
void MLRect::writeRect( FILE* fw )
{
}