A tweet-sized, fork-to-play, community-curated collection of JavaScript.
-
-
Save srifqi/014be78e687b36373dc7 to your computer and use it in GitHub Desktop.
Applying 4x4 Matrix to Vector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function( | |
a, //4x4 matrix [array] | |
//[a, b, c, d, | |
// e, f, g, h, | |
// i, j, k, l, | |
// m, n, o, p] | |
b,c,d //vector (x, y, z) | |
){ | |
for( //loop through matrix | |
var f=e=[];f<16;) // [] coerces to 0 as number | |
e[f/4] = //add to list | array indices are coerced to integer values | |
a[f++]*b+ //calculate | |
a[f++]*c+ //calculate | |
a[f++]*d+ //calculate | |
a[f++] //calculate | |
; | |
return e //returned result: [x,y,z,1] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(a,b,c,d){for(var f=e=[];f<16;)e[f/4]=a[f++]*b+a[f++]*c+a[f++]*d+a[f++];return e} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 srifqi srifqi.github.io | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "applyMatrix4ToVector", | |
"description": "Applying 4x4 Matrix to Vector.", | |
"keywords": [ | |
"apply", | |
"square", | |
"matrix", | |
"to", | |
"vector" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>0, 0, 0, 1</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
// write a small example that shows off the API for your example | |
// and tests it in one fell swoop. | |
var myFunction = function(a,b,c,d){for(var f=e=[];f<16;)e[f/4]=a[f++]*b+a[f++]*c+a[f++]*d+a[f++];return e} | |
document.getElementById( "ret" ).innerHTML = myFunction([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],0,0,0) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[] coerces to 0 as number; array indices are coerced to integer values (save 3 bytes):