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

📄 vector2d.cpp

📁 一个自己写的游戏引擎,用DirectX 写成
💻 CPP
字号:
//--------------------------------------------------
//  Desc: 2D Vector
//  Author: artsylee/2006.11.9
//--------------------------------------------------
#include "../stdafx.h"
#include "Vector2D.h"

ASINLINE float __stdcall InvSqrt(const float x)
{
	union
	{
		int		intPart;
        float	floatPart;
	}convertor;

	convertor.floatPart = x;
	convertor.intPart = 0x5f3759df - (convertor.intPart >> 1);
	return convertor.floatPart*(1.5f - 0.4999f*x*convertor.floatPart*convertor.floatPart);
}

/*
Vector2D *Vector2D::Normalize()
{
	float lenRcp;

	lenRcp = sqrtf(Dot(this));

	if(lenRcp)
	{
		lenRcp = 1.0f/lenRcp;

		x *= lenRcp;
		y *= lenRcp;
	}

	return this;
}
*/

ASINLINE float Vector2D::Dot(const Vector2D *v)
{ 
	return x * v->x + y * v->y;	
}

ASINLINE float Vector2D::Length(void)
{ 
	return sqrtf(Dot(this)); 
}

ASINLINE Vector2D* Vector2D::Normalize(void)
{ 
	float rc = InvSqrt(Dot(this)); 
	x *= rc; 
	y *= rc; 
	return this; 
}

ASINLINE float Vector2D::Angle(const Vector2D *v)
{
	if(v)
	{
		Vector2D s = *this;
		Vector2D t = *v;

		s.Normalize(); 
		t.Normalize();
		return acosf(s.Dot(&t));
	}
	else 
	{
		return atan2f(y, x);
	}
}

ASINLINE Vector2D *Vector2D::Rotate(float a)
{
	Vector2D v;

	v.x = x * cosf(a) - y * sinf(a);
	v.y = x * sinf(a) + y * cosf(a);

	x = v.x; 
	y = v.y;

	return this;
}




⌨️ 快捷键说明

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