@@ -59,38 +59,51 @@ export const getFirstSillyName = () =>
5959export const getRandomInt = ( min , max ) =>
6060 Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ;
6161
62+ export const getDistance = ( a , b ) =>
63+ Math . sqrt ( ( a . x - b . x ) ** 2 + ( a . y - b . y ) ** 2 ) ;
64+ 6265export const getExampleGraphJSON = ( ) => {
6366 // return HARCODED_GRAPH;
64- const NODES_LENGTH = 25 ;
65- const LINKS_LENGTH = 25 ;
6667
6768 const nodes = [ ] ;
6869 const links = [ ] ;
69- for ( let i = 0 ; i < 5 ; i += 1 ) {
70- for ( let j = 0 ; j < 5 ; j += 1 ) {
71- const name = getFirstSillyName ( ) ;
70+ 71+ const usedNames = [ ] ;
72+ 73+ for ( let i = 0 ; i < 8 ; i += 1 ) {
74+ for ( let j = 0 ; j < 8 ; j += 1 ) {
75+ let name ;
76+ do {
77+ name = getFirstSillyName ( ) ;
78+ } while ( usedNames . includes ( name ) ) ;
79+ usedNames . push ( name ) ;
80+ 7281 const weight = getRandomInt ( 1 , 1000 ) ;
73- const x = getRandomInt ( 100 * i + 20 , 100 * ( i + 1 ) - 20 ) ;
74- const y = getRandomInt ( 100 * j + 20 , 100 * ( j + 1 ) - 20 ) ;
82+ const x = getRandomInt ( 125 * i + 20 , 125 * ( i + 1 ) - 20 ) ;
83+ const y = getRandomInt ( 125 * j + 20 , 125 * ( j + 1 ) - 20 ) ;
7584 const node = { name, weight, x, y } ;
7685 nodes . push ( node ) ;
7786 }
7887 }
7988
80- for ( let i = 0 ; i < LINKS_LENGTH ; i += 1 ) {
81- const startIndex = getRandomInt ( 0 , NODES_LENGTH - 1 ) ;
82- let endIndex = true ;
83- do {
84- endIndex = getRandomInt ( 0 , NODES_LENGTH - 1 ) ;
85- } while ( endIndex === startIndex ) ;
89+ nodes . forEach ( node => {
90+ const destinations = nodes . map ( nd => {
91+ const distance =
92+ nd . name === node . name ? Infinity : getDistance ( node , nd ) ;
93+ return { name : nd . name , distance } ;
94+ } ) ;
8695
87- const start = nodes [ startIndex ] . name ;
88- const end = nodes [ endIndex ] . name ;
89- const length = getRandomInt ( 1 , 1000 ) ;
96+ destinations . sort ( ( a , b ) => a . distance - b . distance ) ;
9097
91- const link = { start, end, length } ;
92- links . push ( link ) ;
93- }
98+ destinations . slice ( 0 , 3 ) . forEach ( destination => {
99+ const start = node . name ;
100+ const end = destination . name ;
101+ const length = getRandomInt ( 1 , 1000 ) ;
102+ 103+ const link = { start, end, length } ;
104+ links . push ( link ) ;
105+ } ) ;
106+ } ) ;
94107
95108 return { nodes, links } ;
96109} ;
0 commit comments