Len*_*bro 8 python math svg geometric-arc
我正在尝试在Python中实现SVG路径计算,但我遇到了Arc曲线的问题.
我认为问题在于从终点到中心参数化的转换,但我找不到问题.您可以在SVG规范的 F6.5节中找到有关如何实现它的说明.我也看过其他语言的实现,我也看不出它们有什么不同.
我的Arc对象实现在这里:
class Arc(object):
def __init__(self, start, radius, rotation, arc, sweep, end):
"""radius is complex, rotation is in degrees,
large and sweep are 1 or 0 (True/False also work)"""
self.start = start
self.radius = radius
self.rotation = rotation
self.arc = bool(arc)
self.sweep = bool(sweep)
self.end = end
self._parameterize()
def _parameterize(self):
# Conversion from endpoint to center parameterization
# http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
cosr = cos(radians(self.rotation))
sinr = sin(radians(self.rotation))
dx = (self.start.real - self.end.real) / 2
dy = (self.start.imag - self.end.imag) / 2
x1prim = cosr * dx + sinr * dy
x1prim_sq = x1prim * x1prim
y1prim = -sinr * dx + cosr * dy
y1prim_sq = y1prim * y1prim
rx = self.radius.real
rx_sq = rx * rx
ry = self.radius.imag
ry_sq = ry * ry
# Correct out of range radii
radius_check = (x1prim_sq / rx_sq) + (y1prim_sq / ry_sq)
if radius_check > 1:
rx *= sqrt(radius_check)
ry *= sqrt(radius_check)
rx_sq = rx * rx
ry_sq = ry * ry
t1 = rx_sq * y1prim_sq
t2 = ry_sq * x1prim_sq
c = sqrt((rx_sq * ry_sq - t1 - t2) / (t1 + t2))
if self.arc == self.sweep:
c = -c
cxprim = c * rx * y1prim / ry
cyprim = -c * ry * x1prim / rx
self.center = complex((cosr * cxprim - sinr * cyprim) +
((self.start.real + self.end.real) / 2),
(sinr * cxprim + cosr * cyprim) +
((self.start.imag + self.end.imag) / 2))
ux = (x1prim - cxprim) / rx
uy = (y1prim - cyprim) / ry
vx = (-x1prim - cxprim) / rx
vy = (-y1prim - cyprim) / ry
n = sqrt(ux * ux + uy * uy)
p = ux
theta = degrees(acos(p / n))
if uy > 0:
theta = -theta
self.theta = theta % 360
n = sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy))
p = ux * vx + uy * vy
if p == 0:
delta = degrees(acos(0))
else:
delta = degrees(acos(p / n))
if (ux * vy - uy * vx) < 0:
delta = -delta
self.delta = delta % 360
if not self.sweep:
self.delta -= 360
def point(self, pos):
if self.arc == self.sweep:
angle = radians(self.theta - (self.delta * pos))
else:
angle = radians(self.delta + (self.delta * pos))
x = sin(angle) * self.radius.real + self.center.real
y = cos(angle) * self.radius.imag + self.center.imag
return complex(x, y)
Run Code Online (Sandbox Code Playgroud)
您可以使用以下代码对此进行测试,该代码将使用Turtle模块绘制曲线.(最后的raw_input()就是当程序退出时屏幕不会消失).
arc1 = Arc(0j, 100+50j, 0, 0, 0, 100+50j)
arc2 = Arc(0j, 100+50j, 0, 1, 0, 100+50j)
arc3 = Arc(0j, 100+50j, 0, 0, 1, 100+50j)
arc4 = Arc(0j, 100+50j, 0, 1, 1, 100+50j)
import turtle
t = turtle.Turtle()
t.penup()
t.goto(0, 0)
t.dot(5, 'red')
t.write('Start')
t.goto(100, 50)
t.dot(5, 'red')
t.write('End')
t.pencolor = t.color('blue')
for arc in (arc1, arc2, arc3, arc4):
t.penup()
p = arc.point(0)
t.goto(p.real, p.imag)
t.pendown()
for x in range(1,101):
p = arc.point(x*0.01)
t.goto(p.real, p.imag)
raw_input()
Run Code Online (Sandbox Code Playgroud)
问题:
绘制的这四个弧中的每一个都应从起点绘制到终点.但是,它们来自错误的观点.两条曲线从开始到开始,两条曲线从100,-50到0,0而不是从0到100,50.
部分问题在于实现说明为您提供了如何从端点到中心进行转换的公式,但没有解释它在几何上的作用,所以我不清楚每个步骤的作用.对此的解释也会有所帮助.
我想我在您的代码中发现了一些错误:
theta = degrees(acos(p / n))
if uy > 0:
theta = -theta
self.theta = theta % 360
Run Code Online (Sandbox Code Playgroud)
条件uy > 0
错误,正确的是(如果到 的uy < 0
方向角为负):(1, 0)
(ux, uy)
uy < 0
theta = degrees(acos(p / n))
if uy < 0:
theta = -theta
self.theta = theta % 360
Run Code Online (Sandbox Code Playgroud)
然后
if self.arc == self.sweep:
angle = radians(self.theta - (self.delta * pos))
else:
angle = radians(self.delta + (self.delta * pos))
Run Code Online (Sandbox Code Playgroud)
这里没有必要进行区分,sweep
和参数已在和arc
中考虑。这可以简化为:theta
delta
angle = radians(self.theta + (self.delta * pos))
Run Code Online (Sandbox Code Playgroud)
最后
x = sin(angle) * self.radius.real + self.center.real
y = cos(angle) * self.radius.imag + self.center.imag
Run Code Online (Sandbox Code Playgroud)
这里sin
和cos
混淆了,正确的是
x = cos(angle) * self.radius.real + self.center.real
y = sin(angle) * self.radius.imag + self.center.imag
Run Code Online (Sandbox Code Playgroud)
经过这些修改后,程序按预期运行。
编辑:还有一个问题。该point
方法不考虑可能的rotation
参数。以下版本应该是正确的:
def point(self, pos):
angle = radians(self.theta + (self.delta * pos))
cosr = cos(radians(self.rotation))
sinr = sin(radians(self.rotation))
x = cosr * cos(angle) * self.radius.real - sinr * sin(angle) * self.radius.imag + self.center.real
y = sinr * cos(angle) * self.radius.real + cosr * sin(angle) * self.radius.imag + self.center.imag
return complex(x, y)
Run Code Online (Sandbox Code Playgroud)
(参见SVG 规范中的公式 F.6.3.1 。)