-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Reaktor Orbital Challenge | ||
// (c) Jonne Pihlanen 2016 | ||
|
||
const | ||
fsp = require('fs-promise'), | ||
csv = require('csv'); | ||
|
||
fsp.readFile('./data2.csv') | ||
.then((contents) => { | ||
csv.parse(contents, (err, data) => { | ||
|
||
let seed = data.shift()[0]; | ||
console.log(seed); | ||
let route = data.pop(); | ||
let start = { | ||
latitude: parseFloat(route[1]), | ||
longitude: parseFloat(route[2]), | ||
altitude: 0 | ||
}; | ||
|
||
let end = { | ||
latitude: parseFloat(route[3]), | ||
longitude: parseFloat(route[4]), | ||
altitude: 0 | ||
}; | ||
|
||
data = data.map((item) => { | ||
return { | ||
id: item[0], | ||
latitude: parseFloat(item[1]), | ||
longitude: parseFloat(item[2]), | ||
altitude: parseFloat(item[3]) | ||
}; | ||
}); | ||
|
||
let horizonA = distanceToHorizon(start); | ||
let startPointSatellites = []; | ||
data.forEach((item) => { | ||
let distance = calculateDistance(calculateCoordinate(start), calculateCoordinate(item)); | ||
let horizonB = distanceToHorizon(item); | ||
if (distance <= horizonA + horizonB) { | ||
// console.log("start position can see satellite " + item.id); | ||
startPointSatellites.push(item); | ||
} | ||
}); | ||
|
||
horizonA = distanceToHorizon(end); | ||
let endPointSatellites = []; | ||
data.forEach((item) => { | ||
distance = calculateDistance(calculateCoordinate(end), calculateCoordinate(item)); | ||
horizonB = distanceToHorizon(item); | ||
if (distance <= horizonA + horizonB) { | ||
// console.log("end position can see satellite " + item.id); | ||
endPointSatellites.push(item); | ||
} | ||
}); | ||
|
||
startPointSatellites.forEach((item) => { | ||
console.log("finding routes for " + item.id); | ||
findRoute(item, endPointSatellites, data, [], [item]); | ||
}); | ||
|
||
}); | ||
}); | ||
|
||
// algorithm to find the route | ||
const findRoute = (itemA, endPointSatellites, allSatellites, nextSatellites, route) => { | ||
|
||
// console.log("processing satellite " + itemA.id); | ||
if(endPointSatellites.indexOf(itemA) > -1) { | ||
console.log(route.map(item => item.id).toString()); | ||
return route.push(itemA); | ||
} | ||
|
||
let horizonA = distanceToHorizon(itemA); | ||
allSatellites.forEach((itemB) => { | ||
let distance = calculateDistance(calculateCoordinate(itemA), calculateCoordinate(itemB)); | ||
let horizonB = distanceToHorizon(itemB); | ||
if (distance <= horizonA + horizonB && itemA.id != itemB.id && route.indexOf(itemB) === -1) { | ||
// console.log("satellite " + itemA.id + " can see satellite " + itemB.id); | ||
route.push(itemB); | ||
return findRoute(itemB, endPointSatellites, allSatellites, [], route); | ||
} | ||
}); | ||
} | ||
|
||
// the earth was considered to be a round sphere | ||
// http://stackoverflow.com/questions/8981943/lat-long-to-x-y-z-position-in-js-not-working | ||
const calculateCoordinate = (item) => { | ||
let cosLat = Math.cos(item.latitude * Math.PI / 180.0); | ||
let sinLat = Math.sin(item.latitude * Math.PI / 180.0); | ||
let cosLon = Math.cos(item.longitude * Math.PI / 180.0); | ||
let sinLon = Math.sin(item.longitude * Math.PI / 180.0); | ||
let rad = 6371000; | ||
return { | ||
x: rad * cosLat * cosLon, | ||
y: rad * cosLat * sinLon, | ||
z: rad * sinLat | ||
}; | ||
} | ||
|
||
// distance between two XYZ points | ||
const calculateDistance = (itemA, itemB) => { | ||
return Math.sqrt((itemB.x - itemA.x)*(itemB.x - itemA.x) + (itemB.y - itemA.y)*(itemB.y - itemA.y) + (itemB.z - itemA.z)*(itemB.z - itemA.z)); | ||
} | ||
|
||
// https://en.wikipedia.org/wiki/Horizon#Exact_formula_for_a_spherical_Earth | ||
const distanceToHorizon = (item) => { | ||
let rad = 6378.137; | ||
return Math.sqrt((2*rad*item.altitude) + item.altitude*item.altitude) * 1000; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#SEED: 0.7474189903587103,,,, | ||
SAT0,-49.989809896445685,-141.67445792329482,443.3808469970726, | ||
SAT1,-39.10675429294448,-74.544837218822,499.0880898277302, | ||
SAT2,83.42233682389036,-77.11484255197678,413.6241955027923, | ||
SAT3,-10.13892273327032,90.16479571928511,398.23254533024016, | ||
SAT4,29.404880122097865,8.144846168937875,489.3606360928758, | ||
SAT5,-81.82109567686196,133.68988873756825,544.3103553976588, | ||
SAT6,-46.24260757795606,-14.847486394058478,470.76451990187206, | ||
SAT7,64.42485618393547,47.37887014040777,610.364816764163, | ||
SAT8,19.08126826842792,-24.910706666102328,397.8221275369171, | ||
SAT9,19.566075415533916,-31.470111487830707,635.7562439676083, | ||
SAT10,85.01941071114706,173.7908087477121,402.78065912402116, | ||
SAT11,71.14558716997774,70.72320103888183,533.5347325592111, | ||
SAT12,52.458628054529186,-47.22552102041729,641.7820502721927, | ||
SAT13,-23.857902803843032,-40.95816667324394,473.15051553037347, | ||
SAT14,-12.122575869688262,114.90112609581126,697.559575298595, | ||
SAT15,45.46488891960638,-8.87289292209013,569.0871840142252, | ||
SAT16,-47.86318138083877,59.64941635409426,657.8632623519819, | ||
SAT17,-5.788814136343348,-56.662480811863404,629.4097191724984, | ||
SAT18,-89.31019373375635,-88.37195445788251,689.5225802781598, | ||
SAT19,74.39131000618991,-60.52409542667834,676.9580243561712, | ||
ROUTE,-39.94649332605746,58.69416308741867,38.379492171579216,-33.239929753094685 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#SEED: 0.8512005945667624,,,, | ||
SAT0,-31.515514556212928,87.08179651062551,582.7358538139997, | ||
SAT1,-48.26934238839663,-122.93555366860264,469.0712192530458, | ||
SAT2,-27.667364301435164,-140.02507468426145,303.68912409756587, | ||
SAT3,-76.74930806305389,144.8698532145853,370.2907496229141, | ||
SAT4,12.909577915857,124.03325931953088,341.09392296390376, | ||
SAT5,51.89096871691069,-28.03031629250512,509.83831068491713, | ||
SAT6,-80.64053148785267,-102.01806140406197,448.66298545343295, | ||
SAT7,14.714737200122883,-136.53179390731265,506.2145001678505, | ||
SAT8,-3.1155689906874926,28.12854530814883,461.51102445647126, | ||
SAT9,-11.503365635971491,-142.85187591007136,367.1868219400208, | ||
SAT10,88.59075908732822,3.60109102177654,662.6825328832408, | ||
SAT11,15.62638727922976,-66.32963975873389,417.48130055228233, | ||
SAT12,53.46937817062371,16.147183636313855,351.63545201171496, | ||
SAT13,14.571497481218316,55.72448076814268,385.7667154484617, | ||
SAT14,-88.946474171508,-13.231664391900978,650.3857506397896, | ||
SAT15,79.1935663590344,124.95624067309564,452.80276549592895, | ||
SAT16,55.36611460583683,-156.18665935606288,648.9629233992027, | ||
SAT17,26.76916805093866,-95.65338067833284,549.1524083387458, | ||
SAT18,44.66441827137686,159.7256268024209,593.6384154112761, | ||
SAT19,-70.97513902215374,-118.07840212299234,491.0271587520272, | ||
ROUTE,-65.1386535041511,165.61800790075932,69.2357035498647,69.32549665359181 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "reaktor-orbital-challenge", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "app.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "Jonne Pihlanen", | ||
"license": "ISC", | ||
"dependencies": { | ||
"csv": "^0.4.6", | ||
"fs-promise": "^0.5.0" | ||
} | ||
} |