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

Bugfix for rotated ellipse #221

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion svgpathtools/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def to_complex(v):
new_radius = complex(rx, ry)

xeigvec = eigvecs[:, 0]
rot = np.degrees(np.arccos(xeigvec[0]))
rot = np.degrees(np.arctan2(xeigvec[1], xeigvec[0]))

if new_radius.real == 0 or new_radius.imag == 0 :
return Line(new_start, new_end)
Expand Down
36 changes: 36 additions & 0 deletions test/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,42 @@ def test_group_transform(self):
self.assertEqual(path[8], Line(start=(90+0j), end=(10+0j)))
self.assertEqual(path[9], Arc(start=(10+0j), radius=(10+10j), rotation=0.0, large_arc=False, sweep=True, end=-10j))

def test_ellipse_transform(self):
# Check that ellipse to path conversion respects rotation transforms

cx, cy = 40, 80
rx, ry = 15, 20

def dist_to_ellipse(angle, pt):
rot = np.exp(-1j * np.radians(angle))
transformed_pt = rot * complex(pt.real - cx, pt.imag - cy)
return transformed_pt.real**2 / rx**2 + transformed_pt.imag**2 / ry**2 - 1

for angle in np.linspace(-179, 180, num=123):
svgstring = "\n".join(
[
'<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"',
' style="fill:green;stroke:black;stroke-width:1.5">',
f' <ellipse cx="{cx}" cy="{cy}" rx="{rx}" ry="{ry}" transform="rotate({angle} {cx} {cy})"/>',
"</svg>",
]
)

doc = Document.from_svg_string(svgstring)

for p in doc.paths():
subtended_angle = 0.0
for s in p:
if isinstance(s, Arc):
# check that several points lie on the original ellipse
for t in [0.0, 1 / 3.0, 0.5, 2 / 3.0, 1.0]:
dist = dist_to_ellipse(angle, s.point(t))
self.assertAlmostEqual(dist, 0)

# and that the subtended angles sum to 2*pi
subtended_angle = subtended_angle + s.delta
self.assertAlmostEqual(np.abs(subtended_angle), 360)

def test_group_flatten(self):
# Test the Document.paths() function against the
# groups.svg test file.
Expand Down