From 949c29c9f9c81b035e0f264b2a4224a85769ce7b Mon Sep 17 00:00:00 2001 From: Jacob Date: Tue, 26 Mar 2019 14:41:13 +0100 Subject: [PATCH] Corrects tergetReached algorithm, adds graph export Target reached now checks if target is closer than firklift speed to avoid skipping the target location.Magazine can now be exported as graph. --- magazine/static/magazine/forklift.js | 7 +++- magazine/static/magazine/sketch.js | 52 +++++++++++++++++----------- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/magazine/static/magazine/forklift.js b/magazine/static/magazine/forklift.js index fee6151..fe59d78 100644 --- a/magazine/static/magazine/forklift.js +++ b/magazine/static/magazine/forklift.js @@ -45,7 +45,12 @@ class Forklift { move() { this.positoin = p5.Vector.add(this.positoin, this.velocity); debugger; - if (this.positoin.equals(sections[this.currentTarget])) { + if ( + Math.abs(this.positoin.x - sections[this.currentTarget].x) <= + this.speed && + Math.abs(this.positoin.y - sections[this.currentTarget].y) <= + this.speed + ) { this.nextTarget(); this.setVelocity(); } diff --git a/magazine/static/magazine/sketch.js b/magazine/static/magazine/sketch.js index 6f66af1..7df03fd 100644 --- a/magazine/static/magazine/sketch.js +++ b/magazine/static/magazine/sketch.js @@ -14,7 +14,8 @@ function setup() { select('#button').mousePressed(deliver); // Create a forklift instance - forklift = new Forklift(sections['START'].x, sections['START'].y); + forklift = new Forklift(sections[0].x, sections[0].y); + console.log(magazineToGraph()); } // This runs every frame @@ -48,7 +49,7 @@ function drawMagazine() { textAlign(CENTER, CENTER); // Draw all sections in the magazine for (let section of Object.keys(sections)) { - if (section === 'START') { + if (section === 0) { fill(80); rect(sections[section].x - 15, sections[section].y, 30, 30); } else { @@ -61,7 +62,7 @@ function drawMagazine() { } function deliver() { - path = ['B', 'A', 'D', 'E', 'F']; + path = [2, 1, 5, 4]; forklift.setPath(path); going = true; } @@ -70,22 +71,33 @@ function createMagazineLayout() { unit = width / 5; sections = { - START: { x: unit * 2, y: 0 }, - A: createVector(unit, unit), - B: createVector(2 * unit, unit), - C: createVector(2 * unit, 2 * unit), - D: createVector(unit, 2 * unit), - E: createVector(unit, 3 * unit), - F: createVector(2 * unit, 3 * unit) + 0: { x: unit * 2, y: 0 }, + 1: createVector(unit, unit), + 2: createVector(2 * unit, unit), + 3: createVector(2 * unit, 2 * unit), + 4: createVector(3 * unit, 2 * unit), + 5: createVector(unit, 3 * unit), + 6: createVector(2 * unit, 3 * unit) }; - roads = [ - ['START', 'B'], - ['A', 'B'], - ['A', 'E'], - ['E', 'D'], - ['E', 'F'], - ['D', 'C'], - ['B', 'C'], - ['C', 'F'] - ]; + roads = [[0, 2], [1, 2], [1, 5], [5, 4], [5, 6], [4, 3], [2, 3], [3, 6]]; +} + +function magazineToGraph() { + graph = {}; + for (let key of Object.keys(sections)) { + graph[key] = {}; + } + for (let road of roads) { + start = road[0]; + end = road[1]; + graph[start][end] = Math.sqrt( + Math.pow(sections[start].x - sections[end].x, 2) + + Math.pow(sections[start].y - sections[end].y, 2) + ); + graph[end][start] = Math.sqrt( + Math.pow(sections[start].x - sections[end].x, 2) + + Math.pow(sections[start].y - sections[end].y, 2) + ); + } + return graph; }