drawlib 0.2.3.dev4__py3-none-any.whl → 0.2.3.dev5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of drawlib might be problematic. Click here for more details.

drawlib/__init__.py CHANGED
@@ -14,7 +14,7 @@ import sys
14
14
  from typing import Final, List
15
15
 
16
16
  # please update here when you release new version
17
- LIB_VERSION = "0.2.3.dev4"
17
+ LIB_VERSION = "0.2.3.dev5"
18
18
 
19
19
  # please list active main committers (1+ commits per month)
20
20
  AUTHORS: Final[List[str]] = [
@@ -11,7 +11,7 @@
11
11
  """Canvas's original arrow feature implementation module."""
12
12
 
13
13
  import math
14
- from typing import List, Literal, Optional, Tuple, Union
14
+ from typing import Any, List, Literal, Optional, Tuple, Union
15
15
 
16
16
  from matplotlib.patches import PathPatch
17
17
  from matplotlib.path import Path
@@ -219,8 +219,8 @@ class CanvasOriginalArrowFeature(CanvasBase):
219
219
  new_xys.insert(0, xys_start)
220
220
  new_xys.append(xys_end)
221
221
  aph = ArrowPolylineHelper(new_xys, r, 100)
222
- parallel_xys1 = aph.get_parallel_curve_points(tail_width / 2)
223
- parallel_xys2 = aph.get_parallel_curve_points(tail_width / -2)
222
+ parallel_xys1 = aph.get_parallel_points(tail_width / 2)
223
+ parallel_xys2 = aph.get_parallel_points(tail_width / -2)
224
224
  parallel_xys2.reverse()
225
225
 
226
226
  if head in {"<-", "<->"}:
@@ -568,6 +568,14 @@ class ArrowPolylineHelper:
568
568
  curve.append((x, y))
569
569
  return curve
570
570
 
571
+ def remove_consecutive_duplicates(lst: List[Any]) -> List[Any]:
572
+ return [v for i, v in enumerate(lst) if i == 0 or v != lst[i - 1]]
573
+
574
+ self._r = r
575
+ if r == 0:
576
+ self._original_points = xys[:]
577
+ return
578
+
571
579
  points = []
572
580
  bezier_start: Tuple[float, float] = (0, 0)
573
581
  last_i = len(xys) - 2
@@ -592,24 +600,122 @@ class ArrowPolylineHelper:
592
600
  points.extend(get_points(bezier_points))
593
601
  bezier_start = p2
594
602
 
595
- self._original_curve_points = points
603
+ self._original_points = remove_consecutive_duplicates(points)
596
604
 
597
- def get_parallel_curve_points(self, distance: float) -> List[Tuple[float, float]]:
605
+ def get_parallel_points(self, distance: float) -> List[Tuple[float, float]]:
606
+ """Internal function"""
607
+ if self._r == 0:
608
+ return self._get_parallel_straight_points(distance)
609
+ return self._get_parallel_curved_points(distance)
610
+
611
+ def _get_parallel_curved_points(self, distance: float) -> List[Tuple[float, float]]:
598
612
  """Internal function"""
599
613
 
600
614
  def get_distance(p1: Tuple[float, float], p2: Tuple[float, float]) -> float:
601
615
  return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
602
616
 
603
617
  parallel_curve_points = []
604
- for i in range(1, len(self._original_curve_points)):
605
- p0, p1 = self._original_curve_points[i - 1], self._original_curve_points[i]
618
+ for i in range(1, len(self._original_points)):
619
+ p0, p1 = self._original_points[i - 1], self._original_points[i]
606
620
  dx, dy = p1[0] - p0[0], p1[1] - p0[1]
607
621
  length = get_distance(p0, p1)
622
+ print(p0, p1, length)
608
623
  nx, ny = -dy / length, dx / length
609
624
  px0, py0 = p0[0] + distance * nx, p0[1] + distance * ny
610
625
  px1, py1 = p1[0] + distance * nx, p1[1] + distance * ny
611
626
  parallel_curve_points.append((px0, py0))
612
- if i == len(self._original_curve_points) - 1:
627
+ if i == len(self._original_points) - 1:
613
628
  parallel_curve_points.append((px1, py1))
614
629
 
615
630
  return parallel_curve_points
631
+
632
+ def _get_parallel_straight_points(self, distance: float) -> List[Tuple[float, float]]:
633
+ """Internal function"""
634
+
635
+ def get_parallel_line_xys(
636
+ xy1: Tuple[float, float],
637
+ xy2: Tuple[float, float],
638
+ ) -> Tuple[Tuple[float, float], Tuple[float, float]]:
639
+ """Internal function"""
640
+ x1, y1 = xy1
641
+ x2, y2 = xy2
642
+
643
+ # calc vector
644
+ vx = x2 - x1
645
+ vy = y2 - y1
646
+ length = math.sqrt(vx**2 + vy**2)
647
+ dx = -vy / length * distance
648
+ dy = vx / length * distance
649
+
650
+ # add vector
651
+ x1_prime = x1 + dx
652
+ y1_prime = y1 + dy
653
+ x2_prime = x2 + dx
654
+ y2_prime = y2 + dy
655
+
656
+ return (x1_prime, y1_prime), (x2_prime, y2_prime)
657
+
658
+ def find_lines_intersection(
659
+ xy1: Tuple[float, float],
660
+ xy2: Tuple[float, float],
661
+ xy3: Tuple[float, float],
662
+ xy4: Tuple[float, float],
663
+ ) -> Tuple[float, float]:
664
+ """Internal function"""
665
+ x1, y1 = xy1
666
+ x2, y2 = xy2
667
+ x3, y3 = xy3
668
+ x4, y4 = xy4
669
+
670
+ # Check if lines are vertical
671
+ if x1 == x2 and x3 == x4:
672
+ raise ValueError(f'line "{xy1}" -> "{xy2}" and "{xy3}" => "{xy4}" are parallel.')
673
+
674
+ # Handle vertical lines
675
+ if x1 == x2:
676
+ m2 = (y4 - y3) / (x4 - x3)
677
+ b2 = y3 - m2 * x3
678
+ x = x1
679
+ y = m2 * x + b2
680
+ return (x, y)
681
+
682
+ if x3 == x4:
683
+ m1 = (y2 - y1) / (x2 - x1)
684
+ b1 = y1 - m1 * x1
685
+ x = x3
686
+ y = m1 * x + b1
687
+ return (x, y)
688
+
689
+ m1 = (y2 - y1) / (x2 - x1)
690
+ b1 = y1 - m1 * x1
691
+ m2 = (y4 - y3) / (x4 - x3)
692
+ b2 = y3 - m2 * x3
693
+
694
+ if m1 == m2:
695
+ raise ValueError(f'line "{xy1}" -> "{xy2}" and "{xy3}" => "{xy4}" are parallel.')
696
+
697
+ x = (b2 - b1) / (m1 - m2)
698
+ y = m1 * x + b1
699
+
700
+ return (x, y)
701
+
702
+ xys = self._original_points
703
+ parallel_lines: List[Tuple[Tuple[float, float], Tuple[float, float]]] = []
704
+ for i in range(len(xys) - 1):
705
+ xy1, xy2 = get_parallel_line_xys(xys[i], xys[i + 1])
706
+ parallel_lines.append((xy1, xy2))
707
+
708
+ points: List[Tuple[float, float]] = []
709
+ for i in range(len(parallel_lines) - 1):
710
+ xy1, xy2 = parallel_lines[i]
711
+ xy3, xy4 = parallel_lines[i + 1]
712
+ xy = find_lines_intersection(xy1, xy2, xy3, xy4)
713
+ points.append(xy)
714
+
715
+ first_point = parallel_lines[0][0]
716
+ last_point = parallel_lines[len(parallel_lines) - 1][1]
717
+
718
+ points.insert(0, first_point)
719
+ points.append(last_point)
720
+
721
+ return points
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: drawlib
3
- Version: 0.2.3.dev4
3
+ Version: 0.2.3.dev5
4
4
  Summary: Python drawing library. Illustration as Code.
5
5
  Home-page: https://www.drawlib.com
6
6
  Author: Yuichi Ito
@@ -1,4 +1,4 @@
1
- drawlib/__init__.py,sha256=jXsDFzb9eWZYUjfkP43y1J-umimTxB5iXTEx201e0Ow,2678
1
+ drawlib/__init__.py,sha256=yuaqeWXsVZ_zJ5lJedaZVNNb5A7sKIjkkQeZm_vLhak,2678
2
2
  drawlib/__main__.py,sha256=UDsH6DiEIWBEwlClbYI10aWYBw_gOmaNCB0TSImWZ-c,604
3
3
  drawlib/apis.py,sha256=Q1Uz4U6IK63ZtjaV_UTWPZq7xDpxTz99qHfpGsL8SnI,531
4
4
  drawlib/assets/__init__.py,sha256=bYT4EWquLr0oyEziAuixlD9M4EPnMY6PyhknOxJjxqY,437
@@ -26,7 +26,7 @@ drawlib/v0_2/private/core_canvas/base.py,sha256=FipdPyX9M-JWL9FQpAT1r8hoo9o1wyCS
26
26
  drawlib/v0_2/private/core_canvas/canvas.py,sha256=haDITKf-Wz2zA2zDC_NmrdH-QsSagukQ7LT7xEB2crY,9594
27
27
  drawlib/v0_2/private/core_canvas/image.py,sha256=1KoLmCTbr4VoTZi0rI_e1_HIYOmpGklygeq5ACgwVus,6958
28
28
  drawlib/v0_2/private/core_canvas/line.py,sha256=qp5IcQUTfLyH1hi4JOKa-h_8TVWRWCEthfSSEEErMgQ,17862
29
- drawlib/v0_2/private/core_canvas/original_arrow.py,sha256=vTnL7krRxEtplF4CGF5pYnwI0T_YtuNaQ_5bI3zmvCU,21231
29
+ drawlib/v0_2/private/core_canvas/original_arrow.py,sha256=rJJhoBf1Z2nWt1Ny6WTRYhoBLQHU5xzuXPVxLjyyuy8,24632
30
30
  drawlib/v0_2/private/core_canvas/original_polygon.py,sha256=uvBbfr72J3QEw9efxHdd5YXl1IKZhEA82qzEXdEK7Kc,14546
31
31
  drawlib/v0_2/private/core_canvas/patches.py,sha256=uDRZMYqMnkh37lXVLGwTUmsb7XuNjNMNZBjP3Cgi5Eg,14211
32
32
  drawlib/v0_2/private/core_canvas/text.py,sha256=bqdiqptdXeV8HVjp0Fx2pl2eA0q29XHYtWcK4WSyCw8,3520
@@ -75,8 +75,8 @@ drawlib/v0_2/private/validators/smartarts.py,sha256=fOAlC0syvZVk-GCLQgaPhhfWr3gc
75
75
  drawlib/v0_2/private/validators/style.py,sha256=R7aLlaVfzdo33L7GLQ3WVfzzD_EsgV2-Y7ERcbFq7QI,3308
76
76
  drawlib/v0_2/private/validators/text.py,sha256=ELT87nekr9hMx7m-wpVTku0QPxL3qCIX4cxI5RbFoIc,1126
77
77
  drawlib/v0_2/private/validators/types.py,sha256=KtiIgivuaFyFaxIGeqr5N8xpzqIk9soiacSzcFDdjSA,4155
78
- drawlib-0.2.3.dev4.dist-info/LICENSE.txt,sha256=h_ZiliCgEDqIYpxmUPmqGGP87VBcUHil1S4wUI48ZKg,11344
79
- drawlib-0.2.3.dev4.dist-info/METADATA,sha256=keuYahbx9v68956lsqVpKMBmY3l0G6rHq28VVvphFkc,4007
80
- drawlib-0.2.3.dev4.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
81
- drawlib-0.2.3.dev4.dist-info/entry_points.txt,sha256=Lp6mbrRMP-g6c7P6Iz21CEjceLITCWrma3b0ncP30oQ,49
82
- drawlib-0.2.3.dev4.dist-info/RECORD,,
78
+ drawlib-0.2.3.dev5.dist-info/LICENSE.txt,sha256=h_ZiliCgEDqIYpxmUPmqGGP87VBcUHil1S4wUI48ZKg,11344
79
+ drawlib-0.2.3.dev5.dist-info/METADATA,sha256=ICI9-Seg7xPDVr4pIG0xdrSjd9iQea3YEfNOjDe-9do,4007
80
+ drawlib-0.2.3.dev5.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
81
+ drawlib-0.2.3.dev5.dist-info/entry_points.txt,sha256=Lp6mbrRMP-g6c7P6Iz21CEjceLITCWrma3b0ncP30oQ,49
82
+ drawlib-0.2.3.dev5.dist-info/RECORD,,