如果你想用你的双手和标准库来计算这个,你可以基于following formula来计算。由于acos,这仅对椭圆上半部分的两个点有效,但我们将直接将其与角度一起使用。
计算包括以下步骤:
- 从 SVG 数据开始:起点、a 、b 旋转、长弧、扫掠、终点
- 旋转坐标系以匹配椭圆的水平轴。
- 求解具有 4 个未知数的 4 个方程组,得到中心点以及与起点和终点相对应的角度
- 通过小段上的离散和来近似积分。按照 cmets 中的建议,您可以在此处使用
scipy.special.ellipeinc。
第 2 步很简单,只需使用旋转矩阵(注意角度rot 顺时针方向为正):
m = [
[math.cos(rot), math.sin(rot)],
[-math.sin(rot), math.cos(rot)]
]
第 3 步在this answer 中有很好的解释。请注意,为a1 获得的值是模 pi,因为它是通过atan 获得的。这意味着您需要计算 t1 和 t2 两个角度的中心点并检查它们是否匹配。如果没有,请将 pi 添加到 a1 并再次检查。
第 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))
好消息是扫描标志并不重要,只要您只是在寻找弧的长度。
我不能 100% 确定模 pi 问题得到正确处理,并且上面的实现可能存在一些错误。
尽管如此,在半圆的简单情况下,它给了我一个很好的近似长度,所以我敢称它为 WIP。让我知道这是否值得追求,当我坐在电脑前时,我可以进一步看看。或者也许有人可以同时想出一个干净的方法来做到这一点?