備忘録

プログラムやゲーム関連に関すること

【C++】三角関数

画角からの焦点距離

http://4.bp.blogspot.com/-piK7w-EcpCs/UDTa4ukSeGI/AAAAAAAAAFc/Qh7r-7rfFF0/s320/Lens.jpg

#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	auto toRadian = [](float deg){ return deg * static_cast<float>(M_PI) / 180.f; };

	auto width = 800.f;
	auto height = 600.f;
	auto aspect = width / height;
	auto fov = toRadian(45.f);
	auto distance = width / aspect / 2.f / tan(fov / 2.f);

	cout << "幅:" << width << endl;
	cout << "高さ:" << height << endl;
	cout << "アスペクト比:" << aspect << endl;
	cout << "画角:" << fov << endl;
	cout << "焦点距離:" << distance << endl;

	return 0;
}

参考・引用:3D Animation & Programming: 画角と焦点距離のお話