Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arc transform fixed #98

Merged
merged 5 commits into from
Jun 23, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions svgpathtools/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ def scale_bezier(bez):
raise TypeError("Input `curve` should be a Path, Line, "
"QuadraticBezier, CubicBezier, or Arc object.")


def transform(curve, tf):
"""Transforms the curve by the homogeneous transformation matrix tf"""
def to_point(p):
Expand All @@ -274,9 +273,28 @@ def to_complex(v):
elif isinstance(curve, Arc):
new_start = to_complex(tf.dot(to_point(curve.start)))
new_end = to_complex(tf.dot(to_point(curve.end)))
new_radius = to_complex(tf.dot(to_vector(curve.radius)))
return Arc(new_start, radius=new_radius, rotation=curve.rotation,
large_arc=curve.large_arc, sweep=curve.sweep, end=new_end)

# Based on https://math.stackexchange.com/questions/2349726/compute-the-major-and-minor-axis-of-an-ellipse-after-linearly-transforming-it
rx2 = curve.radius.real ** 2
ry2 = curve.radius.imag ** 2

Q = np.array([[1/rx2, 0], [0, 1/ry2]])
invT = np.linalg.inv(tf[:2,:2])
D = invT.T @ Q @ invT

eigvals = np.linalg.eigvals(D)

rx = 1 / np.sqrt(eigvals[0])
ry = 1 / np.sqrt(eigvals[1])

new_radius = complex(rx, ry)

if new_radius.real == 0 or new_radius.imag == 0 :
return Line(new_start, new_end)
else :
return Arc(new_start, radius=new_radius, rotation=curve.rotation,
large_arc=curve.large_arc, sweep=curve.sweep, end=new_end)
Vrroom marked this conversation as resolved.
Show resolved Hide resolved

else:
raise TypeError("Input `curve` should be a Path, Line, "
"QuadraticBezier, CubicBezier, or Arc object.")
Expand Down