mathandy / svgpathtools

A collection of tools for manipulating and analyzing SVG Path objects and Bezier curves.
MIT License
548 stars 138 forks source link

Converting from CubicBezier to Poly1D doesnt work correctly #193

Open Fischchen opened 1 year ago

Fischchen commented 1 year ago

Hey everyone!

I'm currently having a problem with this libary where CubicBezier curves when turned into poly1d sometimes end up being linear or end in other garbage.

This is the wanted path:

image

And this is what I get:

image

As you can see, for some reason it converts all curves into diagonals or some weird mix. I'd appreciate any help or pointers in the right direction. This is my Code:

import turtle

import numpy as np
from svgpathtools import svg2paths, Line, CubicBezier

from pprint import pprint
from PIL import Image, ImageDraw

s = turtle.getscreen()
t = turtle.Turtle()
t.speed(0)

paths, attributes = svg2paths("Logo - Kopie.svg")  # type: ignore

scale_factor = -0.25

t.penup()
for path in paths:
    for curve in path:
        pprint(curve)
        if isinstance(curve, Line):
            t.goto(-curve.start.real * scale_factor, curve.start.imag * scale_factor)
            t.pendown()
            t.goto(-curve.end.real * scale_factor, curve.end.imag * scale_factor)
            t.penup()

        elif isinstance(curve, CubicBezier):
            #! Something is wrong here
            poly_curve = curve.poly()
            poly_curve = np.poly1d(poly_curve)
            print(poly_curve)
            x_values = np.linspace(curve.start.real, curve.end.real, 250)
            y_values = np.linspace(curve.start.imag, curve.end.imag, 250)

            t.goto(-curve.start.real * scale_factor, curve.start.imag * scale_factor)
            t.pendown()
            for x, y in zip(x_values, y_values):

                t.goto(-x * scale_factor, y * scale_factor)
            t.penup()

input("Press Enter to continue...")
print("Done")

Best regards, Fischchen