From 3912b5d853b2f566681f2d5229082fa0cc9001b8 Mon Sep 17 00:00:00 2001 From: vec2pt <68401718+vec2pt@users.noreply.github.com> Date: Mon, 24 Jun 2024 18:38:58 +0200 Subject: [PATCH] V and H commands for Lines. + relative coordinates #135 --- drawsvg/elements.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/drawsvg/elements.py b/drawsvg/elements.py index be7f994..73ff77d 100644 --- a/drawsvg/elements.py +++ b/drawsvg/elements.py @@ -567,7 +567,7 @@ class Lines(Path): Additional keyword arguments are output as additional arguments to the SVG node e.g. fill="red", stroke="#ff4477", stroke_width=2. ''' - def __init__(self, sx, sy, *points, close=False, **kwargs): + def __init__(self, sx, sy, *points, close=False, relative=False, **kwargs): super().__init__(d='', **kwargs) self.M(sx, sy) if len(points) % 2 != 0: @@ -575,7 +575,16 @@ def __init__(self, sx, sy, *points, close=False, **kwargs): 'expected an even number of positional arguments x0, y0, ' 'x1, y1, ...') for i in range(len(points) // 2): - self.L(points[2*i], points[2*i+1]) + px = points[2*(i-1)] if i > 0 else sx + py = points[2*(i-1)+1] if i > 0 else sy + x = points[2*i] + y = points[2*i+1] + if x == px: + self.v(y - py) if relative else self.V(y) + elif y == py: + self.h(x - px) if relative else self.H(x) + else: + self.l(x - px, y - py) if relative else self.L(x, y) if close: self.Z() @@ -585,8 +594,10 @@ class Line(Lines): Additional keyword arguments are output as additional arguments to the SVG node e.g. fill="red", stroke="#ff4477", stroke_width=2. ''' - def __init__(self, sx, sy, ex, ey, **kwargs): - super().__init__(sx, sy, ex, ey, close=False, **kwargs) + def __init__(self, sx, sy, ex, ey, relative=False, **kwargs): + super().__init__( + sx, sy, ex, ey, close=False, relative=relative, **kwargs + ) class Arc(Path): '''A circular arc.