在python中查找给定的直线(方程)是否能够成功地分离两个点列表

uidvcgyl  于 2023-03-20  发布在  Python
关注(0)|答案(9)|浏览(376)

我遇到了一个问题,明确要求我不要使用numpy或panda
问题:
给定两组数据点的元组列表形式,如

Red =[(R11,R12),(R21,R22),(R31,R32),(R41,R42),(R51,R52),..,(Rn1,Rn2)]
Blue=[(B11,B12),(B21,B22),(B31,B32),(B41,B42),(B51,B52),..,(Bm1,Bm2)]

和直线方程组(字符串格式,即字符串列表)

Lines = [a1x+b1y+c1,a2x+b2y+c2,a3x+b3y+c3,a4x+b4y+c4,..,K lines]

注意:你需要在这里做字符串解析,得到x,y和截距的系数。
你在这里的任务是为每一行打印“YES”/“NO”。如果所有的红点都在该行的一边,蓝点在该行的另一边,你应该打印YES,否则你应该打印NO。

Ex:

Red= [(1,1),(2,1),(4,2),(2,4), (-1,4)]
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["1x+1y+0","1x-1y+0","1x+0y-3","0x+1y-0.5"]

Output:
YES
NO
NO
YES

从数学上讲,你用直线方程S

Suppose S(x)(y) = 1x+1y+0

Now take points (1,1) and (-6,-1)

S(1)(1) = 1(1)+ 1(1) = 2 >0

S(-6)(-1) = 1(-6)+(1)(-1) = -7 <0

因此,我们可以得出结论,(1,1)和(-6,-1)位于线S的不同侧。
在这个问题中,给定一个方程S,所有的红色应该在方程的一边,蓝色在另一边。
这里的问题是,我不知道如何用python替换以字符串形式给出的方程中列表中的点的值。
此外,我没有拿出一个逻辑(如何使用循环根据我们的要求)的代码来解决上述问题。
希望你能对这个问题有深刻的见解。先谢了。

mutmk8jj

mutmk8jj1#

使用your_string.replace()和eval()函数,将x和y字符替换为它的值,eval()将字符串作为等式执行

for element in red:
    equation=line.replace('x','*'+str(element[0]))
    equation=equation.replace('y','*'+str(element [1]))
    result=eval(equation)
    if result > 0:
        pass
    else:
        return "NO"
#code for blue
return "Yes"
drkbr07n

drkbr07n2#

假设字符串的形式总是

ax+by+c

(in那个订单),你可以写

import re

for line in Lines:
    a, b, c = [float(coef.strip()) for coef in re.split('x|y', line)]
    ...
9njqaruj

9njqaruj3#

Python3程序检查两个点是否位于同一侧

def pointsAreOnSameSideOfLine(a, b, c, x1, y1, x2, y2): 
fx1 = 0 # Variable to store a * x1 + b * y1 - c 
fx2 = 0 # Variable to store a * x2 + b * y2 - c 
fx1 = a * x1 + b * y1 - c 
fx2 = a * x2 + b * y2 - c 

# If fx1 and fx2 have same sign 
if ((fx1 * fx2) > 0): 
    return True
return False

驱动程序代码

a, b, c = 1, 1, 1
x1, y1 = 1, 1
x2, y2 = 2, 1
if (pointsAreOnSameSideOfLine(a, b, c, x1, y1, x2, y2)): 
    print("Yes") 
else: 
    print("No")
lokaqttq

lokaqttq4#

@赫曼特·拉瓦瓦拉普,关于分裂(x|y)将基于x或y分裂线方程,即,如果你给予空间,那么它将基于空间分裂,同样,在这里,它也在找到x或y的地方分裂。

5lhxktic

5lhxktic5#

e1 = []
e2 = []
def points():
    for k in Lines:
        a=k[0];b=k[2]+k[3];c=k[5]+k[6]
        n = [];m=[]
        for i in Red:
            x1=i[0];y1=i[1]
            eq1 = int(a) * int(x1) + int(b) * int(y1)-int(c)
            n.append(eq1)
        e1.append(n)
        for j in Blue:
            x2=j[0];y2=j[1]
            eq2 = int(a) * int(x2) + int(b) * int(y2)-int(c)
            m.append(eq2)
        e2.append(m)
    print(e1)
    print('----------------------------------------------------------------------')
    print(e2)
    print('----------------------------------------------------------------------')
    p=[]
    for i,j in zip(e1,e2):
        q = []
        for k,l in zip(i,j):
            x=k*l
            if x<0:
                q.append(x)
        p.append(q)
    print(p)
    for i in p:
        if len(i)==5:
            print('yes')
        else:
            print('No')

Red= [(1,1),(2,1),(4,2),(2,4),(-1,4)]
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["1x+1y+0","1x-1y+0","1x+0y-3","0x+1y-0.5"]
points()
fsi0uk1n

fsi0uk1n6#

对于字符串解析:

def getCor(s):

    first = s.split('x')
    a = float(first[0])
    
    second = first[1].split('y')
    b = float(second[0])
    c = float(second[1])
    
    return (a,b,c)

getCor('24x+1y-0.5')

输出:

(24.0, 1.0, -0.5)
ljo96ir5

ljo96ir57#

import math

def getCor(s):
 
    first = s.split('x')
    a = float(first[0])
    
    second = first[1].split('y')
    b = float(second[0])
    c = float(second[1])
    
    return (a,b,c)

def getSide(t,p):
    
    cal = (t[0] * p[0]) + (t[1] * p[1]) + (t[2])

    if cal > 0:
        return 1
    elif cal < 0:
        return -1
    elif cal == 0:
        return 0
    return -2

def getAns(red,blue,line):
    
    sign_red = getSide(getCor(line),red[0])
    sign_blue = getSide(getCor(line),blue[0])
        
    for j in range(len(red)):
        if sign_red != getSide(getCor(line),red[j]):
            return 'no'

    for j in range(len(blue)):

        if sign_blue != getSide(getCor(line),blue[j]):
            return 'no'
    return 'Yes'

Red= [(1,1),(2,1),(4,2),(2,4), (-1,4)]   
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["1x+1y+0","1x-1y+0","1x+0y-3","0x+1y-0.5"]

for i in Lines:
    ans = getAns(Red, Blue, i)
    print(ans)

输出:

Yes
no
no
Yes
eufgjt7s

eufgjt7s8#

import math
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input strings

# you can free to change all these codes/structure
def i_am_the_one(red,blue,line):
    # your code
    for i in red:
        eq=line.replace('x','*'+str(i[0]))
        eq=eq.replace('y','*'+str(i[1]))
        answer=eval(eq)
        if answer>0:
            pass
        else:
            return "NO"
        
    # Code for Blue
    for j in blue:
        eq1=line.replace('x','*'+str(j[0]))
        eq1=eq1.replace('y','*'+str(j[1]))
        answer1=eval(eq1)
        if answer1<0:
            pass
        else:
            return "NO"
    return "Yes"

Red= [(1,1),(2,1),(4,2),(2,4), (-1,4)]
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["1x+1y+0","1x-1y+0","1x+0y-3","0x+1y-0.5"]

for i in Lines:
    yes_or_no = i_am_the_one(Red, Blue, i)
    print(yes_or_no)
6tqwzwtp

6tqwzwtp9#

import math
# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input strings

def get_Coefficents(eqn):
  xCoeff = eqn.split('x')
  a = 1.0 if xCoeff[0] =="" else -1.0 if xCoeff[0] =="-" else float(xCoeff[0])

  yCoeff = xCoeff[1].split('y')
  b = 1.0 if yCoeff[0] =="+" else -1.0 if yCoeff[0] =="-" else float(yCoeff[0]) 

  c = float(yCoeff[1])
  return(a,b,c)

def get_Side(coeff,point):
    
    cal = (coeff[0] * point[0]) + (coeff[1] * point[1]) + (coeff[2])

    if cal > 0:
        return 1
    elif cal < 0:
        return -1
    elif cal == 0:
        return 0
    return -2

# you can free to change all these codes/structure
def i_am_the_one(red,blue,line):
    # your code
    coefficents = get_Coefficents(line)
    is_red = get_Side(coefficents,red[0])
    is_blue = get_Side(coefficents,blue[0])

    for point in range(len(red)):
      if is_red != get_Side(get_Coefficents(line),red[point]):
            return 'no'
    for point in range(len(blue)):
      if is_blue != get_Side(get_Coefficents(line),blue[point]):
            return 'no'
    return 'yes'

Red= [(1,1),(2,1),(4,2),(2,4), (-1,4)]
Blue= [(-2,-1),(-1,-2),(-3,-2),(-3,-1),(1,-3)]
Lines=["x+y+0","1x-1y+0","1x+0y-3","0x+1y-0.5"]

for i in Lines:
    yes_or_no = i_am_the_one(Red, Blue, i)
    print(yes_or_no) # the returned value

相关问题