Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@srifqi
Forked from 140bytes/LICENSE.txt
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srifqi/014be78e687b36373dc7 to your computer and use it in GitHub Desktop.
Save srifqi/014be78e687b36373dc7 to your computer and use it in GitHub Desktop.
Applying 4x4 Matrix to Vector

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

Applying 4x4 Matrix to Vector

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]
}
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}
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.
{
"name": "applyMatrix4ToVector",
"description": "Applying 4x4 Matrix to Vector.",
"keywords": [
"apply",
"square",
"matrix",
"to",
"vector"
]
}
<!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>
@atk
Copy link

atk commented Aug 25, 2014

[] coerces to 0 as number; array indices are coerced to integer values (save 3 bytes):

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}

@srifqi
Copy link
Author

srifqi commented Aug 27, 2014

Thanks, updated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment