{"size": 3979, "ext": "js", "max_stars_repo_path": "05-06-Web_Technologies_HTML5/sourceCode/chap11/pointTracker.js", "max_stars_repo_name": "MohamedFathiM/OSTrack2019", "max_stars_repo_stars_event_min_datetime": "2019-08-04T22:30:35.000Z", "max_stars_repo_stars_event_max_datetime": "2020-02-24T11:18:22.000Z", "max_issues_repo_path": "05-06-Web_Technologies_HTML5/sourceCode/chap11/pointTracker.js", "max_issues_repo_name": "MohamedFathiM/OSTrack2019", "max_issues_repo_issues_event_min_datetime": "2019-08-11T21:51:32.000Z", "max_issues_repo_issues_event_max_datetime": "2019-08-21T11:12:22.000Z", "max_forks_repo_path": "05-06-Web_Technologies_HTML5/sourceCode/chap11/pointTracker.js", "max_forks_repo_name": "MohamedFathiM/OSTrack2019", "max_forks_repo_forks_event_min_datetime": "2019-08-05T21:11:03.000Z", "max_forks_repo_forks_event_max_datetime": "2019-09-29T16:05:52.000Z"}
{"max_stars_count": 5, "max_issues_count": 2, "max_forks_count": 14, "avg_line_length": 29.0437956204, "max_line_length": 76, "alphanum_fraction": 0.5355616989}
/*******************************************************
* pointTracker.js
* John Dean
*
* This file implements a Point class.
* It stores Point objects for the locations of the two
* most recent mouse clicks.
*******************************************************/

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    if (Point.count == undefined) {
      Point.count = 1;
    }
    else if (Point.count == 1) {
      Point.count = 2;
    }
  } // end constructor

  //***************************************************

  // Return the point in the format "(x, y)"

  value() {
    return "(" + this.x + ", " + this.y + ")";
  } // end value

  //***************************************************

  // Return a count for the number of Point objects

  static getCount() {
    return (Point.count == undefined) ? 0 : Point.count;
  } // end getCount

  //***************************************************

  // Return the distance between the two points or null if one
  // or more of the points is missing.

  static distance(pt1, pt2) {
    var xDist, yDist; // distances apart in the 2 dimensions
    var distance;     // distance apart with direct connection

    if (Point.count == 2) {
      xDist = pt1.x - pt2.x;
      yDist = pt1.y - pt2.y;
      distance = Math.sqrt(xDist * xDist + yDist * yDist);
    }
    else {
      distance = null;
    }

    return distance;
  } // end distance

  //***************************************************

  // After removing a point, call this method to reduce the count.

  static reduceCount() {
    if (Point.count == 1) {
      Point.count = undefined;
    }
    else if (Point.count == 2) {
      Point.count = 1;
    }
  } // end reduceCount
} // end class Point

//***************************************************

var point1, point2;  // the most recently clicked points

// To implement delete key functionality, remove the html element's
// onclick event handler and use the following instead.
// document.addEventListener("click", captureClick);
// document.addEventListener("keydown", removePoint);

// This function stores a user's click location as a point.

function captureClick(e) {
  if (Point.getCount() == 0) {
    point1 = new Point(e.clientX, e.clientY);
    document.getElementById("pt1").innerHTML = point1.value();
  }
  else if (Point.getCount() == 1) {
    point2 = new Point(e.clientX, e.clientY);
    document.getElementById("pt2").innerHTML = point2.value();
  }
  else {
    point1 = point2;
    point2 = new Point(e.clientX, e.clientY);
    document.getElementById("pt1").innerHTML = point1.value();
    document.getElementById("pt2").innerHTML = point2.value();
  }
} // end captureClick

//***************************************************

// This function calculates and returns the distance between 2 points.

function displayDistance(e) {
  var distance;     // distance apart with direct connection
  var message;      // message with distance or a warning

  e.stopPropagation(); // prevent the button from creating a new point
  distance = Point.distance(point1, point2);
  if (distance == null) {
    message = "To calculate a distance, you must first create two points!";
  }
  else {
    message =
      "The two points are " + distance.toFixed(1) + " pixels apart.";
  }

  document.getElementById("message").innerHTML = message;
} // end displayDistance

//***************************************************

// This function removes the most recently created point

function removePoint(e) {
  if (e.key == "Delete") {
    if (Point.getCount() == 1) {
      document.getElementById("pt1").innerHTML = "";
      Point.reduceCount();
    }
    else if (Point.getCount() == 2) {
      document.getElementById("pt2").innerHTML = "";
      Point.reduceCount();
    }
  } // end if key == "Delete"
} // end removePoint