1

I'm training with a website which ask me to make a program which will ask for the coords of two rectangles and check if the rectangles intersect. Then, when I send the program, the website tests it a few times. It asks for the x min, x max, y min and y max of A rectangle and then, B rectangle.

Here's what I do:

xmin_a = int(input())
xmax_a = int(input())
ymin_a = int(input())
ymax_a = int(input())

xmin_b = int(input())
xmax_b = int(input())
ymin_b = int(input())
ymax_b = int(input())

if xmin_a < xmax_b <= xmax_a and (ymin_a < ymax_b <= ymax_a or ymin_a <= ymin_b < ymax_a):
   print('YES')
elif xmin_a <= xmin_b < xmax_a and (ymin_a < ymax_b <= ymax_a or ymin_a <= ymin_b < ymax_a):
   print('YES')
elif xmin_b < xmax_a <= xmax_b and (ymin_b < ymax_a <= ymax_b or ymin_b <= ymin_a < ymax_b):
   print('YES')
elif xmin_b <= xmin_a < xmax_b and (ymin_b < ymax_a <= ymax_b or ymin_b <= ymin_a < ymax_b):
   print('YES')
else:
   print('NO')

Unfortunately, it doesn't work and I don't get why. Any idea?

PS: http://data.france-ioi.org/Task/7b0ee4fb57949c3db1f694afcef9d1a1//exemple.png

  • Is this even legal `xmin_a < xmax_b <= xmax_a`? Did you even check whether the statements you write work like you think they do? – luk32 Feb 26 '14 at 14:17
  • Yeah it works fine. In fact, the program works sometimes and sometimes not. – user3356177 Feb 26 '14 at 14:17
  • Uh, ok, it is quite weird form, at least to me. However, if you say that "It somethimes works" then it is expected to specify the input, actual output and expected output. It is easier to spot the bug if you know the test that fails. – luk32 Feb 26 '14 at 14:21
  • Try writing a unittest to narrow down the bug. – 0xcaff Feb 26 '14 at 14:22
  • I am terribly sorry. I normally have my questions filtered by the Java tag. No wonder my answer was downvoted. The syntax is strangely similar, though! (Feel free to throw banana peels at me because Python is in the title of the question) – Rainbolt Feb 26 '14 at 14:34
  • Don't make it too complicated, check SO for the solution of this common problem, i.e. http://math.stackexchange.com/q/99565/265765 or http://stackoverflow.com/q/9324339/893159. I did not test the two solutions, but i guess they will work. There are a lot of questions about rectangle intersection and even the area of the intersection, some will be usable for you. – allo Jul 18 '16 at 09:54

1 Answers1

1

I wrote a short program that generates random squares, and tests them with your function. Press control to generate new squares until you see something that fails. I did some quick testing and saw some cases failing. You need to consider if they are counted as intersecting, if they have borders at the exact same value.

like this for example: enter image description here

import numpy as np
import matplotlib.pyplot as plt
from random import randint


def caseTest(xmin_a, xmax_a, ymin_a, ymax_a, xmin_b, xmax_b, ymin_b, ymax_b):

    if xmin_a < xmax_b <= xmax_a and (ymin_a < ymax_b <= ymax_a or ymin_a <= ymin_b < ymax_a):
        print('YES'),
    elif xmin_a <= xmin_b < xmax_a and (ymin_a < ymax_b <= ymax_a or ymin_a <= ymin_b < ymax_a):
        print('YES'),
    elif xmin_b < xmax_a <= xmax_b and (ymin_b < ymax_a <= ymax_b or ymin_b <= ymin_a < ymax_b):
        print('YES'),
    elif xmin_b <= xmin_a < xmax_b and (ymin_b < ymax_a <= ymax_b or ymin_b <= ymin_a < ymax_b):
        print('YES'),
    else:
        print('NO'),

    print(xmin_a, xmax_a, ymin_a, ymax_a, xmin_b, xmax_b, ymin_b, ymax_b)


def createRandomSquares():
    fig = plt.gcf()
    fig.clf()

    xmin_a = randint(0, 9)
    xmax_a = randint(xmin_a + 1, 10)
    ymin_a = randint(0, 9)
    ymax_a = randint(ymin_a + 1, 10)

    xmin_b = randint(0, 9)
    xmax_b = randint(xmin_b + 1, 10)
    ymin_b = randint(0, 9)
    ymax_b = randint(ymin_b + 1, 10)

    caseTest(xmin_a, xmax_a, ymin_a, ymax_a, xmin_b, xmax_b, ymin_b, ymax_b)

    sqr1_x = [xmin_a, xmax_a, xmax_a, xmin_a, xmin_a]
    sqr1_y = [ymin_a, ymin_a, ymax_a, ymax_a, ymin_a]
    sqr2_x = [xmin_b, xmax_b, xmax_b, xmin_b, xmin_b]
    sqr2_y = [ymin_b, ymin_b, ymax_b, ymax_b, ymin_b]

    plt.plot(sqr1_x, sqr1_y)
    plt.plot(sqr2_x, sqr2_y)

    ax = plt.gca()

    ax.set_ylim(min([ymin_b, ymin_a]) - 1, max([ymax_a, ymax_b]) + 1)
    ax.set_xlim(min([xmin_b, xmin_a]) - 1, max([xmax_a, xmax_b]) + 1)

    fig.canvas.draw()


def key_event(event):
    if (event.key == 'control'):
        createRandomSquares()

fig = plt.figure()
fig.canvas.mpl_connect('key_press_event', key_event)

plt.show()

This one gives NO - obviously should be yes.

enter image description here

  • Are they considered intersecting if one is fully contained in the other?
    This one gives YES - i would say that might be NO.

enter image description here

M4rtini
  • 13,186
  • 4
  • 35
  • 42