Problem:


Parallel/Orthogonal

For given two lines s1 and s2, print "2" if they are parallel, "1" if they are orthogonal, or "0" otherwise.

s1 crosses points p0 and p1, and
s2 crosses points p2 and p3.

Input

The entire input looks like:

q (the number of queries)
1st query
2nd query
...
qth query

Each query consists of integer coordinates of the points p0, p1, p2, p3 in the following format:

xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3

Output

For each query, print "2", "1" or "0".

Constraints

1 ≤ q ≤ 1000

-10000 ≤ xpi, ypi ≤ 10000

p0 &ne; p1 and
p2 &ne; p3.

Sample Input 1

3
0 0 3 0 0 2 3 2
0 0 3 0 1 1 1 4
0 0 3 0 1 1 2 2

Sample Output 1

2
1
0



Sample input:
3
0 0 3 0 0 2 3 2
0 0 3 0 1 1 1 4
0 0 3 0 1 1 2 2


Sample output:
2
1
0

Submission status: Accepted

Solution code:
import math


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

    def __add__(self, v):
        return Vector2(self.x+v.x, self.y+v.y)

    def __sub__(self, v):
        return Vector2(self.x-v.x, self.y-v.y)

    def __mul__(self, v):
        return Vector2(self.x*v, self.y*v)

    def __truediv__(self, v):
        return Vector2(self.x/v, self.y/v)

    def __abs__(self):
        return math.sqrt(float(self.x*self.x+self.y*self.y))

    def dot(self, v):
        return self.x*v.x+self.y*v.y

    def cross(self, v):
        return self.x*v.y-self.y*v.x

    def norm(self):
        d = abs(self)
        return Vector2(self.x/d, self.y/d)


n = int(input())
for i in range(n):
    x0, y0, x1, y1, x2, y2, x3, y3 = map(int, input().split())
    v1 = Vector2(x1-x0, y1-y0).norm()
    v2 = Vector2(x3-x2, y3-y2).norm()
    d = v1.dot(v2)
    c = v1.cross(v2)
    if math.isclose(d, 0.0):
        print("1")
    elif math.isclose(c, 0.0):
        print("2")
    else:
        print("0")

Submission status: Accepted

Solution code:
from sys import stdin

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

    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)

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

    def __mul__(self, k):
        return Vector(self.x * k, self.y * k)

    def __gt__(self, other):
        return self.x > other.x and self.y > other.y

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

    def dot(self, other):
        return self.x * other.x + self.y * other.y

    # usually cross operation return Vector but it returns scalor
    def cross(self, other):
        return self.x * other.y - self.y * other.x

    def norm(self):
        return self.x * self.x + self.y * self.y

    def abs(self):
        return math.sqrt(self.norm())

class Segment:
    def __init__(self, p1=Vector(0, 0), p2=Vector(1, 1)):
        self.p1 = p1
        self.p2 = p2

def is_orthogonal(*args, **kwargs):
    # input two vectors with the startpoint (0, 0)
    if len(args) == 2:
        v1, v2 = args
        return v1.dot(v2) == 0
    # input two vectors with the startpoint (a1, b1) and endpoint (a2, b2)
    elif len(args) == 4:
        a1, a2, b1, b2 = args
        return is_orthogonal(a1 - a2, b1 - b2)
    # input two segments
    elif len(kwargs) == 2:
        v1 = kwargs['s1'].p1 - kwargs['s1'].p2
        v2 = kwargs['s2'].p1 - kwargs['s2'].p2
        return v1.dot(v2) == 0
    else:
        raise ArgsError

def is_parallel(*args, **kwargs):
    # input two vectors with the startpoint (0, 0)
    if len(args) == 2:
        v1, v2 = args
        return v1.cross(v2) == 0
    # input two vectors with the startpoint (a1, b1) and endpoint (a2, b2)
    elif len(args) == 4:
        a1, a2, b1, b2 = args
        return is_parallel(a1 - a2, b1 - b2)
    # input two segments
    elif len(kwargs) == 2:
        v1 = kwargs['s1'].p1 - kwargs['s1'].p2
        v2 = kwargs['s2'].p1 - kwargs['s2'].p2
        return v1.cross(v2) == 0
    else:
        raise ArgsError

from sys import stdin

def read_and_print_results(n):
    for _ in range(n):
        line = stdin.readline().strip().split()
        p0 = Vector(int(line[0]), int(line[1]))
        p1 = Vector(int(line[2]), int(line[3]))
        p2 = Vector(int(line[4]), int(line[5]))
        p3 = Vector(int(line[6]), int(line[7]))
        s1 = Segment(p0, p1)
        s2 = Segment(p2, p3)
        if is_parallel(s1=s1, s2=s2):
            print('2')
        elif is_orthogonal(s1=s1, s2=s2):
            print('1')
        else:
            print('0')

n = int(input())
read_and_print_results(n)

Submission status: Accepted

Solution code:
q=int(input())

for i in range(q):
    xp0,yp0,xp1,yp1,xp2,yp2,xp3,yp3=map(int,input().split())
    if xp0==xp1:
        if xp2==xp3:
            print(2)
        elif yp2==yp3:
            print(1)
        else:
            print(0)
    elif xp2==xp3:
        if xp1==xp0:
            print(2)
        elif yp0==yp1:
            print(1)
        else:
            print(0)
    else:
        a1=(yp1-yp0)/(xp1-xp0)
        a2=(yp3-yp2)/(xp3-xp2)
        
        #print(a1,a2)
        if a2>=a1-0.0000001 and a2<=a1+0.0000001:
            print(2)
        elif a2*a1>=-1-0.0000001 and a2*a1<=-1+0.0000001:
            print(1)
        else:
            print(0)

Submission status: Accepted

Solution code:
q = int(input())
for _ in range(q):
    x0, y0, x1, y1, x2, y2, x3, y3 = map(int, input().split())
    dot = (x1 - x0)*(x3 - x2) + (y1 - y0)*(y3 - y2)
    if dot == 0:
        print(1)
    elif ((x1 - x0)**2 + (y1 - y0)**2)*((x3 - x2)**2 + (y3 - y2)**2) == dot**2:
        print(2)
    else:
        print(0)

Submission status: Accepted

Solution code:
#! /usr/bin/env python3

from typing import List
from math import sqrt

EPS = 1e-10


def float_equal(x: float, y: float) -> bool:
    return abs(x - y) < EPS


class Point:

    def __init__(self, x: float=0.0, y: float=0.0) -> None:
        self.x = x
        self.y = y

    def __repr__(self) -> str:
        return "Point({}, {})".format(self.x, self.y)

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, Point):
            return NotImplemented
        return float_equal(self.x, other.x) and \
            float_equal(self.y, other.y)

    def __add__(self, other: 'Point') -> 'Point':
        return Point(self.x + other.x, self.y + other.y)

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

    def __mul__(self, k: float) -> 'Point':
        return Point(self.x * k, self.y * k)

    def __rmul__(self, k: float) -> 'Point':
        return self * k

    def __truediv__(self, k: float) -> 'Point':
        return Point(self.x / k, self.y / k)

    def __lt__(self, other: 'Point') -> bool:
        return self.y < other.y \
            if abs(self.x - other.x) < EPS \
            else self.x < other.x

    def norm(self):
        return self.x * self.x + self.y * self.y

    def abs(self):
        return sqrt(self.norm())

    def dot(self, other: 'Point') -> float:
        return self.x * other.x + self.y * other.y

    def cross(self, other: 'Point') -> float:
        return self.x * other.y - self.y * other.x

    def is_orthogonal(self, other: 'Point') -> bool:
        return float_equal(self.dot(other), 0.0)

    def is_parallel(self, other: 'Point') -> bool:
        return float_equal(self.cross(other), 0.0)


Vector = Point


class Segment:

    def __init__(self, p1: Point = None, p2: Point = None) -> None:
        self.p1: Point = Point() if p1 is None else p1
        self.p2: Point = Point() if p2 is None else p2

    def __repr__(self) -> str:
        return "Segment({}, {})".format(self.p1, self.p2)

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, Segment):
            return NotImplemented
        return self.p1 == other.p1 and self.p2 == other.p2

    def vector(self):
        return self.p2 - self.p1

    def is_orthogonal(self, other: 'Segment') -> bool:
        return self.vector().is_orthogonal(other.vector())

    def is_parallel(self, other: 'Segment') -> bool:
        return self.vector().is_parallel(other.vector())


Line = Segment


class Circle:

    def __init__(self, c: Point=None, r: float=0.0) -> None:
        self.c: Point = Point() if c is None else c
        self.r: float = r

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, Circle):
            return NotImplemented
        return self.c == other.c and self.r == other.r

    def __repr__(self) -> str:
        return "Circle({}, {})".format(self.c, self.r)


def classify(s1: Segment, s2: Segment) -> int:
    if s1.is_parallel(s2):
        return 2
    elif s1.is_orthogonal(s2):
        return 1
    else:
        return 0


def main() -> None:
    q = int(input())
    for _ in range(q):
        s1, s2 = Segment(), Segment()
        s1.p1.x, s1.p1.y, s1.p2.x, s1.p2.y, \
            s2.p1.x, s2.p1.y, s2.p2.x, s2.p2.y = \
            [int(x) for x in input().split()]
        print(classify(s1, s2))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
class Point(object):
    __slots__ = ["x", "y"]

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


class Vector(object):
    __slots__ = ["p1", "p2", "x", "y"]

    def __init__(self, p1: Point, p2: Point):
        self.p1 = p1
        self.p2 = p2
        self.x = p2.x - p1.x
        self.y = p2.y - p1.y

    def dot(self, other: "Vector"):
        return self.x * other.x + self.y * other.y

    def cross(self, other: "Vector"):
        return self.x * other.y - self.y * other.x


if __name__ == "__main__":
    import sys
    answer = []
    append = answer.append
    input()
    for x1, y1, x2, y2, x3, y3, x4, y4 in (map(int, l.split()) for l in sys.stdin):
        vector1 = Vector(Point(x1, y1), Point(x2, y2))
        vector2 = Vector(Point(x3, y3), Point(x4, y4))
        if vector1.dot(vector2) == 0:
            append(1)
        elif vector1.cross(vector2) == 0:
            append(2)
        else:
            append(0)

    print(*answer, sep="\n")

Submission status: Accepted

Solution code:
def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def LIR(n): return [LI() for i in range(n)]
def MI(): return map(int, input().split())

#1
#1_A
"""
x,y,s,t = map(float, input().split())
a = int(input())
s-=x
t-=y
while a:
    a -= 1
    p,q = map(float, input().split())
    p-=x
    q-=y
    ans_x = s*(q*t+p*s)/(t*t+s*s)
    ans_y = t*(q*t+p*s)/(t*t+s*s)
    print(x+ans_x, y+ans_y)
"""

#1_B
"""
p1x,p1y,c,d = MI()
q = II()
if p1x == c:
    f = 0
elif p1y == d:
    f = 1
else:
    f = 2
    m = (d-p1y)/(c-p1x)
for _ in range(q):
    px,py = MI()
    if not f:
        a = 2*p1x-px
        b = py
    elif f == 1:
        a = px
        b = 2*p1y-py
    else:
        a = (2*py+(1/m-m)*px+2*m*p1x-2*p1y)/(m+1/m)
        b = -1/m*(a-px)+py
    print(a,b)
"""

#1_C
"""
def inner_product(a,b):
    return a[0]*b[0]+a[1]*b[1]

def cross_product(a,b):
    return a[0]*b[1]-a[1]*b[0]
p,q,c,d = MI()
a = [c-p,d-q]
r = II()
for _ in range(r):
    c,d = MI()
    b = [c-p,d-q]
    co = inner_product(a,b)
    si = cross_product(a,b)
    if si == 0:
        if co >= 0:
            if a[0]**2+a[1]**2 >= b[0]**2+b[1]**2:
                print("ON_SEGMENT")
            else:
                print("ONLINE_FRONT")
        else:
            print("ONLINE_BACK")
    elif si > 0:
        print("COUNTER_CLOCKWISE")
    else:
        print("CLOCKWISE")
"""

#2
#2_A
def inner_product(a,b):
    return a[0]*b[0]+a[1]*b[1]

def cross_product(a,b):
    return a[0]*b[1]-a[1]*b[0]
q = II()
for i in range(q):
    s,t,a,b,x,y,c,d = MI()
    a = [a-s,b-t]
    b = [c-x,d-y]
    co = inner_product(a,b)
    si = cross_product(a,b)
    if not si:
        print(2)
    elif not co:
        print(1)
    else:
        print(0)

Submission status: Accepted

Solution code:
#  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A&lang=jp
#   Parallel/Orthogonal : python3
#  2018.12.15 yonezawa

#from collections import deque
import sys
input = sys.stdin.readline
#import cProfile
from math import cos,sin,radians,sqrt

def main():

    for i in range(int(input())):
        (x1,y1,x2,y2,x3,y3,x4,y4) = map(int,input().split())

        inner = (x2-x1) * (x4-x3) + (y2-y1) * (y4-y3)
        cross = (x2-x1) * (y4-y3) - (x4-x3) * (y2-y1)               
        if inner == 0:
            print("1")
        elif cross == 0:
            print ("2")
        else:
            print("0")

    
if __name__ == '__main__':
    main()
    #pr = cProfile.Profile()
    #pr.runcall(main)
    #pr.print_stats()

Submission status: Accepted

Solution code:
q = int(input())
eps = 1e-8
for _ in range(q):
    xp0, yp0, xp1, yp1, xp2, yp2, xp3, yp3 = map(int, input().split())
    v1 = complex(xp1 - xp0, yp1 - yp0)
    v2 = complex(xp3 - xp2, yp3 - yp2)
    temp = v1.conjugate() * v2
    dot = temp.real
    cross = temp.imag
    
    if  abs(dot) < eps:
        print("1")
    elif abs(cross) < eps:
        print("2")
    else:
        print("0")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# CGL_2_A: Segments/Lines - Parallel/Orthogonal


def is_parallel(v1, v2):
    return dot(orthogonal(v1), v2) == 0


def is_orthogonal(v1, v2):
    return dot(v1, v2) == 0


def dot(v1, v2):
    x1, y1 = v1
    x2, y2 = v2
    return x1 * x2 + y1 * y2


def orthogonal(v):
    x, y = v
    return -y, x


def run():
    q = int(input())

    for _ in range(q):
        x0, y0, x1, y1, x2, y2, x3, y3 = [int(i) for i in input().split()]

        v1 = (x1-x0, y1-y0)
        v2 = (x3-x2, y3-y2)

        if is_parallel(v1, v2):
            print(2)
        elif is_orthogonal(v1, v2):
            print(1)
        else:
            print(0)


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
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 __repr__(self):
        return f"({self.x},{self.y})"


class Segment:
    def __init__(self, x: Point, y: Point):
        self.pt1 = x
        self.pt2 = y
        self.vector = self.pt2 - self.pt1

    def dot(self, other):
        return self.vector.x * other.vector.x + self.vector.y * other.vector.y

    def cross(self, other):
        return self.vector.x * other.vector.y - self.vector.y * other.vector.x

    def __repr__(self):
        return f"{self.pt1},{self.pt2},{self.vector}"


def main():
        n = int(input())
        for i in range(n):
            p0_x, p0_y, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y = map(int, input().split())
            seg_1 = Segment(Point(p0_x, p0_y), Point(p1_x, p1_y))
            seg_2 = Segment(Point(p2_x, p2_y), Point(p3_x, p3_y))

            if seg_1.dot(seg_2) == 0:
                print('1')
            elif seg_1.cross(seg_2) == 0:
                print('2')
            else:
                print('0')
        return

main()

Submission status: Accepted

Solution code:
import math

EPS = 1e-10

def equals(a, b):
    return abs(a - b) < EPS


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

    def __add__(self, p):
        return Point(self.x + p.x, self.y + p.y)

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

    def __mul__(self, a):
        return Point(self.x * a, self.y * a)

    def __rmul__(self, a):
        return self * a

    def __truediv__(self, a):
        return Point(self.x / a, self.y / a)

    def norm(self):
        return self.x * self.x + self.y * self.y

    def abs(self):
        return math.sqrt(self.norm())

    def __lt__(self, p):
        if self.x != p.x:
            return self. x < p.x
        else:
            return self.y < p.y

    def __eq__(self, p):
        return equals(self.x, p.x) and equals(self.y, p.y)


class Segment:
    def __init__(self, p1, p2):
        self.p1 = p1
        self.p2 = p2


def dot(a, b):
    return a.x * b.x + a.y * b.y


def cross(a, b):
    return a.x * b.y - a.y * b.x


def isOrthogonal(s1, s2):
    return equals(dot(s1.p2 - s1.p1, s2.p2 - s2.p1), 0)


def isParallel(s1, s2):
    return equals(cross(s1.p2 - s1.p1, s2.p2 - s2.p1), 0)


if __name__ == '__main__':
    q = int(input())
    ans = []
    for i in range(q):
        x0, y0, x1, y1, x2, y2, x3, y3 = [int(v) for v in input().split()]
        s1 = Segment(Point(x0, y0), Point(x1, y1))
        s2 = Segment(Point(x2, y2), Point(x3, y3))
        if isParallel(s1, s2):
            ans.append(2)
        elif isOrthogonal(s1, s2):
            ans.append(1)
        else:
            ans.append(0)

    for v in ans:
        print(v)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

import sys
import math
import itertools as it
from collections import deque

sys.setrecursionlimit(10000000)

q = input()

for loop in range(q):
    x0, y0, x1, y1, x2, y2, x3, y3 = map(int, raw_input().split())
    a1, b1 = [x1 - x0, y1 - y0]
    a2, b2 = [x3 - x2, y3 - y2]

    if a1 * b2 - b1 * a2 == 0:
        print 2
    elif a1 * a2 + b1 * b2 == 0:
        print 1
    else:
        print 0

Submission status: Runtime Error

Solution code:
n = int(input())
for i in range(n):
    x0,y0,x1,y1,x2,y2,x3,y3 = map(float,input().split())
    if x1 - x0 == 0:
        if x3 -x2 == 0:
            print('2')
        elif y3 - y2 == 0:
            print('1')
        else:
            print('0')
    elif x3 - x2 == 0:
        if y1 - y0 == 0:
            print('1')
        else:
            print('0')
    elif y1 - y0 == 0:
        if y3 - y2 == 0:
            print('2')
        elif x3 - x2 == 0:
            print('1')
        else:
            print('0')
    elif y3 - y2 == 0:
        elif x1 - x0 == 0:
            print('1')
        else:
            print('0')
    else:
        a1 = (y1-y0)/(x1-x0)
        a2 = (y3-y2)/(x3-x2)
        if a1 * a2 == -1:
            print('1')
        elif a1 == a2 or a1 == -a2:
            print('2')
        else:
            print('0')

Submission status: Runtime Error

Solution code:

points=[]
n=int(input())
points=input().split()

p0,q0,p1,q1,p2,q2,p3,q3=points

for i in range(n):
    if (q1 - q0) * (p2 - p3) == (q2 - q3) * (p1 - p0):
        print(2)
    elif (q1 - q0) * (q2 - q3) + (p1 - p0) * (p2 - p3) == 1:
        print(1)
    else:
        print(0)