计算SVG圆弧的中心
我有以下信息:
- radiusX (rx)
- radiusY (ry)
- x1
- y1
- x2
- y2
SVG 规范允许您通过指定其半径以及起点和终点来定义圆弧。还有其他选项,例如 Large-arc-flag 和 sweep-flag ,它们有助于定义起点如何到达终点。 更多详细信息请参见此处。
我不太擅长数学,所以理解所有这些几乎是不可能的。
我想我正在寻找一个简单的方程,让我知道在 SVG 的 arc 命令接受的所有参数的情况下的 centerX 和 centerY 值。
任何帮助表示赞赏。
我搜索过 stackoverflow,但似乎没有一个答案可以用简单的英语解释解决方案。
I have the following information:
- radiusX (rx)
- radiusY (ry)
- x1
- y1
- x2
- y2
The SVG spec allows you to define an arc by specifying its radius, and start and end points. There are other options such as large-arc-flag
and sweep-flag
which help to define how you want the start-point to reach the end-point. More details here.
I am not mathematically inclined, so understanding all of this is near impossible.
I guess I am looking for a simple equation that results in me knowing the centerX
and centerY
values given all the arguments accepted by SVG's arc command.
Any help is appreciated.
I've search stackoverflow and none of the answers seem to explain the solution in plain english.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 W3C SVG 1.1 规范: 从端点参数化到中心参数化的转换
您可以看看详细的解释。
这是一个 JavaScript 实现。
使用示例:
svg
js
From W3C SVG 1.1 spec: Conversion from endpoint to center parameterization
You can take a look at the detailed explanation.
This is a javascript implementation.
Usage example:
svg
js
我正在考虑 x 轴旋转 = 0 的情况。
起点和终点的方程:
x1 = cx + rx * cos(StartAngle)
y1 = cy + ry * sin(StartAngle)
x2 = cx + rx * cos(EndAngle)
y2 = cy + ry * sin(EndAngle)
排除以下角度方程对给我们带来:
ry^2*(x1-cx)^2+rx^2*(y1-cy)^2=rx^2*ry^2
ry^2*(x2-cx)^2+rx^2*(y2- cy)^2=rx^2*ry^2
该方程组可以通过手动或数学包(Maple、Mathematica 等)的帮助来解析求解 (cx, cy)。二次方程有两个解(由于大弧旗和扫旗组合)。
I'm considering the case of x-axis-rotation = 0.
Equations for start and end points:
x1 = cx + rx * cos(StartAngle)
y1 = cy + ry * sin(StartAngle)
x2 = cx + rx * cos(EndAngle)
y2 = cy + ry * sin(EndAngle)
Excluding angles from equation pairs gives us:
ry^2*(x1-cx)^2+rx^2*(y1-cy)^2=rx^2*ry^2
ry^2*(x2-cx)^2+rx^2*(y2-cy)^2=rx^2*ry^2
This equation system can be analytically solved for (cx, cy) by hands or with help of math packets (Maple, Mathematica etc). There are two solutions of quadratic equation (due to large-arc-flag and sweep-flag combination).
通过实施 https://stackoverflow.com/a/12329083/17719752 我遇到以下问题:
rx != ry
,计算出的角度是错误的,我不是确保我是否正确理解了返回的角度,以及我是否是唯一遇到此问题的人,但如果您似乎也有错误的 startAngle / endAngle,我已经更新了代码,遵循 官方圆弧到中心参数化转换 并修复(在我看来)半径的点被错误地添加到角度方程中:
此 codepen 显示了现有计算角度之间的差异以及新的计算方法。左边的椭圆显示旧计算方法的计算角度,右边的椭圆显示新的角度:
使用示例:
SVG:
JS:
With the implementation of https://stackoverflow.com/a/12329083/17719752 I have the following problem:
rx != ry
, the calculated angles are wrongI'm not sure if I understood the returned angles correctly and if I'm the only one with this problem, but in case you also seem to have a wrong startAngle / endAngle, I've updated the code, following the official arc to center parameterization conversion and fixing the point where (in my opinion) the radii are wrongly added to the angle equation:
This codepen shows the differences of the calculated angles between the existing and the new calculation method. The left ellipse shows the calculated angles of the old calculated method, the right ellipse the new ones:
Usage example:
SVG:
JS: