Skip to content

Commit

Permalink
Added a bunch of commentary
Browse files Browse the repository at this point in the history
  • Loading branch information
TankofVines committed Sep 21, 2012
1 parent bae5f82 commit 8132f8b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion node_ppa.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var qs = require('querystring');
var sylvester = require('sylvester'); var sylvester = require('sylvester');
var async = require('async'); var async = require('async');


// Nearest neighbor distance calculator function
// Takes the coordinates array from the query and build a D matrix
function nndist(query, res, callback) { function nndist(query, res, callback) {
var coords = JSON.parse(query.coordinates); var coords = JSON.parse(query.coordinates);
var darray = []; var darray = [];
Expand Down Expand Up @@ -48,9 +50,16 @@ function nndist(query, res, callback) {
// } // }
// }); // });


// The row builder function creates a new row of values in the D matrix
// It is called recursively until the coordinates array is traversed
// Once all the coordinates have been iterated over a new array is used
// to
function rowBuilder(i) { function rowBuilder(i) {
if(i<coords.length) { if(i<coords.length) {
darray.push([]); darray.push([]);

// The distance calculation function calculates the distance between the current point/row (i)
// and every other point (j) then pushed that value into the current row and traverses the row recursively
function distCalc(j) { function distCalc(j) {
if(j<coords.length) { if(j<coords.length) {
// Distance formula // Distance formula
Expand All @@ -67,6 +76,8 @@ function nndist(query, res, callback) {
distCalc(0); distCalc(0);
} else { } else {
var nnarray = []; var nnarray = [];

// The row sorter function iterates of the D matrix rows and sorts them in ascending order
function rowSorter(i) { function rowSorter(i) {
if(i<darray.length) { if(i<darray.length) {
// Array.min = function( array ){ // Array.min = function( array ){
Expand All @@ -78,6 +89,8 @@ function nndist(query, res, callback) {
rowSorter(i+1); rowSorter(i+1);
} else { } else {
var nndistsum = 0; var nndistsum = 0;

// Iterates over the sorted rows and adds the value of shortest distance to the running total
function meanDist(j) { function meanDist(j) {
if(j<nnarray.length) { if(j<nnarray.length) {
nndistsum += nnarray[j]; nndistsum += nnarray[j];
Expand All @@ -94,12 +107,14 @@ function nndist(query, res, callback) {
} }
rowBuilder(0); rowBuilder(0);


// Respond with the stringified nearest neighbor value
var responseobjstring = JSON.stringify(nnvalue); var responseobjstring = JSON.stringify(nnvalue);
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(callback + '(' + responseobjstring + ')'); res.end(callback + '(' + responseobjstring + ')');

} }


// Create a server and parse the url, query string, and callback
// Switch to direct incoming request to functions
var server = http.createServer(function(req, res) { var server = http.createServer(function(req, res) {


var url = parse(req.url); var url = parse(req.url);
Expand Down

0 comments on commit 8132f8b

Please sign in to comment.