Skip to content

Instantly share code, notes, and snippets.

@hadaytullah
Last active October 25, 2019 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadaytullah/63eeab185144dfe140db762e14ba04d5 to your computer and use it in GitHub Desktop.
Save hadaytullah/63eeab185144dfe140db762e14ba04d5 to your computer and use it in GitHub Desktop.
Find nearest linkstation for a device
/**
* @author Hadaytullah Kundi
*/
/**
* Link Station as ECMAScript 2015 class.
*/
class LinkStation {
/*
* LinkStation Constuctor
* @param {number} locationX Link station location x coordinate.
* @param {number} locationY Link station location y coordinate.
* @param {number} reach Link station reach (in a straight line distance over a 2d map of the space).
*/
constructor(locationX, locationY, reach){
this.location = {
x:locationX,
y:locationY
};
this.reach = reach;
}
}
/**
* Device creating constructor function (prototype style)
* @param {number} locationX Device's location x coordinate.
* @param {number} locationY Device's location y coordinate.
* @return {Object} Device object.
*/
let Device = function (locationX, locationY){
this.location = {
x:locationX,
y:locationY
};
}
/**
* Station locating logic is encapsulated in this class
* EMCA2015
*/
class StationLocator {
/**
* Finds a nearby link station for a device .
* @param {LinkStation[]} linkStationLocations An array of link station location and reach
* @param {Device} deviceLocation The device current location.
* @return {(Object|null)} An object containing nearby link statation and power or null.
*/
findNearbyStation (linkStations, device){
try{
let selectedLinkStation = null,
selectedLinkStationPower = 0;
for (let linkStationIndex in linkStations){
let currentLinkStation = linkStations[linkStationIndex];
// the distance formula, pythagoras theorum
let deviceDistance = Math.sqrt( Math.pow((currentLinkStation.location.x - device.location.x), 2) + Math.pow((currentLinkStation.location.y-device.location.y), 2) );
// calculating power based on the distance
let currentLinkStationPower = 0;
if (deviceDistance <= currentLinkStation.reach){
//power = (reach - device's distance from linkstation)^2
currentLinkStationPower = Math.pow((currentLinkStation.reach - deviceDistance), 2)
}
// check if this link station has more power based on its distance from the device
if(selectedLinkStationPower < currentLinkStationPower){
selectedLinkStation = currentLinkStation
selectedLinkStationPower = currentLinkStationPower
}
}
//power is related to device, it is not the core property of link station, it has to be passed back separately
if (selectedLinkStation){ //a nearby link station found
return {
linkStation: selectedLinkStation,
power:selectedLinkStationPower
};
}else{ //a nearby link station not found
return null;
}
}
catch(e){
console.error(e);//TODO:log the error using a logger
return null;
}
}
}
//testing
let test1 = function (){
// Initializing link stations
let linkStations =[new LinkStation(0, 0, 10),
new LinkStation(20, 20, 5),
new LinkStation(10, 0, 12)];
// Initializing test devices
let devices = [new Device(0,0),
new Device(100,100),
new Device(15,10),
new Device(18,18)]
let stationLocator = new StationLocator();
// locating nearby link station for the devices
devices.forEach( function (device){
let linkStationProfile = stationLocator.findNearbyStation(linkStations, device);
let output = null;
if (linkStationProfile){
output = 'Best link station for point '+device.location.x+','+device.location.y+' is '+linkStationProfile.linkStation.location.x+','+linkStationProfile.linkStation.location.y+' with power '+Math.round(linkStationProfile.power*1000)/1000+'.';
}else{
output = 'No link station within reach for point '+device.location.x+','+device.location.y+'.';
}
document.getElementById("output").appendChild(document.createTextNode(output));
})
}
test1();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment