Lua實現angle角__angle角

來源:互聯網
上載者:User

function i3k_vec3(x, y, z)return { x = x, y = y, z = z };endfunction i3k_vec3_normalize1(v)return i3k_vec3_normalize2(v.x, v.y, v.z);endfunction 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;endreturn i3k_vec3(_x, _y, _z);end-- only x, zfunction i3k_vec3_angle1(p1, p2, p3)return i3k_vec3_angle2(i3k_vec3(p1.x - p2.x, 0, p1.z - p2.z), p3);endfunction 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 thenangle = 0;elseif math.abs(dot + 1) <= epsilon thenangle = math.pi;elseangle = math.acos(dot);local cross = _v1.x * _v2.z - _v2.x * _v1.z;if cross < 0 thenangle = 2 * math.pi - angle;endendreturn angle;end


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.