⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gspoint.cpp

📁 网络泡泡被.net管理
💻 CPP
字号:
// GsPoint.cpp: implementation of the GsPoint class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GSLib_Internal.h"
#include <cmath>

double const k_pi = 3.1415926535897932384626433832795;


// ---------------------------------------------------------------
// inline SScreenPoint members
// ---------------------------------------------------------------
inline SScreenPoint::SScreenPoint()// : x( 0 ), y( 0 )
{
	x = 0;
	y = 0;
}

inline SScreenPoint::SScreenPoint( int new_x, int new_y )
{
	x = new_x;
	y = new_y;
}

inline SScreenPoint::SScreenPoint( TPoint2d< int > const & other )
{
	x = other.x;
	y = other.y;
}

inline SScreenPoint::SScreenPoint( SScreenPoint const & other )
{
	x = other.x;
	y = other.y;
}

inline SScreenPoint & SScreenPoint::operator+=( SScreenPoint const & right )
{
	x += right.x;
	y += right.y;
	return *this;
}

inline SScreenPoint & SScreenPoint::operator-=( SScreenPoint const & right )
{
	x -= right.x;
	y -= right.y;
	return *this;
}

inline SScreenPoint & SScreenPoint::operator*=( int right )
{
	x *= right;
	y *= right;
	return *this;
}

inline SScreenPoint & SScreenPoint::operator/=( int right )
{
	x /= right;
	y /= right;
	return *this;
}

inline SScreenPoint & SScreenPoint::operator%=( int right )
{
	x %= right;
	y %= right;
	return *this;
}

inline SScreenPoint & SScreenPoint::operator<<=( int right )
{
	x <<= right;
	y <<= right;
	return *this;
}

inline SScreenPoint & SScreenPoint::operator>>=( int right )
{
	x >>= right;
	y >>= right;
	return *this;
}

inline SScreenPoint::operator TPoint2d< int >() const
{
	return TPoint2d< int >( x, y );
}

// ---------------------------------------------------------------
// inline SScreenPoint free functions
// ---------------------------------------------------------------

inline bool operator==( SScreenPoint const & left, SScreenPoint const & right )
{
	return left.x == right.x && left.y == right.y;
}

inline bool operator!=( SScreenPoint const & left, SScreenPoint const & right )
{
	return !( left == right );
}

inline SScreenPoint operator-( SScreenPoint const & point )
{
	return SScreenPoint( -point.x, -point.y );
}

inline SScreenPoint operator+( SScreenPoint const & left, SScreenPoint const & right )
{
	SScreenPoint result( left );
	result += right;
	return result;
}

inline SScreenPoint operator-( SScreenPoint const & left, SScreenPoint const & right )
{
	SScreenPoint result( left );
	result -= right;
	return result;
}

inline SScreenPoint operator*( SScreenPoint const & left, int right )
{
	SScreenPoint result( left );
	result *= right;
	return result;
}

inline SScreenPoint operator*( int left, SScreenPoint const & right )
{
	return right * left;
}

inline SScreenPoint operator/( SScreenPoint const & left, int right )
{
	SScreenPoint result( left );
	result /= right;
	return result;
}

inline SScreenPoint operator%( SScreenPoint const & left, int right )
{
	SScreenPoint result( left );
	result %= right;
	return result;
}

inline SScreenPoint operator<<( SScreenPoint const & left, int right )
{
	SScreenPoint result( left );
	result <<= right;
	return result;
}

inline SScreenPoint operator>>( SScreenPoint const & left, int right )
{
	SScreenPoint result( left );
	result >>= right;
	return result;
}

inline double distance( SScreenPoint const & point )
{
	return sqrt( point.x * point.x + point.y * point.y );
}

inline double angle( SScreenPoint const & point )
{
	double result = atan2( point.y, point.x );
	if ( result < 0.0 )
		result += k_pi * 2;
	return result;
}

inline std::streambuf & operator<<( std::streambuf & stream, SScreenPoint const & point )
{
	put< LONG >( stream, point.x );
	put< LONG >( stream, point.y );
	return stream;
}

inline std::streambuf & operator>>( std::streambuf & stream, SScreenPoint & point )
{
	SScreenPoint result;
	result.x = get< LONG >( stream );
	result.y = get< LONG >( stream );
	point = result;
	return stream;
}


// ---------------------------------------------------------------
// inline SScreenRect members
// ---------------------------------------------------------------

inline SScreenRect::SScreenRect() 
{
	left	= 0;
	top		= 0;
	right	= 0;
	bottom	= 0;
}

inline SScreenRect::SScreenRect( int new_left, int new_top, int new_right, int new_bottom )
{
	left	= new_left;
	top		= new_top;
	right	= new_right;
	bottom	= new_bottom;
}

inline SScreenRect::SScreenRect( SScreenPoint const & top_left, SScreenPoint const & size )
{
	left	= top_left.x;
	top		= top_left.y;
	right	= top_left.x + size.x;
	bottom	= top_left.y + size.y;
}

inline SScreenRect::SScreenRect( TRect2d< int > const & other )
{
	left	= other.left;
	top		= other.top;
	right	= other.right;
	bottom	= other.bottom;
}

inline SScreenRect::SScreenRect( SScreenRect const & other )
{
	left	= other.left;
	top		= other.top;
	right	= other.right;
	bottom	= other.bottom;
}


inline int SScreenRect::height() const
{
	return bottom - top;
}

inline int SScreenRect::width() const
{
	return right - left;
}

inline SScreenPoint SScreenRect::top_left() const
{
	return SScreenPoint( left, top );
}

inline SScreenPoint SScreenRect::top_right() const
{
	return SScreenPoint( right, top );
}

inline SScreenPoint SScreenRect::bottom_left() const
{
	return SScreenPoint( left, bottom );
}

inline SScreenPoint SScreenRect::bottom_right() const
{
	return SScreenPoint( right, bottom );
}

inline SScreenPoint SScreenRect::size() const
{
	return SScreenPoint( width(), height() );
}

inline bool SScreenRect::contains( SScreenRect const& arg ) const
{
	return left <= arg.left && right >= arg.right && top <= arg.top && bottom >= arg.bottom;
}

inline SScreenRect & SScreenRect::operator+=( SScreenPoint const & point )
{
	left += point.x;
	right += point.x;
	top += point.y;
	bottom += point.y;
	return *this;
}

inline SScreenRect & SScreenRect::operator-=( SScreenPoint const & point )
{
	left -= point.x;
	right -= point.x;
	top -= point.y;
	bottom -= point.y;
	return *this;
}

inline SScreenRect & SScreenRect::operator*=( int arg )
{
	left *= arg;
	right *= arg;
	top *= arg;
	bottom *= arg;
	return *this;
}

inline SScreenRect & SScreenRect::operator/=( int arg )
{
	left /= arg;
	right /= arg;
	top /= arg;
	bottom /= arg;
	return *this;
}

inline SScreenRect & SScreenRect::operator<<=( int arg )
{
	left <<= arg;
	right <<= arg;
	top <<= arg;
	bottom <<= arg;
	return *this;
}

inline SScreenRect & SScreenRect::operator>>=( int arg )
{
	left >>= arg;
	right >>= arg;
	top >>= arg;
	bottom >>= arg;
	return *this;
}

inline void	SScreenRect::make_extern( SScreenRect const & rect )
{
	if(left>rect.left)
		left = rect.left;
	if(top>rect.top)
		top	= rect.top;
	if(right<rect.right)
		right = rect.right;
	if(bottom<rect.bottom)
		bottom = rect.bottom;
}
inline void SScreenRect::make_clipper( SScreenRect const & rect )
{
	if(left<rect.left)
		left = rect.left;
	if(top<rect.top)
		top	= rect.top;
	if(right>rect.right)
		right = rect.right;
	if(bottom>rect.bottom)
		bottom = rect.bottom;
}
/*
inline void SScreenRect::make_fit( SScreenRect const & rect )
{
	
}
*/
inline void SScreenRect::expand(GPOINT pt)
{
	left -= pt.x;
	right += pt.x;
	top -= pt.y;
	bottom += pt.y;

}

inline void SScreenRect::expand(LONG size)
{
	left -= size;
	right += size;
	top -= size;
	bottom += size;
}



inline void SScreenRect::make_room( SScreenRect const & rect )
{
	int w1	= width(), w2 = rect.width(), h1 = height(), h2 = rect.height();
	if( w1<=0 || w2<=0 || h1<=0 || h2<=0 )
	{
		left = 0;
		right = 0;
		top	= 0;
		bottom = 0;
	}
	else
	{
		float a = (float)w1/(float)h1;
		float b = (float)w2/(float)h2;
		if(a==b)
			return;
		else if(a>b)
		{
			int c = (w1 - h1*b)/2;
			left	*= c;
			right	-= c;
		}
		else
		{
			int c = (h1 - w1/b)/2;
			top 	+= c;
			bottom	-= c;
		}
	}
}
inline SScreenRect::operator TRect2d< int >() const
{
	return TRect2d< int >( left, top, right, bottom );
}



// ---------------------------------------------------------------
// inline SScreenRect free functions
// ---------------------------------------------------------------

inline bool operator==( SScreenRect const & left, SScreenRect const & right )
{
	return left.left == right.left && left.top == right.top && left.right == right.right && left.bottom == right.bottom;
}

inline bool operator!=( SScreenRect const & left, SScreenRect const & right )
{
	return !( left == right );
}

inline SScreenRect operator+( SScreenRect const & left, SScreenPoint const & right )
{
	SScreenRect result( left );
	result += right;
	return result;
}

inline SScreenRect operator+( SScreenPoint const & left, SScreenRect const & right )
{
	return right + left;
}

inline SScreenRect operator-( SScreenRect const & left, SScreenPoint const & right )
{
	SScreenRect result( left );
	result -= right;
	return result;
}

inline SScreenRect operator*( SScreenRect const & left, int right )
{
	SScreenRect result( left );
	result *= right;
	return result;
}

inline SScreenRect operator*( int left, SScreenRect const & right )
{
	return right * left;
}

inline SScreenRect operator/( SScreenRect const & left, int right )
{
	SScreenRect result( left );
	result /= right;
	return result;
}

inline SScreenRect operator<<( SScreenRect const & left, int right )
{
	SScreenRect result( left );
	result <<= right;
	return result;
}

inline SScreenRect operator>>( SScreenRect const & left, int right )
{
	SScreenRect result( left );
	result >>= right;
	return result;
}

inline bool is_point_in_rect( SScreenPoint const & point, SScreenRect const & rect )
{
	return point.x >= rect.left && point.x < rect.right && point.y >= rect.top && point.y < rect.bottom;
}

inline bool is_normalized( SScreenRect const & rect )
{
	return rect.left <= rect.right && rect.top <= rect.bottom;
}

inline void normalize( SScreenRect & rect )
{
	if ( rect.left > rect.right )
		std::swap( rect.left, rect.right );

	if ( rect.top > rect.bottom )
		std::swap( rect.top, rect.bottom );
}

inline bool intersect( SScreenRect const & left, SScreenRect const & right )
{
	return		left.left < right.right
			&&	right.left < left.right
			&&	left.top < right.bottom
			&&	right.top < left.bottom;
}

inline SScreenRect intersection( SScreenRect const & left, SScreenRect const & right )
{
	return	SScreenRect(
				left.left > right.left ? left.left : right.left,
				left.top > right.top ? left.top : right.top,
				left.right < right.right ? left.right : right.right,
				left.bottom < right.bottom ? left.bottom : right.bottom );
}

// return the rectangle enclosing both rectangles.
inline SScreenRect get_extent( SScreenRect const& left, SScreenRect const& right )
{
	return SScreenRect( left.left   < right.left   ? left.left : right.left,
		                  left.top    < right.top    ? left.top  : right.top,
						  left.right  > right.right  ? left.right : right.right,
						  left.bottom > right.bottom ? left.bottom : right.bottom );
}

inline std::streambuf & operator<<( std::streambuf & stream, SScreenRect const & rect )
{
	put< LONG >( stream, rect.left );
	put< LONG >( stream, rect.top );
	put< LONG >( stream, rect.right );
	put< LONG >( stream, rect.bottom );
	return stream;
}

inline std::streambuf & operator>>( std::streambuf & stream, SScreenRect & rect )
{
	SScreenRect result;
	result.left = get< LONG >( stream );
	result.top = get< LONG >( stream );
	result.right = get< LONG >( stream );
	result.bottom = get< LONG >( stream );
	rect = result;
	return stream;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -