码迷,mamicode.com
首页 > 其他好文 > 详细

Lua实现angle角

时间:2016-07-19 10:17:35      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

function i3k_vec3(x, y, z)
	return { x = x, y = y, z = z };
end
function i3k_vec3_normalize1(v)
	return i3k_vec3_normalize2(v.x, v.y, v.z);
end

function i3k_vec3_normalize2(x, y, z)
	local _x = x;
	local _y = y;
	local _z = z;

	local d = math.sqrt(_x * _x + _y * _y + _z * _z);
	if d ~= 0 then
		_x = _x / d;
		_y = _y / d;
		_z = _z / d;
	end

	return i3k_vec3(_x, _y, _z);
end
-- only x, z
function i3k_vec3_angle1(p1, p2, p3)
	return i3k_vec3_angle2(i3k_vec3(p1.x - p2.x, 0, p1.z - p2.z), p3);
end


function i3k_vec3_angle2(p1, p2)
	local _v1 = i3k_vec3_normalize1(p1);
	local _v2 = i3k_vec3_normalize1(p2);

	local epsilon = 0.000001;

	local dot = _v1.x * _v2.x + _v1.z * _v2.z;

	local angle = 0;

	if math.abs(dot - 1) <= epsilon then
		angle = 0;
	elseif math.abs(dot + 1) <= epsilon then
		angle = math.pi;
	else
		angle = math.acos(dot);

		local cross = _v1.x * _v2.z - _v2.x * _v1.z;
		if cross < 0 then
			angle = 2 * math.pi - angle;
		end
	end

	return angle;
end


Lua实现angle角

标签:

原文地址:http://blog.csdn.net/heyuchang666/article/details/51944424

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!