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

📄 point.h

📁 这是一款2d游戏引擎
💻 H
字号:
/*  $Id: point.h,v 1.23 2004/01/02 15:42:43 mbn Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
**
*/

//! clanCore="Math"
//! header=core.h

#ifndef header_point
#define header_point

#if _MSC_VER > 1000
#pragma once
#endif

#include "math.h"

//: 2D (x,y) point structure.
//- !group=Core/Math!
//- !header=core.h!
class CL_Point
{
// Construction:
public:
	//: Constructs a point.
	//param x: Initial x value.
	//param y: Initial y value.
	//param p: Point to use for initial values.
	CL_Point()
	{ return; }

	CL_Point(int x, int y)
	: x(x), y(y) { }

	CL_Point(const CL_Point &p)
	{ x = p.x; y = p.y; }

// Operations:
public:
	//: Return a rotated version of this point.
	//param hotspot: The point around which to rotate.
	//param angle: The amount of degrees to rotate by, clockwise.
	CL_Point rotate(
		const CL_Point &hotspot,
		float angle) const
	{
		//Move the hotspot to 0,0
		CL_Point r(x - hotspot.x, y - hotspot.y);
		
		//Do some Grumbel voodoo.

		// Because MSVC sucks ass wrt standards compliance, it gets it own special function calls
		#ifdef _MSC_VER
		const float c = (float) sqrt((float)r.x*(float)r.x + (float)r.y*(float)r.y);
		const float nw = (float)(atan2((float)r.y, (float)r.x) + ((angle + 180) * M_PI / 180));
		r.x = (int)((sin(1.5 * M_PI - nw) * c) + 0.5) + hotspot.x;
		r.y = -(int)((sin(nw) * c) + 0.5) + hotspot.y;
		#else
		const float c = (float) std::sqrt((float)r.x*(float)r.x + (float)r.y*(float)r.y);
		const float nw = (float)(std::atan2((float)r.y, (float)r.x) + ((angle + 180) * M_PI / 180));
 		r.x = (int)((std::sin(1.5 * M_PI - nw) * c) + 0.5) + hotspot.x;
		r.y = -(int)((std::sin(nw) * c) + 0.5) + hotspot.y;
		#endif

		return r;
	}
	
	//: Translate point.
	CL_Point &operator+=(const CL_Point &p)
	{ x += p.x; y += p.y; return *this; }

	//: Translate point negatively.
	CL_Point &operator-=(const CL_Point &p)
	{ x -= p.x; y -= p.y; return *this; }
	
	//: Point + Point operator.
	CL_Point operator+(const CL_Point &p) const
	{ return CL_Point(x + p.x, y + p.y); }

	//: Point - Point operator.
	CL_Point operator-(const CL_Point &p) const
	{ return CL_Point(x - p.x, y - p.y); }

	//: Point == Point operator (deep compare)
	bool operator==(const CL_Point &p) const
	{ return (x == p.x) && (y == p.y); }

// Attributes:
public:
	//: X coordinate.
	int x;

	//: Y coordinate.
	int y;
};

//: 2D (x,y) floating point point structure.
class CL_Pointf
{
// Construction:
public:
	//: Constructs a point.
	//param x: Initial x value.
	//param y: Initial y value.
	//param p: Point to use for initial values.
	CL_Pointf()
	{ return; }
	
	CL_Pointf(float x, float y)
	: x(x), y(y) { }
	
	CL_Pointf(const CL_Pointf &p)
	{ x = p.x; y = p.y; }
	
// Operations:
public:
	//: Return a rotated version of this point.
	//param hotspot: The point around which to rotate.
	//param angle: The amount of degrees to rotate by, clockwise.
	CL_Pointf rotate(
		const CL_Pointf &hotspot,
		float angle) const
	{
		//Move the hotspot to 0,0
		CL_Pointf r(x - hotspot.x, y - hotspot.y);
		
		//Do some Grumbel voodoo.
		// MSVC is doesn't recognize std::sin and friends
		#ifdef _MSC_VER
		const float c = (float)(sqrt(r.x*r.x + r.y*r.y));

		const float nw = (float)((float)atan2(r.y, r.x) + ((angle + 180) * M_PI / 180));

		r.x = ((float)(sin(1.5 * M_PI - nw)) * c) + hotspot.x;
		r.y = -((float)(sin(nw)) * c) + hotspot.y;
		#else
 		const float c = (float)(std::sqrt(r.x*r.x + r.y*r.y));

		const float nw = (float)((float)std::atan2(r.y, r.x) + ((angle + 180) * M_PI / 180));

		r.x = ((float)(std::sin(1.5 * M_PI - nw)) * c) + hotspot.x;
		r.y = -((float)(std::sin(nw)) * c) + hotspot.y;
		#endif
		
		return r;
	}
	
	//: Translate point.
	CL_Pointf &operator+=(const CL_Pointf &p)
	{ x += p.x; y += p.y; return *this; }
	
	//: Translate point negatively.
	CL_Pointf &operator-=(const CL_Pointf &p)
	{ x -= p.x; y -= p.y; return *this; }
	
	//: Point + Point operator.
	CL_Pointf operator+(const CL_Pointf &p) const
	{ return CL_Pointf(x + p.x, y + p.y); }

	//: Point - Point operator.
	CL_Pointf operator-(const CL_Pointf &p) const
	{ return CL_Pointf(x - p.x, y - p.y); }

	//: Point == Point operator (deep compare)
	bool operator==(const CL_Pointf &p) const
	{ return (x == p.x) && (y == p.y); }

// Attributes:
public:
	//: X coordinate.
	float x;
	
	//: Y coordinate.
	float y;
};

#endif

⌨️ 快捷键说明

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