Draw a rectangle centered on a hexagon

wingnut

Donator
Donator
Joined
May 10, 2013
Messages
129
Reaction score
0
Points
16
I am trying to draw a Rectangle centered on a hexagon like in the attached illustration and I would like to ask for help how to achieve that.

For the drawing API I'm using I need to know:
  • the location of the top-left point of the rectangle

I know:
  • the location of the hexagon's center
  • the width and height of the rectangle

The size of the line from the center of the hexagon to the top-left point of the rectangle should be [math]d=\frac{\sqrt{r.width^2 + r.height^2}}{2}[/math]

With the law of cosines I can get the required angle from the center of the hexagon to the rectangle's corner:
[math]angle = arccos(\frac{a^2+b^2-c^2}{2ab})[/math]

So in my case I should be able to calculate this angle with: [math]angle = arccos(\frac{2d^2-r.height^2}{4d})[/math]

And this is where I'm stuck because I am not sure if this is correct and how to calculate the location of the rectangle's top-left corner with that?
 

Attachments

  • rect_hex.png
    rect_hex.png
    10.6 KB · Views: 37
Why don't you solve it in vector coordinates? (And then go to trigonometry)

[math]\vec{v_l} = \begin{pmatrix}x_{hex} \\ y_{hex} \end{pmatrix} - \begin{pmatrix}\frac{width}{2} \\ \frac{height}{2} \end{pmatrix}[/math]
 
Last edited:
I don't quite follow... You have width and height of the rectangle, and center of the hexagon, which judging from the picture would also be the center of the rectangle...

So, having width, height and center of a retangle, where exactly do you need any angle to draw it (except if it is not aligned with the coordinate system, but in that case I assume you know the rotation of the hexagon, which makes the problem the same plus rotating the corners...)?
 
Indeed, it's puzzling why this is puzzling. No offense. Maybe I don't understand what the problem is. But here's what I think I understand.

You know the center of the hexagon, hence you know the center of the rectangle (because they coincide). Let this point be (hx, hy).

If the rectangle is axis aligned, then top left corner is (hx-0.5*width, hy-0.5*height).

Suppose instead that you want the rectangle height to be aligned with the line from the hexagon center to the 270deg corner (let this be cx, cy). For the rest of this post I will assume you know (cx, cy); if not, we'll see how you may get them from what you know.

Let dx = (cx-hx)
Let dy = (cy-hy)
Let r = sqrt(dx^2 + dy^2)
Let px = hx-0.5*width
Let py = hy-0.5*height (the axis aligned rectangle corner coordinates from before)

then
c = dy/r
s = dx/r (nope, not an error; since the axis towards the 270deg corner is vertical, the s aka sin variable looks at the dx component)

and your top left rectangle corner should be

rx = c*px - s*py
ry = s*px + c*py

(aka, a rotation of the axis aligned version, by an angle given by the axis towards the 270deg corner).

That should be it, lol, unless I messed up the signs for the s factors :P Try and see.
 
D'oh :facepalm:

If the rectangle is axis aligned, then top left corner is (hx-0.5*width, hy-0.5*height).

This sentence rammed it home for me. I definitely was thinking too complicated.

Thank you all for your comments.
 
Back
Top