Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 11, 2019. It is now read-only.

Commit 55ecee3

Browse files
committed
fix random
1 parent f5975b6 commit 55ecee3

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

‎build/assets/index.js‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/reducers/reducerUtils.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const ONE_FIELD_SETTERS = {
2121
export const getInitialState = () => ({
2222
graph: new Graph(),
2323
distanceType: DijkstraSearch.BY_COORDINATES,
24-
showLinkDots: true,
24+
showLinkDots: false,
2525
showProperties: false,
2626
activeItem: null,
2727
answers: null,

‎src/utils.js‎

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,51 @@ export const getFirstSillyName = () =>
5959
export 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+
6265
export 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(leti=0;i<LINKS_LENGTH;i+=1) {
81-
const startIndex = getRandomInt(0,NODES_LENGTH-1);
82-
letendIndex=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+
constdistance=
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /