clsRect.cpp 1.9 KB

#include "clsRect.h"
#include <math.h>

using namespace std;
namespace DmapCore_30
{
	Rect::Rect()
	{
		m_dTop = 0;
		m_dRight = 0;
		m_dBottom = 0;
		m_dLeft = 0;
	}
	Rect::Rect(double top, double right, double bottom, double left)
	{
		m_dTop = top;
		m_dRight = right;
		m_dBottom = bottom;
		m_dLeft = left;
	}

	Rect::~Rect()
	{
	}
	


	Rect * Rect::CreateNew()
	{
		Rect * dest = new Rect();
		return dest;
	}

	Rect * Rect::CreateNew(double top, double right, double bottom, double left)
	{
		Rect* dest = new Rect(top, right, bottom, left);
		return dest;
	}

	bool Rect::Normal()
	{
		double temp;
		if (m_dLeft > m_dRight)
		{
			temp = m_dLeft;
			m_dLeft = m_dRight;
			m_dRight = temp;
		}
		if (m_dBottom > m_dTop)
		{
			temp = m_dTop;
			m_dTop = m_dBottom;
			m_dBottom = temp;
		}
		return true;
	}
	bool Rect::Scale(double num)
	{
		//中心点为标准?
		this->Normal();

		double w = m_dRight - m_dLeft;
		double h = m_dTop - m_dBottom;

		double x = (m_dRight + m_dLeft) / 2;
		double y = (m_dTop + m_dBottom) / 2;

		double w_ = w * num;
		double h_ = h * num;

		m_dTop = y + h_ / 2;
		m_dRight = x + w_ / 2;
		m_dBottom = y - h_ / 2;
		m_dLeft = x - w_ / 2;

		return true;
	}
	double Rect::GetArea()
	{
		this->Normal();
		double area = (m_dRight - m_dLeft)* (m_dTop - m_dBottom);
		return area;
	}

	bool Rect::GetExtent(double &boxX1, double& boxX2, double &boxY1, double &boxY2)
	{
		if (std::fmin(boxX1, boxX2) > this->m_dRight || std::fmax(boxX1, boxX2) < this->m_dLeft ||
				std::fmin(boxY1, boxY2) > this->m_dTop || std::fmax(boxY1, boxY2) < this->m_dBottom)
			{
				if (!(std::fmin(boxY1, boxY2) > this->m_dRight || std::fmax(boxY1, boxY2) < this->m_dLeft ||
					  std::fmin(boxX1, boxX2) > this->m_dTop || std::fmax(boxX1, boxX2) < this->m_dBottom))
				{
					double x1 = boxX1;
					boxX1 = boxY1;
					boxY1 = x1;
					double x2 = boxX2;
					boxX2 = boxY2;
					boxY2 = x2;
					return true;
				}
			}

		return false;
	}

}