Permalink
Browse files

Added a bunch of commentary

  • Loading branch information...
1 parent bae5f82 commit 8132f8b33734caea57e23f7bd407f8109adc618e @TankofVines committed Sep 21, 2012
Showing with 16 additions and 1 deletion.
  1. +16 −1 node_ppa.js
View
@@ -4,6 +4,8 @@ var qs = require('querystring');
var sylvester = require('sylvester');
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) {
var coords = JSON.parse(query.coordinates);
var darray = [];
@@ -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) {
if(i<coords.length) {
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) {
if(j<coords.length) {
// Distance formula
@@ -67,6 +76,8 @@ function nndist(query, res, callback) {
distCalc(0);
} else {
var nnarray = [];
+
+ // The row sorter function iterates of the D matrix rows and sorts them in ascending order
function rowSorter(i) {
if(i<darray.length) {
// Array.min = function( array ){
@@ -78,6 +89,8 @@ function nndist(query, res, callback) {
rowSorter(i+1);
} else {
var nndistsum = 0;
+
+ // Iterates over the sorted rows and adds the value of shortest distance to the running total
function meanDist(j) {
if(j<nnarray.length) {
nndistsum += nnarray[j];
@@ -94,12 +107,14 @@ function nndist(query, res, callback) {
}
rowBuilder(0);
+ // Respond with the stringified nearest neighbor value
var responseobjstring = JSON.stringify(nnvalue);
res.setHeader('Content-Type', 'application/json');
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 url = parse(req.url);

0 comments on commit 8132f8b

Please sign in to comment.