How to obtain a rectangle's side's positions if its origin isn't in its middle?

74 Views Asked by At

Basically I have an algorithm which generates rooms and corridors randomly and each time a room is made, a new corridor is placed on $1/4$ of the room's sides and its origin point is set to that room's random side

But now I'm trying to make a collision algorithm, but I can't work out the corridor's side positions for it, because each of their origin points differ from one another. I can't set their origin points to their middle, because that would make them off-centered.

EDIT: https://github.com/MrCappucino/Dungeon This is all the source code with some pictures of the map.

enter image description here

EDIT 2: Even better explanation (I hope)

Possible starting (origin) points

1

There are 1 best solutions below

0
On

The following assumes that the corridor is a rectangle; that you know two points on the corridor, one at each end (indicated by the colored dots in your figures), that each of the known points is at the midpoint of a side of the rectangle; that the width of every corridor (that is, the length of the sides on which the known points lie) is the same; that the distance between the known points of every corridor is no less than some known minimum value; that the corridors are drawn in a coordinate system with $x$ and $y$ axes; and that the sides each corridor are parallel with one or the other of these axes.

For convenience, set $\delta$ to half the width of a corridor. That is, each corridor is $2\delta$ units wide. Also, consider the corridor to be aligned with the line through its two known points and with the coordinate axis parallel to that line.

Let the coordinates of the two known points at the corridor's ends by $(x_1,y_1)$ and $(x_2,y_2)$.

There are two cases to consider: a corridor aligned with the $x$ axis and a corridor aligned with the $y$ axis. There are a few ways to tell which case is which, all by comparing the coordinates of the known points:

  • Determine whether $y_1 = y_2$. This is safe even against the usual floating-point errors of computer programs as long as the coordinates of one endpoint of the corridor are gotten by copying the other endpoint and then modifying just one coordinate.
  • Determine whether $|y_1 - y_2| < \epsilon$, where $\epsilon$ is greater than the largest possible discrepancy that your floating point arithmetic might cause in the coordinate values, but smaller than the length of the shortest possible corridor.
  • Determine whether $|y_1 - y_2| < |x_1 - x_2|$. This works as long as the minimum corridor length is greater than the largest possible floating-point error you might encounter.

The two cases are:

Case 1: Depending on which of the three tests you use, this case occurs if $y_1 = y_2$, or $|y_1 - y_2| < \epsilon$, or $|y_1 - y_2| < |x_1 - x_2|$. This corridor is aligned with the $x$-axis. The coordinates of the corners of the corridor are then (in consecutive sequence around the perimeter)

$$\begin{array}{c} (x_1,y_1-\delta),\\ (x_1,y_1+\delta),\\ (x_2,y_2+\delta),\\ (x_2,y_2-\delta). \end{array}$$

Case 2: Depending on which of the three tests you use, this case occurs if $x_1 = x_2$, or $|x_1 - x_2| < \epsilon$, or $|x_1 - x_2| < |y_1 - y_2|$. This corridor is aligned with the $y$-axis. The coordinates of the corners of the corridor are then

$$\begin{array}{c} (x_1+\delta,y_1),\\ (x_1-\delta,y_1),\\ (x_2-\delta,y_2),\\ (x_2+\delta,y_2). \end{array}$$

If you encounter the "third case", where neither of the conditions you tested for Case 1 or Case 2 is true, then you invoke some kind of error reporting and debug your program, because at least one of its assumptions has been violated.