如何渲染尽可能少的顶点的圆? [英] How to render a circle with as few vertices as possible?
问题描述
我想弄清楚如何决定需要多少顶点来使我的圈子尽可能平滑。
I'm trying to figure out how to decide how many vertices I need to have to make my circle look as smooth as possible.
这里是一个例子两个圆形,共有24个顶点:$ b $ b
Here is an example of two circles, both having 24 vertices:
如你所见,圆圈越大,我需要隐藏直线的顶点越多。
As you see, the bigger the circle becomes, the more vertices I need to hide the straight lines.
刚开始我认为最小边缘上的一条线的长度应该是6px,但是当我增加圆的大小时,该方法失败:我有太多的顶点。我也想过计算角度,但我很快意识到角度在不同大小的圆圈上没有不同。我也检查了这个答案,但我没有一个线索如何将它转换为代码(和一些奇怪的东西有: th
使用自己计算自己)和我认为它甚至不工作,因为作者使用从一个切片到圆的中间的角度,如果圆圈变大则不改变。
At first I thought that the minimum length of one line on the edge should be 6px, but that approach failed when I increased the circle size: I got way too many vertices. I also thought about calculating the angles, but I quickly realised that angles doesn't differ on different sized circles. I also checked this answer, but I don't have a clue how to convert it into code (and some weird stuff there: th
uses itself for calculating itself), and I think it doesn't even work, since the author is using the angle from one slice to the middle of circle, which doesn't change if the circle gets larger.
然后我意识到,也许解决方案是检查两个顶点之间的角度,在这样:
Then I realised that maybe the solution is to check the angle between two vertices at the edges, in this way:
如您所见,顶点越少,这些三角形的长度越大。所以这是必须的答案,我只是不知道如何使用这些信息计算顶点的数量。
As you see, the fewer vertices, the bigger the lengths are for those triangles. So this has to be the answer, I just don't know how to calculate the number of vertices by using this information.
推荐答案
你从这个答案所需要的决定性公式就是这个:
The decisive formula that you need from that answer is this one:
th = arccos(2 * (1 - e / r)^2 - 1)
这会告诉你两个顶点之间的角度,其中 r
是圆的半径, e
是您愿意容忍的最大误差,即您的多边形与圆的最大偏差 - 这是您的图表中标记的误差。例如,您可以选择将 e
设置为像素的0.5。
This tells you the angle between two vertices, where r
is the radius of the circle and e
is the maximum error you're willing to tolerate, i.e. the maximum deviation of your polygon from the circle -- this is the error marked in your diagram. For example, you might choose to set e
to 0.5 of a pixel.
因为 th
以弧度表示,360度(整圆)等于以弧度表示的 2 * pi
,您需要的顶点数
Because th
is measured in radians, and 360 degrees (a full circle) is equal to 2*pi
in radians, the number of vertices you need is
num_vertices = ceil(2*pi/th)
这篇关于如何渲染尽可能少的顶点的圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!