-1

So I have a problem. I write a programm to figure out how many triangles are in a given set of lines and I want to calculate that with the line intersections.

I have a problem with line intersections. My problems are to figure out if the intersection is in the line segments. Here is my code:

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 are the are the beginning/ endpoints of the first line segment

x21,x22,y21,y22 are the are the beginning/ endpoints of the second line segment

xs,ys are the coordinates of the intersection

it always gives out 'uncool' shouldn't be right

For testing purposes i chose

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

with these coordinates xs and ys are 4,33 and 4,66

thanks in advance

Banana
  • 21
  • 1
  • 4
  • 1
    I just tried your given input with `xs=4.33` and `ys=4.66`, which I guess is what you meant. It worked fine. Please post a [mcve]. – khelwood Oct 31 '17 at 10:49
  • Your logic are right here (I get 'cool' too). But if you use `retutn` I suppose you use function. So your mistake is because of incorrect function use (maybe you need to check the scope of variables) – Mikhail_Sam Oct 31 '17 at 10:54

1 Answers1

0

It could be due to the way in which you are referencing the global constants. (it's hard to know without your complete function.) Global constants must be declared with the global keyword in your function definition if you wish to reference them.

The following function will return the string 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)
S. Whittaker
  • 118
  • 9