使用二次贝塞尔曲线和三次贝塞尔曲线绘制椭圆
我正在构建一个 JavaScript 模块来向 HTML5 canvas 元素添加便利功能。我试图提供尽可能多的不同实现来填写我的模块。要查看我的进度,请访问我的项目页面和我的示例页面。
我有一种使用三次贝塞尔曲线的椭圆绘制方法。我知道二次贝塞尔曲线可以转换为三次贝塞尔曲线,但我有一些问题:
- 近似圆时误差范围是否有差异?椭圆形?
- 有什么理由同时采用这两种实现吗? (性能、准确性等)
- 我是否缺少任何其他绘制椭圆的方法?
PS 这没有直接关系,但是在这样的模块中是否还有其他功能会更好?
注意:这不是家庭作业。
编辑:这是我的椭圆代码(xDis 是 x 中的半径,yDis 是 y 中的半径):
function ellipse(x, y, xDis, yDis) {
var kappa = 0.5522848, // 4 * ((√(2) - 1) / 3)
ox = xDis * kappa, // control point offset horizontal
oy = yDis * kappa, // control point offset vertical
xe = x + xDis, // x-end
ye = y + yDis; // y-end
this.moveTo(x - xDis, y);
this.bezierCurveTo(x - xDis, y - oy, x - ox, y - yDis, x, y - yDis);
this.bezierCurveTo(x + ox, y - yDis, xe, y - oy, xe, y);
this.bezierCurveTo(xe, y + oy, x + ox, ye, x, ye);
this.bezierCurveTo(x - ox, ye, x - xDis, y + oy, x - xDis, y);
}
相关问题:
I am building a JavaScript module to add convenience functions to the HTML5 canvas element. I am trying to give as many different implementations as possible to fill out my module. To see my progress, please visit my project page and my examples page.
I have an ellipse drawing method that uses cubic bezier curves. I know that quadratic bezier curves can be converted to cubic bezier curves, but I have some questions:
- Is there any difference in margin for error when approximating a circle? An ellipse?
- Is there any reason to have both implementations? (performance, accuracy, etc)
- Am I missing any other methods for drawing ellipses?
P.S. This isn't directly related, but are there any other functions that would be nice to have in such a module?
Note: This is not a homework assignment.
EDIT: Here is my code for an ellipse (xDis is radius in x, yDis is radius in y):
function ellipse(x, y, xDis, yDis) {
var kappa = 0.5522848, // 4 * ((√(2) - 1) / 3)
ox = xDis * kappa, // control point offset horizontal
oy = yDis * kappa, // control point offset vertical
xe = x + xDis, // x-end
ye = y + yDis; // y-end
this.moveTo(x - xDis, y);
this.bezierCurveTo(x - xDis, y - oy, x - ox, y - yDis, x, y - yDis);
this.bezierCurveTo(x + ox, y - yDis, xe, y - oy, xe, y);
this.bezierCurveTo(xe, y + oy, x + ox, ye, x, ye);
this.bezierCurveTo(x - ox, ye, x - xDis, y + oy, x - xDis, y);
}
Related questions:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
三次贝塞尔曲线还有两条 自由度高于二次自由度。因此可以进一步减少错误。尽管曲线阶数越高,选择控制点的数学就越复杂,但三次曲线应该足够简单。
性能上的差别并不大。还有一些乘法和加法。
虽然这可以通过旋转平面来实现,但如果能够指定椭圆的轴就更好了。一个中心和两个轴将形成椭圆
center + cos(t)*axis1 + sin(t)*axis2
。该库的另一个功能是不同类型的多项式曲线和样条曲线。 B-spline 类似于贝塞尔曲线,但可以沿着多个控制点继续。
A cubic Bezier curve has two more degrees of freedom than a quadratic one. It can thus reduce error further. Though the math for choosing the control-points gets more complex the higher the degree of the curve, a cubic curve should be simple enough.
The difference in performance is not much. A few more multiplications and additions.
Although this could be achieved by rotating the plane, it would be nice to be able to specify the axes of the ellipse. A center, and two axes would form the ellipse
center + cos(t)*axis1 + sin(t)*axis2
.Another feature of the library could be different kinds of polynomial curves and splines. A B-spline is similar to a Bezier curve, but can continue along multiple control points.
如果它是一个普通椭圆,那么绘制椭圆的标准参数形式并在其具有非规范方向时将结果通过旋转变换不是更容易吗?
If it's a vanilla ellipse, wouldn't it be easier to plot the standard parametric form of the ellipse, and put the result through a rotation transform if it has a non-canonical orientation?