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.
This commit is contained in:
Jacob 2019-03-26 14:41:13 +01:00
parent 60240bb483
commit 949c29c9f9
2 changed files with 38 additions and 21 deletions

View File

@ -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();
}

View File

@ -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;
}