I'd like to try calculating the intersection of two straight lines with a solve.

Asked 2 years ago, Updated 2 years ago, 128 views

When the straight line is parallel to the x-axis or the y-axis, do we need to deal with the case separately?How many cases should I divide it into?
Thank you.
The following error appears in Example 7.
UnboundLocalError: local variable 'a' referred before assignment

(Original post) Check if the two lines intersect.
https://qiita.com/tydesign/items/405f1345a437b03b6403
(Reference) I tried pycharm with reference to "Check if the two lines intersect.">sympy intersection(entity1,entity2)
https://qiita.com/mrrclb48z/items/c1fe2d1f3e7cd12d07cd

from sympathy import*
var('Ax Ay Bx By Cx Cy Dx Dy Kx Ky')
var('abc de f')
var('xy')
def myKoutenLine(lineSeg):
    (myAx, myAy), (myBx, myBy), (myCx, myCy), (myDx, myDy) = lineSeg
    if(myAx!=myBx and myCx!=myDx):
       a = 1.0
       d = 1.0
    an=solve([a*Ax+b*Ay+c,\
                 a*Bx+b*By+c,\
                 d*Cx+e*Cy+f,\
                 d*Dx+e*Dy+f,\
                 a*Kx+b*Ky+c,\
                 d*Kx+e*Ky+f], \
                [Kx, Ky, b, c, e, f])
    myKx=ans[0][0].subs({Ax:myAx, Ay:myAy, Bx:myBx, By:myBy, Cx:myCx, Cy:myCy, Dx:myDx, Dy:myDy})
    myKy=ans[0][1].subs({Ax:myAx, Ay:myAy, Bx:myBx, By:myBy, Cx:myCx, Cy:myCy, Dx:myDx, Dy:myDy})
    return myKx, myKx
# Example 1
lineSeg = ((2, 3), (11, 12), (10, 5), (3, 11))
print('#Example 1', lineSeg, '->', myKoutenLine(lineSeg))
# Example 7 (additional)
lineSeg = ((-10, 0), (10, 0), (0, -10), (0, 10))
print('#Example7(additional), lineSeg, '->', myKoutenLine(lineSeg))

python algorithm sympy

2022-09-29 22:48

1 Answers

[ [Help Center] [Frequently Asked Questions] says:
What kind of questions should I ask?
Any programming questions, of course, are welcome! However, be aware of the following:

  • The content is specific and detailed
  • Explain your questions in an easy-to-understand way
  • The content should be useful to any other programmer in the world


because it was hard to understand what you were going to do. I've just supplemented it

Article: According to Check if the two lines intersect

This,

Use the formula processing library SymPy to see if the two lines intersect in Python (similarly?)

As for (1), I just call the intersection method, so I need to check if there are any coding errors, but I don't think it's necessary to implement the cross-determination method.

In (2), we use straight lines instead of line segments, so whether or not the intersection point is included in the line segment...Doesn't it depend on the implementation?

Qiita I tried to simplify the code for articles
(It doesn't appear in the question, but I will use some of them in the following process.)

 from sympy import Segment

def myKousaHantei(p1,p2,p3,p4):
    seg_p12 = Segment(p1,p2)
    seg_p34 = Segment(p3,p4)
    pt_i = seg_p12.intersection(seg_p34)
    ifpt_i:
        return pt_i[0]# Since it is not a curve, there is at most one intersection.

p1, p2, p3, p4 = ((2, 3), (11, 12), (10, 5), (3, 11))
myKousaHantei (p1, p2, p3, p4)

Run Results

SymPy also has Line in addition to Segment

obtained by passing two specified points or specifying one point and angle
 from sympy import Line, solo, symbols
fromsympy.plotting import plot

p1, p2, p3, p4 = ((2, 3), (11, 12), (10, 5), (3, 11))
ln_p12 = Line (p1, p2)
ln_p34 = Line (p3, p4)

expr_a=ln_p12.equation()
display(expr_a)
expr_b=ln_p34.equation()
display(expr_b)
print(f'intersection coordinates: {solve([expr_a, expr_b])}')

x,y=symbols('xy')
plot(ln_p12.equation(x,y),y)[0],solve(ln_p34.equation(x,y),y)[0])

Run Results & Graphs
Each equation can be obtained from a straight line

  • -9*x+9*y-9=0
  • -6*x-7*y+95=0

The coordinates of the intersection point can be seen as {x:88/13, y:101/13}.

For more information about the error in the question, see

  • Tried to change it as a local variable even though it is a global variable
  • Even though it is defined as Symbol for SymPy, it is substituting numbers

There are problems such as this, and although the direct cause of the error is the former, it is still difficult to understand what you want to do


2022-09-29 22:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.