Skip to content

Commit

Permalink
fixed automatic placement of nodes on graph
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Wicenec committed Jan 31, 2024
1 parent 7ece4be commit a9df89b
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/Eagle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4318,34 +4318,29 @@ export class Eagle {

getNewNodePosition = (width: number, height: number) : {x:number, y:number} => {
const MARGIN = 100; // buffer to keep new nodes away from the maxX and maxY sides of the LG display area
const navBarHeight = 84
let suitablePositionFound = false;
let numIterations = 0;
const MAX_ITERATIONS = 100;
let x;
let y;

while (!suitablePositionFound && numIterations <= MAX_ITERATIONS){
// get visible screen size
const minX = this.leftWindow().shown() ? this.leftWindow().width(): 0;
const maxX = this.rightWindow().shown() ? $('#logicalGraphD3Div').width() - this.rightWindow().width() - width - MARGIN : $('#logicalGraphD3Div').width() - width - MARGIN;
const minY = 0;
const maxY = $('#logicalGraphD3Div').height() - height - MARGIN;

const minX = this.leftWindow().shown() ? this.leftWindow().width()+MARGIN: 0+MARGIN;
const maxX = this.rightWindow().shown() ? $('#logicalGraphParent').width() - this.rightWindow().width() - width - MARGIN : $('#logicalGraphParent').width() - width - MARGIN;
const minY = 0 + navBarHeight + MARGIN;
const maxY = $('#logicalGraphParent').height() - height - MARGIN + navBarHeight;
// choose random position within minimums and maximums determined above
const randomX = Math.floor(Math.random() * (maxX - minX + 1) + minX);
const randomY = Math.floor(Math.random() * (maxY - minY + 1) + minY);

x = randomX;
y = randomY;

// modify random positions using current translation of viewport
x -= this.globalOffsetX();
y -= this.globalOffsetY();

x /= this.globalScale();
y /= this.globalScale();

//console.log("Candidate Position", numIterations, ":", x, ",", y, "X:", minX, "-", maxX, "Y:", minY, "-", maxY);
//translate the chosen arandomised position into graph co-ordinates
x= GraphRenderer.SCREEN_TO_GRAPH_POSITION_X(x)
y=GraphRenderer.SCREEN_TO_GRAPH_POSITION_Y(y)

// check position is suitable, doesn't collide with any existing nodes
const collision = this.logicalGraph().checkForNodeAt(x, y, width, height, null);
Expand Down

0 comments on commit a9df89b

Please sign in to comment.