Skip to content

Instantly share code, notes, and snippets.

@arcollector
Last active August 29, 2015 14:22
Show Gist options
  • Save arcollector/8131e3cdc06e9b796049 to your computer and use it in GitHub Desktop.
Save arcollector/8131e3cdc06e9b796049 to your computer and use it in GitHub Desktop.
Anti aliasing by filtering using a weighted averaging scheme
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