LoginSignup
0
0

More than 1 year has passed since last update.

「2本の線分が交差しているか調べる」を参考にPythonのsympyでやってみた。

Last updated at Posted at 2021-06-28

(オリジナルポスト)

sympyを使って(intersection)

交点が線分の間にあれば、交差です。

from sympy import *
def myKyoriHantei(ax,ay,bx,by,cx,cy,dx,dy,kx,ky):
    ans = "Nil"
    Lab=sqrt((ax-bx)**2+(ay-by)**2)
    Lcd=sqrt((cx-dx)**2+(cy-dy)**2)
    if (sqrt((kx - ax) ** 2 + (ky - ay) ** 2) <= Lab and \
        sqrt((kx - bx) ** 2 + (ky - by) ** 2) <= Lab and \
        sqrt((kx - cx) ** 2 + (ky - cy) ** 2) <= Lcd and \
        sqrt((kx - dx) ** 2 + (ky - dy) ** 2) <= Lcd
        ):
     ans="T"
    return ans
def myKousaHantei(lineSeg):
    (myAx, myAy), (myBx, myBy), (myCx, myCy), (myDx, myDy) = lineSeg
    PA, PB = Point(myAx, myAy), Point(myBx, myBy)
    SAB = Segment(PA, PB)
    PC, PD = Point(myCx, myCy), Point(myDx, myDy)
    SCD = Segment(PC, PD)
    ans=SAB.intersection(SCD)
    if(ans==[]):
        return "Nil"
    myKx=SAB.intersection(SCD)[0][0]
    myKy=SAB.intersection(SCD)[0][1]
    return myKyoriHantei(myAx, myAy, myBx, myBy, myCx, myCy, myDx, myDy, myKx, myKy)
# 例1
lineSeg=((2,3),(11,12),(10, 5),(3,11))
print('# 例1 (lineSeg:IsIntersect) ->',lineSeg,'->',myKousaHantei(lineSeg))
# 例2
lineSeg=((2,3),( 5,12),(10, 5),( 8,11))
print('# 例2 (lineSeg:IsIntersect) ->',lineSeg,'->',myKousaHantei(lineSeg))
# 例3
lineSeg=((2,3),( 5,12),(10, 5),( 6, 8))
print('# 例3 (lineSeg:IsIntersect) ->',lineSeg,'->',myKousaHantei(lineSeg))
# 例4
lineSeg=((2,3),(11,12),(10, 5),( 8, 9))
print('# 例4 (lineSeg:IsIntersect) ->',lineSeg,'->',myKousaHantei(lineSeg))
# 例5
lineSeg=((2,3),( 4, 9),(10, 5),( 5,12))
print('# 例5 (lineSeg:IsIntersect) ->',lineSeg,'->',myKousaHantei(lineSeg))
# 例6
lineSeg=((2,3),( 7,12),(10, 5),( 7,12))
print('# 例6 (lineSeg:IsIntersect) ->',lineSeg,'->',myKousaHantei(lineSeg))
# 例1 (lineSeg:IsIntersect) -> ((2, 3), (11, 12), (10, 5), (3, 11)) -> T
# 例2 (lineSeg:IsIntersect) -> ((2, 3), (5, 12), (10, 5), (8, 11)) -> Nil
# 例3 (lineSeg:IsIntersect) -> ((2, 3), (5, 12), (10, 5), (6, 8)) -> Nil
# 例4 (lineSeg:IsIntersect) -> ((2, 3), (11, 12), (10, 5), (8, 9)) -> T
# 例5 (lineSeg:IsIntersect) -> ((2, 3), (4, 9), (10, 5), (5, 12)) -> Nil
# 例6 (lineSeg:IsIntersect) -> ((2, 3), (7, 12), (10, 5), (7, 12)) -> T

sympyを使って(solve)

質問中

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