Tat*_*ize 5 python svg ellipse scipy automatic-ref-counting
我希望能够计算SVG弧的确切长度。我可以很轻松地完成所有操作。但是,我不确定是否有解决方案或该解决方案的确切实现。
这是椭圆圆周的精确解。使用流行的库很好。我完全掌握没有简单的解决方案,因为它们都需要精确的超几何函数。
from scipy import pi, sqrt
from scipy.special import hyp2f1
def exact(a, b):
t = ((a - b) / (a + b)) ** 2
return pi * (a + b) * hyp2f1(-0.5, -0.5, 1, t)
a = 2.667950e9
b = 6.782819e8
print(exact(a, b))
Run Code Online (Sandbox Code Playgroud)
我的想法是,如果您恰好scipy安装了该代码,则将其作为选择加入的代码,它将使用确切的超精巧解决方案,否则将退回到较弱的近似代码(逐行缩小线段,直到误差很小)。问题是这里的数学水平高于我。而且我不知道是否有一些方法可以指定起点和终点。
大多数逼近解都适用于椭圆,但是我只想要弧线。对于我来说,可能还存在一个未知的解决方案,用于计算椭圆上的弧长,但由于开始和结束位置可以在任何地方。说扫掠角为可能的总角度的15%,因此不是椭圆周长的15%,似乎并不是立即可行的。
更有效的花式弧近似也可能很好。椭圆近似值越来越好,但是我不能从椭圆周长到圆弧长度,所以这些对当前无济于事。
假设圆弧参数化是椭圆上的起点和终点。因为这就是SVG参数化的方式。但是,诸如arc_length参数化之类的没有重言式的东西都是正确的答案。
如果您想徒手计算这个值并使用标准库,您可以根据以下公式进行计算。这仅对椭圆上半部分上的两个点有效,因为但acos我们将直接将其与角度一起使用。
计算包括以下步骤:
scipy.special.ellipeinc正如评论中所建议的,您可以在此处使用。步骤2很简单,只需使用旋转矩阵即可(注意角度rot在顺时针方向为正):
m = [
[math.cos(rot), math.sin(rot)],
[-math.sin(rot), math.cos(rot)]
]
Run Code Online (Sandbox Code Playgroud)
第 3 步在这个答案中得到了很好的解释。请注意, 获得的值a1是对 pi 求模,因为它是通过 获得的atan。这意味着您需要计算两个角度的中心点t1并t2检查它们是否匹配。如果没有,请添加 pia1并再次检查。
第 4 步非常简单。将区间 [ t1, t2] 分为 n 段,获取每段末尾的函数值,将其除以段长度,然后将其相加。您可以尝试通过在每个段的中点获取函数的值来改进这一点,但我不确定这样做有多大好处。段的数量可能对精度有更大的影响。
这是上面的一个非常粗糙的 Python 版本(请忍受丑陋的编码风格,我是在旅行时在手机上这样做的)
import math
PREC = 1E-6
# matrix vector multiplication
def transform(m, p):
return ((sum(x * y for x, y in zip(m_r, p))) for m_r in m)
# the partial integral function
def ellipse_part_integral(t1, t2, a, b, n=100):
# function to integrate
def f(t):
return math.sqrt(1 - (1 - a**2 / b**2) * math.sin(t)**2)
start = min(t1, t2)
seg_len = abs(t1 - t2) / n
return - b * sum(f(start + seg_len * (i + 1)) * seg_len for i in range(n))
def ellipse_arc_length(x1, y1, a, b, rot, large_arc, sweep, x2, y2):
if abs(x1 - x2) < PREC and abs(y1 - y2) < PREC:
return 0
# get rot in radians
rot = math.pi / 180 * rot
# get the coordinates in the rotated coordinate system
m = [
[math.cos(rot), math.sin(rot)],
[- math.sin(rot), math.cos(rot)]
]
x1_loc, y1_loc, x2_loc, y2_loc = *transform(m, (x1,y1)), *transform(m, (x2,y2))
r1 = (x1_loc - x2_loc) / (2 * a)
r2 = (y2_loc - y1_loc) / (2 * b)
# avoid division by 0 if both points have same y coord
if abs(r2) > PREC:
a1 = math.atan(r1 / r2)
else:
a1 = r1 / abs(r1) * math.pi / 2
if abs(math.cos(a1)) > PREC:
a2 = math.asin(r2 / math.cos(a1))
else:
a2 = math.asin(r1 / math.sin(a1))
# calculate the angle of start and end point
t1 = a1 + a2
t2 = a1 - a2
# calculate centre point coords
x0 = x1_loc - a * math.cos(t1)
y0 = y1_loc - b * math.sin(t1)
x0s = x2_loc - a * math.cos(t2)
y0s = y2_loc - b * math.sin(t2)
# a1 value is mod pi so the centres may not match
# if they don't, check a1 + pi
if abs(x0 - x0s) > PREC or abs(y0 - y0s) > PREC:
a1 = a1 + math.pi
t1 = a1 + a2
t2 = a1 - a2
x0 = x1_loc - a * math.cos(t1)
y0 = y1_loc - b * math.sin(t1)
x0s = x2_loc - a * math.cos(t2)
y0s = y2_loc - b * math.sin(t2)
# get the angles in the range [0, 2 * pi]
if t1 < 0:
t1 += 2 * math.pi
if t2 < 0:
t2 += 2 * math.pi
# increase minimum by 2 * pi for a large arc
if large_arc:
if t1 < t2:
t1 += 2 * math.pi
else:
t2 += 2 * math.pi
return ellipse_part_integral(t1, t2, a, b)
print(ellipse_arc_length(0, 0, 40, 40, 0, False, True, 80, 0))
Run Code Online (Sandbox Code Playgroud)
好消息是,只要您只是寻找弧的长度,扫描标志并不重要。
我不能 100% 确定模 pi 问题是否得到正确处理,并且上面的实现可能存在一些错误。尽管如此,它给了我一个很好的近似半圆简单情况下的长度,所以我敢称它为WIP。让我知道这是否值得追求,当我坐在电脑前时我可以进一步看看。或者也许有人可以同时想出一种干净的方法来做到这一点?