-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmp.ts
205 lines (170 loc) · 6.26 KB
/
tmp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import * as fs from 'fs';
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;
// 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 strData from './data';
let data: {
"guid": string;
"title": string;
"coordinates": {
"lat": number;
"lng": number;
};
"link": {
"intel": string;
"gmap": string;
};
"image": string;}[]
data = strData.map(datum => { return {
guid: datum.guid,
title: datum.title,
link: datum.link,
image: datum.image,
coordinates: {
lat: +datum.coordinates.lat,
lng: +datum.coordinates.lng
}
}})
//init output
let output: {
anchor?: Portal
portalList?: Portal[]
linksFrom?: Array<Link[]>
linksTo?: Array<Link[]>
allLinks?: Array<Link>
betterPortalList? : {portal: Portal, linksFrom: Link[], linksTo: Link[]}[]
} = {}
// sort the list to find the westernmost point
let sortedList = data.sort((a,b) => Math.abs(b.coordinates.lng) - Math.abs(a.coordinates.lng))
output.anchor = {title: sortedList[0].title, x: sortedList[0].coordinates.lng, y: sortedList[0].coordinates.lat }
// 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!`)
// 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)
// Add the portals to the output Portal List
output.portalList = [output.anchor, ...portals]
output.betterPortalList =
[
{portal: output.anchor, linksFrom: [] as Link[], linksTo: [] as Link[]}]
.concat(portals.map(
(portal => {return {portal: portal, linksFrom: [] as Link[], linksTo: [] as Link[]}})
))
dbg(portals.map(portal => `from ${portal.title}`));
// Running tally of all possible links
let allLinks: Link[] = []
// add the links to the anchor to allLinks
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}
}
})
output.allLinks = [...allLinks]
// all these links go TO the anchor
output.linksTo = []
for (let index = 0; index < portals.length+1; index++) {
output.linksTo[index] = [];
}
output.linksTo[0] = [...allLinks]
output.betterPortalList[0].linksTo = [...allLinks]
// add each link to the linksFrom
output.linksFrom = []
for (let index = 0; index < portals.length+1; index++) {
output.linksFrom[index] = [];
//output.betterPortalList[index].linksFrom = [] // This should already be done when making betterPortalList
}
for (let index = 0; index < allLinks.length; index++) {
const link = allLinks[index];
output.linksFrom[index+1] = [link,...output.linksFrom[index+1]]
output.betterPortalList[index+1].linksFrom = [link, ...output.betterPortalList[index+1].linksFrom]
}
/*
portals: [ A, b, c, d]
linksFrom:[[0],[1],[1],[1]]
linksTo: [[86],[],[],[]]
*/
// Start with the 2nd highest sloped portal.
// The highest slope is trivial. It has 1 link TO the anchor
console.log(`starting from the top, checking for links from each portal`)
for (let sourcePortalIndex = 1; sourcePortalIndex < portals.length; sourcePortalIndex++) {
// links[i] = []
const sourcePortal = portals[sourcePortalIndex];
console.log(`checking ${sourcePortal.title}`)
for (let destPortalIndex = 0; destPortalIndex < sourcePortalIndex; destPortalIndex++){
const destPortal = portals[destPortalIndex]
const tmpLink = {source: sourcePortal, dest: destPortal}
console.log(`..against ${destPortal.title}`)
let conflict = false;
conflict = allLinks.some((link) => intersect(link, tmpLink))
if (!conflict){
console.log(`........No conflict found! Add link from ${sourcePortal.title} to ${destPortal.title}`)
allLinks.push(tmpLink);
output.linksFrom[sourcePortalIndex+1].push(tmpLink)
output.linksTo[destPortalIndex+1].push(tmpLink)
output.betterPortalList[sourcePortalIndex+1].linksFrom.push(tmpLink)
output.betterPortalList[destPortalIndex+1].linksTo.push(tmpLink)
}
else {
console.log(`........Conflict. Can't link ${sourcePortal.title} to ${destPortal.title}`)
}
}
}
output.allLinks = [...allLinks]
//console.log(output)
fs.writeFileSync('./tmpoutput.json',JSON.stringify(output))
console.log(`~~~~~~~~~~~~~~~~~~~Portal,Keys~~~~~~~~~~~~~~~~~~~`)
output.linksTo.map((x,i) => `${output.portalList[i].title},${x.length}`).forEach(key => console.log(key))
console.log(`~~~~~~~~~~~~~~~~~~~List of Links~~~~~~~~~~~~~~~~~~~`)
output.linksFrom.forEach((links, i) => {
console.log(output.portalList[i].title)
links.forEach((link, i) => {
console.log(`..${i}: ${link.dest.title}`)
})
})