Reputation: 931
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
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