Squis
Squis

Reputation: 931

How do I fit a rectangle in a diamond, while keeping the rectangle's ratio?

I have a rectangle and a square rhombus (a square rotated 45 degrees). I want to resize the rectangle, while keeping its ratio, so it fits inside the rhombus (they both have the same center and the rectangle's corners touch the rhombus' edges).

Bonus question: How can I fit an ellipse in such a rhombus?

I'm trying to do this in JavaScript.

Upvotes: 0

Views: 226

Answers (1)

MBo
MBo

Reputation: 80285

Imagine that rhombus with side A is centered at coordinate origin, old rectangle dimensions are W and H. So top right corner will lie on the rhombus side with equation

 x + y = A

ratio preserving equation

x / y = W / H

Substitute x in the first equation, solve the system for x and y, and

NewWidth = 2 * x
NewHeight = 2 * y  

Upvotes: 1

Related Questions