I created a canvas to draw on. I draw some rectangle objects
class Rectangle extends Shape{
constructor(posTopLeft, width, height){
const halfWidth = width * 0.5;
const halfHeight = height * 0.5;
super(width, height, new Position(posTopLeft.x + halfWidth, posTopLeft.y + halfHeight)); // set width, height and center position
this.centerLeftPosition = new Position(this.centerPosition.x - halfWidth, this.centerPosition.y);
this.centerRightPosition = new Position(this.centerLeftPosition.x + width, this.centerLeftPosition.y);
this.centerTopPosition = new Position(this.centerPosition.x, this.centerPosition.y - halfHeight);
this.centerBottomPosition = new Position(this.centerPosition.x, this.centerTopPosition.y + height);
this.topLeftPosition = posTopLeft;
this.bottomLeftPosition = new Position(this.topLeftPosition.x, this.topLeftPosition.y + height);
this.topRightPosition = new Position(this.topLeftPosition.x + width, this.topLeftPosition.y);
this.bottomRightPosition = new Position(this.topRightPosition.x, this.bottomLeftPosition.y);
}
}
I also want to draw a line from one rectangle to another.
class Lane{
constructor(startRect, targetRect){
this.startRect = startRect;
this.targetRect = targetRect;
}
}
How can I connect these two rectangles by a lane if the lane tries to calculate the shortest path?
Means if one rect is exactly above the other one the lane would go from center bottom from the first rect to center top from the second rect.
If the first rect is on the left and the second is on the right, the lane would go from center right to center left of the other rect.