Skip to content

Commit

Permalink
finished algorithm. needs polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Palay committed Sep 12, 2019
1 parent 4f4d8cd commit 6e2ee0c
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 37 deletions.
4 changes: 2 additions & 2 deletions JustMap.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@



<div id="graph-container"></div>
<div id="graph-container"></div>

<script src="libs/d3.v5.js"></script>
<script src="scripts/d3script.js"></script>
Expand All @@ -34,7 +34,7 @@
left: 40
},
width = 1480 - margin.left - margin.right,
height = 1100 - margin.top - margin.bottom;
height = 700 - margin.top - margin.bottom;
let x = d3.scaleLinear()
.range([0, width]);
let y = d3.scaleLinear()
Expand Down
111 changes: 89 additions & 22 deletions bin/tmp.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,99 @@
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var data_1 = __importDefault(require("./data"));
var DEBUG = false;
// Functions
var dbg = function (str) {
if (DEBUG) {
return console.log(str);
}
};
var slope = function (anchor, point) {
return (point.lat - anchor.lat) / (point.lng - anchor.lng);
};
function CCW(p1, p2, p3) {
return (p3.y - p1.y) * (p2.x - p1.x) > (p2.y - p1.y) * (p3.x - p1.x);
}
var intersect = function (line1, line2) {
dbg("Checking intersection between " + line1.source.title + " -> " + line1.dest.title + " and " + line2.source.title + " -> " + line2.dest.title);
if (line1.dest.title == line2.dest.title || line1.dest.title == line2.source.title ||
line1.source.title == line2.dest.title || line1.source.title == line2.source.title) {
return false;
}
return (CCW(line1.source, line2.source, line2.dest) != CCW(line1.dest, line2.source, line2.dest)) && (CCW(line1.source, line1.dest, line2.source) != CCW(line1.source, line1.dest, line2.dest));
};
// Program start
var data_1 = __importDefault(require("./data"));
// sort the list to find the westernmost point
var sortedList = data_1["default"].sort(function (a, b) { return Math.abs(b.coordinates.lng) - Math.abs(a.coordinates.lng); });
var anchor = sortedList.shift(); // removes the Anchor from the list of portals to check
// Set that portal as anchor and remove it from the list of portals
var anchor = sortedList.shift();
console.log("Using " + anchor.title + " as the Anchor!");
console.log(sortedList
// calculates the slope and adds it to the portal
.map(function (portal) {
return __assign(__assign({}, portal), { slopeFromAnchor: slope(anchor.coordinates, portal.coordinates) });
})
// sorts based on the slope
.sort(function (a, b) { return a.slopeFromAnchor - b.slopeFromAnchor; })
// extracts just the bits we want to see
.map(function (portal) { return "from " + portal.title; }));
// Sort the rest of the portals by their slope from the anchor portal
var portals = sortedList.map(function (portal) {
return { x: portal.coordinates.lng,
y: portal.coordinates.lat,
title: portal.title,
slopeFromAnchor: slope(anchor.coordinates, portal.coordinates) };
}).sort(function (a, b) { return b.slopeFromAnchor - a.slopeFromAnchor; });
dbg(portals.map(function (portal) { return "from " + portal.title; }));
var allLinks = [];
var links = [];
links.push(portals.map(function (portal) {
return {
source: { title: portal.title, x: portal.x, y: portal.y },
dest: { title: anchor.title, x: anchor.coordinates.lng, y: anchor.coordinates.lat }
};
}));
allLinks = portals.map(function (portal) {
return {
source: { title: portal.title, x: portal.x, y: portal.y },
dest: { title: anchor.title, x: anchor.coordinates.lng, y: anchor.coordinates.lat }
};
});
console.log("starting from the top, checking for links from each portal");
var _loop_1 = function (i) {
links[i] = [];
var sourcePortal = portals[i];
console.log("checking " + sourcePortal.title);
var _loop_2 = function (destPortalIndex) {
var destPortal = portals[destPortalIndex];
var testedPassed = [];
var testedFailed = [];
console.log("..against " + destPortal.title);
// loop through all the links from lower # portals and see if they intersect with this new link
var conflict = false;
var _loop_3 = function (j) {
// start at 0 and go up to the portal we're checking as the destination
var testPortal = portals[j];
// find all links involving this portal
var linksWithTest = allLinks.filter(function (link) { return (link.dest.title === testPortal.title || link.source.title == testPortal.title); });
dbg("....found " + linksWithTest.length + " links with " + testPortal.title);
linksWithTest.forEach(function (link) {
dbg("......" + link.source.title + " -> " + link.dest.title);
});
conflict = linksWithTest.some(function (link) { return intersect(link, { source: sourcePortal, dest: destPortal }); });
};
for (var j = destPortalIndex; j < i; j++) // TODO: Was j=0. is this right?
{
_loop_3(j);
}
if (!conflict) {
console.log("........No conflict found! Add link from " + sourcePortal.title + " to " + destPortal.title);
allLinks.push({ source: sourcePortal, dest: destPortal });
links[i].push({ source: sourcePortal, dest: destPortal });
}
else {
console.log("........Conflict. Can't link " + sourcePortal.title + " to " + destPortal.title);
}
};
for (var destPortalIndex = 0; destPortalIndex < i; destPortalIndex++) {
_loop_2(destPortalIndex);
}
};
for (var i = 1; i < portals.length; i++) {
_loop_1(i);
}
console.log(links);
Binary file added output.json
Binary file not shown.
138 changes: 125 additions & 13 deletions tmp.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,134 @@
import data from './data'
type MaxLinkPortal = {
"guid": string;
"title": string;
"coordinates": Coords
"link": {
"intel": string;
"gmap": string;
};
"image": string;
}
type Coords =
{
lat: number
lng: number
}
type ShortPortal =
{
title: string
x: number
y: number
}
type Portal =
{
title: string
x: number
y: number
slopeFromAnchor: number
}
type Link =
{
source: ShortPortal
dest: ShortPortal
}

const DEBUG = false;

const slope = (anchor: {lat: number, lng: number}, point: {lat: number, lng: number}): number =>
// Functions
const dbg = (str: any) => {
if (DEBUG)
{return console.log(str)}
}
const slope = (anchor: Coords, point: Coords): number =>
(point.lat - anchor.lat)/(point.lng - anchor.lng)


function CCW(p1:ShortPortal, p2: ShortPortal, p3: ShortPortal): boolean {
return (p3.y - p1.y) * (p2.x - p1.x) > (p2.y - p1.y) * (p3.x - p1.x);
}

const intersect = (line1: Link, line2: Link): boolean => {
dbg(`Checking intersection between ${line1.source.title} -> ${line1.dest.title} and ${line2.source.title} -> ${line2.dest.title}`)
if (line1.dest.title == line2.dest.title || line1.dest.title == line2.source.title ||
line1.source.title == line2.dest.title || line1.source.title == line2.source.title ){
return false
}
return (CCW(line1.source, line2.source, line2.dest) != CCW(line1.dest, line2.source, line2.dest)) && (CCW(line1.source, line1.dest, line2.source) != CCW(line1.source, line1.dest, line2.dest));
}

// Program start
import data from './data';

// sort the list to find the westernmost point
let sortedList = data.sort((a,b) => Math.abs(b.coordinates.lng) - Math.abs(a.coordinates.lng))
let anchor = sortedList.shift() // removes the Anchor from the list of portals to check

// Set that portal as anchor and remove it from the list of portals
let anchor = sortedList.shift()
console.log(`Using ${anchor.title} as the Anchor!`)
console.log(

sortedList
// calculates the slope and adds it to the portal
.map((portal) => {
return {...portal, slopeFromAnchor: slope(anchor.coordinates, portal.coordinates)}

// Sort the rest of the portals by their slope from the anchor portal
let portals: Portal[] = sortedList.map(
(portal) => {
return {x: portal.coordinates.lng
, y: portal.coordinates.lat
, title: portal.title
, slopeFromAnchor: slope(anchor.coordinates, portal.coordinates)}
}).sort((a,b) => b.slopeFromAnchor - a.slopeFromAnchor)

dbg(portals.map(portal => `from ${portal.title}`));

let allLinks: Link[] = []
let links: Link[][] = []
links.push(portals.map((portal: ShortPortal):Link =>{
return {
source: {title: portal.title, x: portal.x, y: portal.y},
dest: {title: anchor.title, x: anchor.coordinates.lng, y: anchor.coordinates.lat}
}
}))
allLinks = portals.map((portal: ShortPortal):Link =>{
return {
source: {title: portal.title, x: portal.x, y: portal.y},
dest: {title: anchor.title, x: anchor.coordinates.lng, y: anchor.coordinates.lat}
}
})
// sorts based on the slope
.sort((a,b) => a.slopeFromAnchor - b.slopeFromAnchor)

// extracts just the bits we want to see
.map(portal => `from ${portal.title}`)
);
console.log(`starting from the top, checking for links from each portal`)
for (let i = 1; i < portals.length; i++) {
links[i] = []
const sourcePortal = portals[i];
console.log(`checking ${sourcePortal.title}`)
for (let destPortalIndex = 0; destPortalIndex < i; destPortalIndex++){
const destPortal = portals[destPortalIndex]
let testedPassed: number[] = []
let testedFailed: number[] = []
console.log(`..against ${destPortal.title}`)
// loop through all the links from lower # portals and see if they intersect with this new link
let conflict = false;
for (let j=destPortalIndex; j<i; j++) // TODO: Was j=0. is this right?
{
// start at 0 and go up to the portal we're checking as the destination
const testPortal = portals[j];
// find all links involving this portal
let linksWithTest = allLinks.filter((link) => (link.dest.title === testPortal.title || link.source.title == testPortal.title))
dbg(`....found ${linksWithTest.length} links with ${testPortal.title}`)
linksWithTest.forEach(link => {
dbg(`......${link.source.title} -> ${link.dest.title}`)
});
conflict = linksWithTest.some((link) => intersect(link,{source: sourcePortal, dest: destPortal} ))

}
if (!conflict){
console.log(`........No conflict found! Add link from ${sourcePortal.title} to ${destPortal.title}`)
allLinks.push({source: sourcePortal, dest: destPortal});
links[i].push({source: sourcePortal, dest: destPortal})
}
else {
console.log(`........Conflict. Can't link ${sourcePortal.title} to ${destPortal.title}`)
}


}
}

console.log(links)

0 comments on commit 6e2ee0c

Please sign in to comment.