首页 > 解决方案 > 如何找到能够容纳在已知尺寸的椭圆内的最大矩形?

问题描述

更具体地说:给定一个具有已知宽度、高度和 x、y 位置的椭圆——如何找到能够在其中绘制的最大可能矩形的宽度、高度和 x、y 位置。

// eWidth, eHeight, eX, eY are known, arbitrary values
const ellipse = draw.ellipse(eWidth, eHeight).move(eX, eY);

// rWidth, rHeight, rX, rY are unknown
const rect = draw.rect(rWidth, rHeight).move(rX, rY);

标签: javascriptalgorithmgeometry

解决方案


好吧,看这张图就清楚了:

在此处输入图像描述

从这个答案https://stackoverflow.com/a/6716520/160937

// eWidth, eHeight, eX, eY are known, arbitrary values
const ellipse = draw.ellipse(eWidth, eHeight).move(eX, eY);

// Each radius * Square root of 2
const rect = draw.rect((ellipse.width() / 2) * Math.SQRT2, (ellipse.height() / 2) * Math.SQRT2)
  // Then move to the center of the ellipse
  .cx(ellipse.cx()).cy(ellipse.cy());

推荐阅读