Running starter2_closestp.js

➕︎ Aa ➖︎ |Dark 1Dark 2Light|HackFira Code|Back to  PT Mono
// CPCS 223 Analysis & Design of Algorithms
// Brute force closest pair - light use of object-oriented features
// 2020, Dr. Muhammad Al-Hashimi


// -----------------------------------------------------------------------
// better implementation based on an array of point objecs,
// hopefully code simpler to read


// like [] for constant (literal) array, {} is used for objects
var p = [ ,      // ignore p[0]
   {x:1, y:-1},  // a point object has 2 fields: x, y
   {x:3, y:1},
   {x:2, y:0},
   {x:4, y:2}, 
   {x:5, y:3}, 
   {x:6, y:4} 
   ];

// reference property field using usual . notation, example p[i].x
document.write("<p>x-coord of 2nd point: ", p[2].x, "</p>");
document.write("<p>y-coord of 1st point: ", p[1].y, "</p>");
document.write( "<p>",closestPair2(),"</p>" );

// --------------------
function closestPair2()
{
   var n = p.length-1;     // p[] global, ignore index 0 
   var cp = [-1,-1];       // init for testing/err condition
   
   // your code here
   
      
   return cp;
}