CSES - Aalto Competitive Programming 2024 - wk10 - Homework - Results
Submission details
Task:Line Segment Intersection
Sender:aalto2024j_005
Submission time:2024-11-08 21:01:53 +0200
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#10.55 sdetails
#20.47 sdetails
#30.49 sdetails
#40.52 sdetails
#5ACCEPTED0.04 sdetails
#6ACCEPTED0.04 sdetails

Code

import math


class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __sub__(self, other):
        return Point(self.x - other.x, self.y - other.y)

    def __eq__(self, other):
        return self.x == other.x and self.y == other.y


class Line:
    def __init__(self, a, b):
        self.a = a
        self.b = b


def cross_product(p, q):
    #x1y2 − x2y1
    return p.x * q.y - p.y * q.x


def from_the_line(p, s):
    #(p−s1)×(p−s2)
    return cross_product(p - s.a, p - s.b)


def on_the_line(p, s):
    return (cross_product(p - s.a, p - s.b) == 0 and (min(s.a.y, s.b.y) < p.y < max(s.a.y, s.b.y)) and
            (min(s.a.x, s.b.x) < p.x < max(s.a.x, s.b.x)))


def same_line(p, q):
    return on_the_line(p.a, q) or on_the_line(p.b, q) or on_the_line(q.a, p) or on_the_line(q.b, p)


def end_intersection(p, q):
    return p.a == q.a or p.a == q.b or p.b == q.b or p.b == q.a


def middle_intersection(p, q):
    return ((from_the_line(p.a, q) > 0) != (from_the_line(p.b, q) > 0) and
            (from_the_line(q.a, p) > 0) != (from_the_line(q.b, p) > 0))


n = int(input())
for i in range(n):
    x11, y11, x12, y12, x21, y21, x22, y22 = map(int, input().split())
    p1 = Point(x11, y11)
    p2 = Point(x12, y12)
    p3 = Point(x21, y21)
    p4 = Point(x22, y22)
    l1 = Line(p1, p2)
    l2 = Line(p3, p4)
    if same_line(l1, l2):# or end_intersection(l1, l2) or middle_intersection(l1, l2):
        print('YES')
    else:
        print('NO')

Test details

Test 1

Verdict:

input
100000
9 7 1 8 8 -5 0 2
10 1 -1 2 -4 1 -7 3
10 2 -8 6 1 2 2 -1
-10 1 9 -7 4 -3 -5 0
...

correct output
NO
NO
NO
NO
NO
...

user output
NO
NO
NO
NO
NO
...

Test 2

Verdict:

input
100000
81 745 -967 768 -451 -490 -454...

correct output
NO
NO
YES
NO
YES
...

user output
NO
NO
NO
NO
NO
...

Test 3

Verdict:

input
100000
492853 -452834 -657156 -282543...

correct output
YES
YES
NO
YES
YES
...

user output
NO
NO
NO
NO
NO
...

Test 4

Verdict:

input
100000
788522666 939776556 -831492125...

correct output
NO
NO
NO
NO
NO
...

user output
NO
NO
NO
NO
NO
...

Test 5

Verdict: ACCEPTED

input
1
1 6 6 6 4 4 1000000000 1000000...

correct output
YES

user output
YES

Test 6

Verdict: ACCEPTED

input
1
-1000000000 1000000000 9999999...

correct output
NO

user output
NO