Skip to main content
 首页 » 编程设计

python之用 SVG 椭圆弧计算

2025年05月04日8webabcd

我正在开发自定义 pyQt SVG 渲染器,需要帮助。这纯粹是数学问题,因此您不需要 API。

要计算 SVG 椭圆弧,您需要两个半径 rx和 ry ;椭圆的旋转角度 x-axis-rotation ; large-arc-sweep-flag (1 或 0) 确定是否通过大于或小于 180 度绘制圆弧以及圆弧绕圆的方向; sweep-flag (1 或 0) 决定圆弧是朝负方向还是朝正方向绘制;和最后的 x和 y值决定圆弧的终点。

然而,QPainterPath.arcTo(self, float x, float y, float w, float h, float startAngle, float arcLenght)使用边界矩形 x, y, w, h ;起始角startAngle ; arcLength/sweepLength 是弧本身的长度。如何将 SVG 命令转换为 arcTo() 的内容方法可以用吗?

资料来源: W3C SVG , QPainterPath

请您参考如下方法:

SVG 椭圆弧使用“端点参数化”。

QPainterPath.arcTo 弧使用“中心参数化”。

您提供的 SVG 链接在标题为 "Elliptical arc implementation notes"这给出了 formulas for the conversion端点和中心参数化之间。


请注意 QPainterPath.arcTo documentation声明调用签名是

QPainterPath.arcTo (self, QRectF rect, float startAngle, float arcLength) 

但不是arcLength,它的描述是指sweepLength:

Creates an arc that occupies the given rectangle, beginning at the specified startAngle and extending sweepLength degrees counter-clockwise.

奇怪的是,它说 sweepLength 是以度为单位测量的,而弧长有一个传统的数学含义,它总是一个距离。

尽管文档使用了令人困惑的参数名称“arclength”,但我认为这意味着 arcTo 的最后一个参数是扫掠角 Δθ。

因此,如果您使用 SVG 实现说明中的公式 θ1, Δθ 我认为它们可以分别用于 startAngle 和 arcLength。