Last active
August 29, 2015 14:22
-
-
Save arcollector/8131e3cdc06e9b796049 to your computer and use it in GitHub Desktop.
Anti aliasing by filtering using a weighted averaging scheme
This file contains 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
var table = [ | |
[1,2,1], | |
[2,4,2], | |
[1,2,1], | |
]; | |
var tableMaxDim = 3; | |
var tableSum = 16; | |
var filterStart = -1; | |
var filterEnd = 1; | |
function filter( i, j ) { | |
// correct index position | |
i++; | |
j++; | |
if( i >= tableMaxDim || j >= tableMaxDim || i < 0 || j < 0 ) { | |
return 0; | |
} | |
return table[j][i]/tableSum; | |
}; | |
// 7 x 7 pixel grid | |
var screenMaxY = 7; | |
var screenMaxX = 7; | |
// image consists of the infinitesimally thin line y = x in white on a black background | |
function image( x, y ) { | |
return x === y ? 1 : 0; | |
}; | |
var videoMemory = []; | |
for( var y = 0; y < screenMaxY; y++ ) { | |
for( var x = 0; x < screenMaxX; x++ ) { | |
// sum of j = -1 to 1 | |
// sum of i = -1 to 1 | |
// h(i,j)f(x+i,y+j) | |
// where h(i,j) is the filter function | |
// where f(x+i,y+j) represents the scene intensity at point (x,y) | |
var inten = 0; | |
for( var j = filterStart; j <= filterEnd; j++ ) { | |
for( var i = filterStart; i <= filterEnd; i++ ) { | |
inten += filter( i, j ) * image( x + i, y + j ); | |
} | |
} | |
videoMemory.push( inten ); | |
} | |
} | |
// ouput video screen buffer | |
// invert y axis | |
for( var i = 0, y = screenMaxY - 1; y >= 0; y-- ) { | |
var row = []; | |
for( var x = 0; x < screenMaxX; x++ ) { | |
row.push( videoMemory[y*screenMaxX+x]*tableSum ); | |
} | |
console.log( row ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment