python:大于

时间:2017-10-31 10:40:42

标签: python coordinates

所以我有一个问题。 我编写了一个程序来计算给定的一组线中有多少个三角形,我想用线交点来计算。

我遇到了线路交叉点的问题。我的问题是弄清楚交叉点是否在线段中。这是我的代码:

def Schnittkorrekt (xs,ys,x1,x2,y1,y2):
    global y
    global x
    global z
    global w
    global c
    global x3
    global y3
    global z2
    global w2
    global x21
    global x22
    global y21
    global y22
    print (Schnittpunkt(x1,x2,y1,y2,x21,x22,y21,y22))
    if (x1 <= xs) and (y1 <= ys) and (xs <= x2) and (ys <= y2):
        return ('cool')
    elif (x1 <= xs) and (y2 <= ys) and (xs <= x2) and (ys <= y1):
        return ('cool')
    elif (x2 <= xs) and (y1 <= ys) and (xs <= x1) and (ys <= y2):
        return ('cool')
    elif (x2 <= xs) and (y2 <= ys) and (xs <= x1) and (ys <= y1):
        return ('cool')
    else:
        return ('uncool')

x1,x2,y1,y2是第一个线段的起点/终点

x21,x22,y21,y22是第二个线段的起点/终点

xs,ys是交点的坐标

它总是给出'uncool'不应该是正确的

用于测试目的 我选择了

x1=4
x2=5
y1=4
y2=6
x21 = 8
x22 = 1
y21 = 1
y22 = 8

这些坐标xs和ys分别为4,33和4,66

提前致谢

1 个答案:

答案 0 :(得分:0)

这可能是由于您引用全局常量的方式。 (如果没有完整的功能,很难知道。)必须使用function definition if you wish to reference them中的global关键字声明全局常量。

以下函数将返回字符串cool

def intersections(xs, ys):
    # global variable declarations
    global x1
    global x2
    global y1
    global y2
    global x21
    global x22
    global y21
    global y22
    str_test = ''
    if (x1 <= xs) and (y1 <= ys) and (xs <= x2) and (ys <= y2):
        str_test = 'cool'
    elif (x1 <= xs) and (y2 <= ys) and (xs <= x2) and (ys <= y1):
        str_test = 'cool'
    elif (x2 <= xs) and (y1 <= ys) and (xs <= x1) and (ys <= y2):
        str_test = 'cool'
    elif (x2 <= xs) and (y2 <= ys) and (xs <= x1) and (ys <= y1):
        str_test = 'cool'
    else:
        str_test = 'uncool'
    return str_test

print intersections(4.33, 4.66)