0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ChatGPT先生へ「sympy で、ポリラインとポリラインの交点計算のユーザー定義関数を教えて下さい。」

Posted at

(本日の)ChatGPT先生へ

sympy で、ポリラインとポリラインの交点計算のユーザー定義関数を教えて下さい。

✅ 完全版:polyline × polyline の交点関数

from sympy import Point, Segment

def polyline_intersections(poly1, poly2):
    """
    poly1, poly2 : list of sympy.geometry.Point
    return       : list of sympy.geometry.Point
    """
    intersections = []

    for i in range(len(poly1) - 1):
        s1 = Segment(poly1[i], poly1[i+1])

        for j in range(len(poly2) - 1):
            s2 = Segment(poly2[j], poly2[j+1])

            ip = s1.intersection(s2)
            if ip:
                for p in ip:
                    if p not in intersections:
                        intersections.append(p)

    return intersections

✅ 使用例

from sympy import Point

poly1 = [
    Point(0, 0),
    Point(4, 4),
    Point(8, 0)
]

poly2 = [
    Point(0, 3),
    Point(8, 3)
]

print(polyline_intersections(poly1, poly2))

✅ 推奨:PolyLine クラスとして定義

from sympy import Point, Segment

class PolyLine(list):
    def intersections_points_only(self, other):
        """
        self, other : PolyLine (Point の列)
        return      : list of Point
        """
        pts = []

        for i in range(len(self) - 1):
            s1 = Segment(self[i], self[i+1])

            for j in range(len(other) - 1):
                s2 = Segment(other[j], other[j+1])

                for obj in s1.intersection(s2):
                    if isinstance(obj, Point) and obj not in pts:
                        pts.append(obj)

        return pts
from sympy import Point

poly1 = PolyLine([
    Point(0, 0),
    Point(4, 4),
    Point(8, 0)
])

poly2 = PolyLine([
    Point(0, 3),
    Point(8, 3)
])

print(poly1.intersections_points_only(poly2))
[Point2D(3, 3), Point2D(5, 3)]

✨ 応用(すぐ追加できます)
intersections_with_segments()
→ どの線分同士かの index も返す
self_intersections()
→ 自己交差検出
FreeCAD Draft.makeWire 直結版
次はどれに行きますか?
(Cremona 図・Draft・SymPy 構造解析とも相性抜群です)

いつもと違うおすすめです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?