选择圆半径以完全填充矩形

时间:2010-05-23 09:33:04

标签: math trigonometry geometry

pixman图像库可以在两个圆之间绘制径向颜色渐变。我希望径向渐变能够完全填充由“宽度”和“高度”定义的矩形区域。现在我的问题是,我应该如何选择外圈的半径?

我目前的参数如下:

A) inner circle (start of gradient)
center pointer of inner circle: (width*0.5|height*0.5)
radius of inner circle: 1
color: black

B) outer circle (end of gradient)
center pointer of outer circle: (width*0.5|height*0.5)
radius of outer circle: ???
color: white

如何选择外圆的半径以确保外圆完全填充由width * height定义的边界矩形。角落内不应有空白区域,该区域应完全被圆圈覆盖。换句话说,边界矩形宽度,高度必须完全适合外圆。选择

outer_radius = max(width, height) * 0.5

因为外圈的半径显然不够。它必须更大,但更大?

谢谢!

6 个答案:

答案 0 :(得分:6)

圆的直径应该是矩形的对角线,您可以从毕达哥拉斯定理中轻松计算出来。即:

outer_radius = 0.5 * sqrt(width * width + height * height)

答案 1 :(得分:0)

这只是毕达哥拉斯:

outer_radius = sqrt((width / 2)^2 + (height / 2)^2);

或更简单:

outer_radius = sqrt(width^2 + height^2) / 2;

答案 2 :(得分:0)

你的问题不明确,但也许你想要sqrt(w ^ 2 + h ^ 2)/ 2

这是从矩形中心到角落的距离。

答案 3 :(得分:0)

使用毕达哥拉斯:

outer_radius = sqrt(width*width + height*height)*0.5

答案 4 :(得分:0)

你想要一个直角三角形的斜边的长度,边长等于宽度/ 2和高度/ 2。或者,矩形的对角线长度的1/2。 平方根(h / 2 ^ 2 + w / 2 ^ 2) 或1/2 *平方根(h ^ 2 + w ^ 2)

答案 5 :(得分:-1)

制作一个小草图,并应用毕达哥拉斯定理:

[素描图片曾经去过这里;链接被破坏,主机现在被标记为恶意软件]

在代码中:

outer_radius = sqrt(0.25 * (width*width + height*height))