边数要求在OpenGL中绘制一个圆 [英] Number of Sides Required to draw a circle in OpenGL

查看:155
本文介绍了边数要求在OpenGL中绘制一个圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道一些算法来计算使用多边形逼近一个圆所需的边的数量,如果半径,圆的r和圆的最大离开的多边形,D是给定的?我真的需要找到边的数量,因为我需要在OpenGL中绘制近似的圆。



另外,我们有NDC坐标每像素的分辨率通过P和解D = P / 2,我们可以保证我们的圆的精度在半像素范围内。 解析方案

你在这里描述的实际上是一个质量因素,它通常与错误估计结合在一起。



我们处理这个问题的常用方法是计算圆周的一小部分。最重要的是确定圆的切片弧长的差异,与连接圆周上相同两点的线段相比较。你可以使用更有效的方法,如面积,半径等差异,但这种方法应该是足够的。



想象一个八角形,用一个完美的圆圈限定。在这种情况下,误差是八角形上两个相邻点之间的直线长度的差异,以及连接这两个点的圆弧的长度。

< img src =https://i.stack.imgur.com/o9mEq.pngalt =在这里输入图片描述>



弧长很容易计算: PI * r * theta ,其中 r 是您的半径, theta 是两个点之间的角度(以弧度为单位),假定您从每个点绘制线到圆/多边形的中心。对于具有 n 边的封闭多边形,角度恰好<弧度c $(2 * PI / n)弧度。让与 n 这个值对应的弧长等于 A ,即 A = 2 * PI * r / n



两点之间的线长很容易计算。把你的圆圈分成 n 等腰三角形,然后把每个三角形分成两个直角三角形。你知道每个直角三角形的角度是 theta / 2 =(2 * PI / n)/ 2 =(PI / n),斜边是 - [R 。所以,你得到 sin(PI / n)= x / r 的方程式,其中 x 是长度的一半线段连接两个相邻点在你的外接多边形上。设该值为 B (即: B = 2x ,所以 B = 2 * r * sin(PI / n))。

现在,只需计算相对误差 E = | AB | / A (例如: | TrueValue-ApproxValue | / | TrueValue | ),您会得到一个很好的小数百分比错误向量。您可以使用上面的公式来设置 E 的约束(即:它不能大于某个值,比如1.05),以便看起来很好 。

因此,你可以编写一个函数来计算 A B E ,并循环 n 的值,并使其停止循环当 E 的计算值小于您的阈值时。


Does anyone know some algorithm to calculate the number of sides required to approximate a circle using polygon, if radius, r of the circle and maximum departure of the polygon from circularity, D is given? I really need to find the number of sides as I need to draw the approximated circle in OpenGL.

Also, we have the resolution of the screen in NDC coordinates per pixel given by P and solving D = P/2, we could guarantee that our circle is within half-pixel of accuracy.

解决方案

What you're describing here is effectively a quality factor, which often goes hand-in-hand with error estimates.

A common way we handle this is to calculate the error for a a small portion of the circumference of the circle. The most trivial is to determine the difference in arc length of a slice of the circle, compared to a line segment joining the same two points on the circumference. You could use more effective measures, like difference in area, radius, etc, but this method should be adequate.

Think of an octagon, circumscribed with a perfect circle. In this case, the error is the difference in length of the line between two adjacent points on the octagon, and the arc length of the circle joining those two points.

The arc length is easy enough to calculate: PI * r * theta, where r is your radius, and theta is the angle, in radians, between the two points, assuming you draw lines from each of these points to the center of the circle/polygon. For a closed polygon with n sides, the angle is just (2*PI/n) radians. Let the arc length corresponding to this value of n be equal to A, ie A=2*PI*r/n.

The line length between the two points is easily calculated. Just divide your circle into n isosceles triangles, and each of those into two right-triangles. You know the angle in each right triangle is theta/2 = (2*PI/n)/2 = (PI/n), and the hypotenuse is r. So, you get your equation of sin(PI/n)=x/r, where x is half the length of the line segment joining two adjacent points on your circumscribed polygon. Let this value be B (ie: B=2x, so B=2*r*sin(PI/n)).

Now, just calculate the relative error, E = |A-B| / A (ie: |TrueValue-ApproxValue|/|TrueValue|), and you get a nice little percentage, represented in decimal, of your error vector. You can use the above equations to set a constraint on E (ie: it cannot be greater than some value, say, 1.05), in order for it to "look good".

So, you could write a function that calculates A, B, and E from the above equations, and loop through values of n, and have it stop looping when the calculated value of E is less than your threshold.

这篇关于边数要求在OpenGL中绘制一个圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆