Problem:


Counter-Clockwise

For given three points p0, p1, p2, print

COUNTER_CLOCKWISE

if p0, p1, p2 make a counterclockwise turn (1),

CLOCKWISE

if p0, p1, p2 make a clockwise turn (2),

ONLINE_BACK

if p2 is on a line p2, p0, p1 in this order (3),

ONLINE_FRONT

if p2 is on a line p0, p1, p2 in this order (4),

ON_SEGMENT

if p2 is on a segment p0p1 (5).

Input

xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1

In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.

Output

For each query, print the above mentioned status.

Constraints

1 ≤ q ≤ 1000

-10000 ≤ xi, yi ≤ 10000

p0 and p1 are not identical.

Sample Input 1

0 0 2 0
2
-1 1
-1 -1

Sample Output 1

COUNTER_CLOCKWISE
CLOCKWISE

Sample Input 2

0 0 2 0
3
-1 0
0 0
3 0

Sample Output 2

ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT



Sample input:
0 0 2 0
2
-1 1
-1 -1


Sample output:
COUNTER_CLOCKWISE
CLOCKWISE

Submission status: Accepted

Solution code:
x0,y0,x1,y1 = map(int, input().split( ))
p0 = [x0,y0]
p1= [x1,y1]
v1 =[p1[0]-p0[0],p1[1]-p0[1]]
q = int(input())
def cross(a,b):
    crs = a[0]*b[1] - a[1]*b[0]
    return crs
def dot(a,b):
    dt = a[0]*b[0] + a[1]*b[1]
    return dt
eps = 0.000001
for i in range(q):
    p2 = list(map(int, input().split( )))
    v2=[p2[0]-p0[0],p2[1]-p0[1]]
    if cross(v1,v2) >eps:
        print("COUNTER_CLOCKWISE")
    elif cross(v1,v2) <-eps:
        print("CLOCKWISE")
    elif dot(v1,v2)<-eps:
        print("ONLINE_BACK")
    elif v1[0]**2+v1[1]**2 < v2[0]**2+v2[1]**2:#abs(v1[0])<abs(v2[0]):はx成分0でおかしい
        print("ONLINE_FRONT")
    else:#p1=p2はこっち
        print("ON_SEGMENT")

Submission status: Accepted

Solution code:
import math,sys
try: sys.stdin = open('in.in','r')
except: pass

class Point:
	def __init__(self,x,y):
		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,p):
		return Point(self.x*p,self.y*p)

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 Dis(a,b):
	return math.sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) )
def Dis2(a,b):
	return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)

x0,y0,x1,y1 = map(int,input().split())
p0 = Point(x0,y0)
p1 = Point(x1,y1)
q = int(input())
for i in range(q):
	x2,y2 = map(int,input().split())
	p2 = Point(x2,y2)

	cross = Cross(p1-p0,p2-p0)
	if cross > 0:
		print('COUNTER_CLOCKWISE')
	elif cross < 0:
		print('CLOCKWISE')
	else:
		dot = Dot(p1-p0,p2-p0)
		if dot < 0:
			print('ONLINE_BACK')
		else:
			len1 = Dis2(p1,p0)
			len2 = Dis2(p2,p0)
			print( 'ONLINE_FRONT' if len1 < len2 else 'ON_SEGMENT' )

Submission status: Accepted

Solution code:
import cmath
EPS = 1e-4

#外積
def OuterProduct(one, two):
	tmp = one.conjugate() * two
	return tmp.imag

#内積
def InnerProduct(one, two):
	tmp = one.conjugate() * two
	return tmp.real

#点が線分上にあるか
def IsOnSegment(point, begin, end):
	if abs(OuterProduct(begin-point, end-point)) <= EPS and InnerProduct(begin-point, end-point) <= EPS:
		return True
	else:
		return False

#3点が反時計回りか
#一直線上のときの例外処理できていない→とりあえずF
def CCW(p, q, r):
	one, two = q-p, r-q
	if OuterProduct(one, two) > -EPS:
		return True
	else:
		return False

def solve(p, q, r):
	if abs(OuterProduct(q-r, p-r)) <= EPS:
		if InnerProduct(q-r, p-r) <= EPS:
			return "ON_SEGMENT"
		elif abs(p-r) < abs(q-r):
			return "ONLINE_BACK"
		else:
			return "ONLINE_FRONT"
	elif CCW(p, q, r):
		return "COUNTER_CLOCKWISE"
	else:
		return "CLOCKWISE"

a, b, c, d = map(int, input().split())
p, q = complex(a, b), complex(c, d)
n = int(input())
for _ in range(n):
	x, y = map(int, input().split())
	r = complex(x, y)
	print(solve(p, q, r))

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

###########################
#          幾何
###########################
def sgn(a):
    if a < -eps: return -1
    if a >  eps: return  1
    return 0

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

    def tolist(self):
        return [self.x,self.y]

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

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

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

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

    def __lt__(self,other):
        tmp = sgn(self.x - other.x)
        if tmp != 0:
            return tmp < 0
        else:
            return sgn(self.y - other.y) < 0

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

    def abs(self):
        return math.sqrt(self.x**2+self.y**2)

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

    def det(self,p):
        return self.x * p.y - self.y*p.x

    def arg(self,p):
        return math.atan2(y,x)

# 点の進行方向 a -> b -> c
def iSP(a,b,c):
    tmp = sgn((b-a).det(c-a))
    if tmp > 0:   return 1   # 左に曲がる場合
    elif tmp < 0: return -1  # 右に曲がる場合
    else: # まっすぐ
        if sgn((b-a).dot(c-a)) < 0: return -2 # c-a-b の順
        if sgn((a-b).dot(c-b)) < 0: return  2 # a-b-c の順
        return 0 # a-c-bの順

# ab,cd の直線交差
def isToleranceLine(a,b,c,d):
    if sgn((b-a).det(c-d)) != 0: return 1 # 交差する
    else:
        if sgn((b-a).det(c-a)) != 0: return 0 # 平行
        else: return -1 # 同一直線

# ab,cd の線分交差 重複，端点での交差もTrue
def isToleranceSegline(a,b,c,d):
    return sgn(iSP(a,b,c)*iSP(a,b,d))<=0 and sgn(iSP(c,d,a)*iSP(c,d,b)) <= 0

# 直線ab と 直線cd の交点 (存在する前提)
def Intersection(a,b,c,d):
    tmp1 = (b-a)*((c-a).det(d-c))
    tmp2 = (b-a).det(d-c)
    return a+(tmp1/tmp2)

# 直線ab と 点c の距離
def DistanceLineToPoint(a,b,c):
    return abs(((c-a).det(b-a))/((b-a).abs()))

# 線分ab と 点c の距離
def DistanceSeglineToPoint(a,b,c):
    if sgn((b-a).dot(c-a)) < 0: # <cab が鈍角
        return (c-a).abs()
    if sgn((a-b).dot(c-b)) < 0: # <cba が鈍角
        return (c-b).abs()
    return DistanceLineToPoint(a,b,c)

# 直線ab への 点c からの垂線の足
def Vfoot(a,b,c):
    d = c + Point((b-a).y,-(b-a).x)
    return Intersection(a,b,c,d)

# 多角形の面積
def PolygonArea(Plist):
    Plist = ConvexHull(Plist)
    L = len(Plist)
    S = 0
    for i in range(L):
        tmpS = (Plist[i-1].det(Plist[i]))/2
        S += tmpS
    return S

# 多角形の重心
def PolygonG(Plist):
    Plist = ConvexHull(Plist)
    L = len(Plist)
    S = 0
    G = Point(0,0)
    for i in range(L):
        tmpS = (Plist[i-1].det(Plist[i]))/2
        S += tmpS
        G += (Plist[i-1]+Plist[i])/3*tmpS
    return G/S

# 凸法
def ConvexHull(Plist):
    Plist.sort()
    L = len(Plist)
    qu = deque([])
    quL = 0
    for p in Plist:
        while quL >= 2 and iSP(qu[quL-2],qu[quL-1],p) == 1:
            qu.pop()
            quL -= 1
        qu.append(p)
        quL += 1

    qd = deque([])
    qdL = 0
    for p in Plist:
        while qdL >= 2 and iSP(qd[qdL-2],qd[qdL-1],p) == -1:
            qd.pop()
            qdL -= 1
        qd.append(p)
        qdL += 1

    qd.pop()
    qu.popleft()
    hidari = list(qd) + list(reversed(qu)) # 左端開始，左回りPlist
    return hidari

x1,y1,x2,y2 = inpl()
p0,p1 = Point(x1,y1), Point(x2,y2)
q = inp()
for _ in range(q):
    p2 = Point(*inpl())
    tmp = iSP(p1,p0,p2)
    if tmp == -1:
        print('COUNTER_CLOCKWISE')
    elif tmp == 1:
        print('CLOCKWISE')
    elif tmp == 2:
        print('ONLINE_BACK')
    elif tmp == -2:
        print('ONLINE_FRONT')
    else:
        print('ON_SEGMENT')

Submission status: Accepted

Solution code:
import math

eps = 10 ** -8

line = input()
x1, y1, x2, y2 = list(map(int, line.split()))
line = input()
q = int(line)
pts = []
for _ in range(0, q):
    line = input()
    x, y = list(map(int, line.split()))
    pts += [[x, y]]

def solve():
    l1 = math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
    for x, y in pts:
        l2 = math.sqrt((x - x1)**2 + (y - y1)**2)
        dx = (x2 - x1) / l1
        dy = (y2 - y1) / l1
        ip = (x - x1) * (x2 - x1) + (y - y1) * (y2 - y1)
        sine = (x - x1) * (y2 - y1) - (y - y1) * (x2 - x1)
        if sine < 0.0:
            print("COUNTER_CLOCKWISE")
        elif sine > 0.0:
            print("CLOCKWISE")
        elif abs(ip - l1 * l2) < eps:
            if l1 < l2:
                print("ONLINE_FRONT")
            else:
                print("ON_SEGMENT")
        elif ip + l1 * l2 < eps:
            print("ONLINE_BACK")
solve()

Submission status: Accepted

Solution code:
pp = list(map(int, input().split()))
n = int(input())

def cross(a, b):
    return a[0]*b[1]-a[1]*b[0]
        
def dot(a, b):
    n = len(a)
    if n != len(b):
        return None
    ans = 0
    for i, j in zip(a, b):
        ans += i*j
    return ans
    
for i in range(n):
    bb = list(map(int, input().split()))
    a = (pp[2]-pp[0], pp[3]-pp[1])
    b = (bb[0]-pp[0], bb[1]-pp[1])
    if cross(a, b) > 0:
        print("COUNTER_CLOCKWISE")
    elif cross(a, b) < 0:
        print("CLOCKWISE")
    else:
        if dot(a,b)<0:
            print("ONLINE_BACK")
        elif dot(a, (b[0]-a[0],b[1]-a[1]))>0: 
            print("ONLINE_FRONT")
        else:
            print("ON_SEGMENT")

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)


def distanceLP(v1, v2, p):
    '''
    v1 -> v2の直線とpとの距離
    '''
    return abs((v2-v1).cross(p-v1))/abs(v2-v1)


def distanceSP(v1, v2, p):
    '''
    v1 -> v2の線分とpとの距離
    '''
    if (v2-v1).dot(p-v1) < 0.0:
        return abs(p-v1)
    if (v1-v2).dot(p-v2) < 0.0:
        return abs(p-v2)
    return distanceLP(v1, v2, p)


def intersect(p1, p2, p3, p4):
    '''
    p1p2とp3p4の交差判定
    '''
    t1 = (p1.x-p2.x)*(p3.y-p1.y)+(p1.y-p2.y)*(p1.x-p3.x)
    t2 = (p1.x-p2.x)*(p4.y-p1.y)+(p1.y-p2.y)*(p1.x-p4.x)
    t3 = (p3.x-p4.x)*(p1.y-p3.y)+(p3.y-p4.y)*(p3.x-p1.x)
    t4 = (p3.x-p4.x)*(p2.y-p3.y)+(p3.y-p4.y)*(p3.x-p2.x)
    return (t1*t2) < 0.0 and (t3*t4) < 0.0


def distance(a1, a2, b1, b2):
    '''
    線分a1a2とb1b2の距離
    '''
    if intersect(a1, a2, b1, b2):
        return 0.0
    return min([
        min([distanceSP(a1, a2, b1), distanceSP(a1, a2, b2)]),
        min([distanceSP(b1, b2, a1), distanceSP(b1, b2, a2)])
    ])


p0x, p0y, p1x, p1y = map(int, input().split())
p0 = Vector2(p0x, p0y)
p1 = Vector2(p1x, p1y)
q = int(input())
for i in range(q):
    x, y = map(int, input().split())
    p2 = Vector2(x, y)
    c = (p1-p0).cross(p2-p0)
    if math.isclose(c, 0.0):
        d = (p1-p0).dot(p2-p0)
        if d < 0.0:
            print("ONLINE_BACK")
        else:
            d1 = abs(p1-p0)
            d2 = abs(p2-p0)
            if d1 < d2:
                print("ONLINE_FRONT")
            else:
                print("ON_SEGMENT")
    elif c < 0.0:
        print("CLOCKWISE")
    else:
        print("COUNTER_CLOCKWISE")

Submission status: Accepted

Solution code:
from sys import stdin

EPS = 1e-10

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.yb

    def __lt__(self, other):
        return self.x < other.x and self.y < other.yb

    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 Point(Vector):
    def __init__(self, *args, **kargs):
        return super().__init__(*args, **kargs)

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

def ccw(p0, p1, p2):
    a = p1 - p0
    b = p2 - p0
    if a.cross(b) > EPS:
        return 1
    elif a.cross(b) < -EPS:
        return -1
    elif a.dot(b) < -EPS:
        return 2
    elif a.norm() < b.norm():
        return -2
    else:
        return 0

def read_and_print_results(p0, p1, n):
    for _ in range(n):
        line = stdin.readline().strip().split()
        p2 = Vector(int(line[0]), int(line[1]))
        result = ccw(p0, p1, p2)
        if result == 1:
            print('COUNTER_CLOCKWISE')
        elif result == -1:
            print('CLOCKWISE')
        elif result == 2:
            print('ONLINE_BACK')
        elif result == -2:
            print('ONLINE_FRONT')
        elif result == 0:
            print('ON_SEGMENT')

x0, y0, x1, y1 = [int(i) for i in input().split()]
p0 = Point(x0, y0)
p1 = Point(x1, y1)
n = int(input())
read_and_print_results(p0, p1, n)

Submission status: Accepted

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

from typing import List, Tuple
from math import sqrt
from enum import Enum

EPS = 1e-10


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


class PointLocation(Enum):
    COUNTER_CLOCKWISE = 1
    CLOCKWISE = 2
    ONLINE_BACK = 3
    ONLINE_FRONT = 4
    ON_SEGMENT = 5


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):
            # print("NotImplemented in 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)

    def distance(self, other: 'Point') -> float:
        return (self - other).abs()

    def in_side_of(self, seg: 'Segment') -> bool:
        return seg.vector().dot(
            Segment(seg.p1, self).vector()) >= 0

    def in_width_of(self, seg: 'Segment') -> bool:
        return \
            self.in_side_of(seg) and \
            self.in_side_of(seg.reverse())

    def distance_to_line(self, seg: 'Segment') -> float:
        return \
            abs((self - seg.p1).cross(seg.vector())) / \
            seg.length()

    def distance_to_segment(self, seg: 'Segment') -> float:
        if not self.in_side_of(seg):
            return self.distance(seg.p1)
        if not self.in_side_of(seg.reverse()):
            return self.distance(seg.p2)
        else:
            return self.distance_to_line(seg)

    def location(self, seg: 'Segment') -> PointLocation:
        p = self - seg.p1
        d = seg.vector().cross(p)
        if d > EPS:
            return PointLocation.COUNTER_CLOCKWISE
        if d < -EPS:
            return PointLocation.CLOCKWISE
        if seg.vector().dot(p) < 0.0:
            return PointLocation.ONLINE_BACK
        if seg.vector().norm() < p.norm():
            return PointLocation.ONLINE_FRONT
        return PointLocation.ON_SEGMENT


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):
            # print("NotImplemented in Segment")
            return NotImplemented
        return self.p1 == other.p1 and self.p2 == other.p2

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

    def reverse(self) -> 'Segment':
        return Segment(self.p2, self.p1)

    def length(self) -> float:
        return self.p1.distance(self.p2)

    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())

    def projection(self, p: Point) -> Point:
        v = self.vector()
        vp = p - self.p1
        return v.dot(vp) / v.norm() * v + self.p1

    def reflection(self, p: Point) -> Point:
        x = self.projection(p)
        return p + 2 * (x - p)

    def intersect_ratio(self, other: 'Segment') -> Tuple[float, float]:
        a = self.vector()
        b = other.vector()
        c = self.p1 - other.p1
        s = b.cross(c) / a.cross(b)
        t = a.cross(c) / a.cross(b)
        return s, t

    def intersects(self, other: 'Segment') -> bool:
        s, t = self.intersect_ratio(other)
        return (0 <= s <= 1) and (0 <= t <= 1)

    def intersection(self, other: 'Segment') -> Point:
        s, _ = self.intersect_ratio(other)
        return self.p1 + s * self.vector()

    def distance_with_segment(self, other: 'Segment') -> float:
        if not self.is_parallel(other) and \
                self.intersects(other):
            return 0
        else:
            return min(
                self.p1.distance_to_segment(other),
                self.p2.distance_to_segment(other),
                other.p1.distance_to_segment(self),
                other.p2.distance_to_segment(self))


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 main() -> None:
    x0, y0, x1, y1 = [int(x) for x in input().split()]
    s = Segment(Point(x0, y0), Point(x1, y1))
    q = int(input())

    for _ in range(q):
        x2, y2 = [int(x) for x in input().split()]
        print(Point(x2, y2).location(s).name)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
x0, y0, x1, y1 = map(int, input().split())

q = int(input())
for _ in range(q):
    x2, y2 = map(int, input().split())

    cross = (x1 - x0)*(y2 - y0) - (y1 - y0)*(x2 - x0)
    if cross > 0:
        print('COUNTER_CLOCKWISE')
        continue
    if cross < 0:
        print('CLOCKWISE')
        continue

    dot = (x1 - x0)*(x2 - x0) + (y1 - y0)*(y2 - y0)
    if dot < 0:
        print('ONLINE_BACK')
        continue
    if (x1 - x0)**2 + (y1 - y0)**2 < (x2 - x0)**2 + (y2 - y0)**2:
        print('ONLINE_FRONT')
    else:
        print('ON_SEGMENT')

Submission status: Accepted

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

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

    def __iter__(self):
        yield self.x
        yield self.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, s: int):
        return Point(self.x * s, self.y * s)

    def project(self, vector: "Vector") -> "Point":
        return (vector * (Vector(vector.p1, self).dot(vector) / vector.norm)).p2

    def reflect(self, vector: "Vector") -> "Point":
        return self + (self.project(vector) - self) * 2


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

    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
        self.norm = self.x ** 2 + self.y ** 2
        self.abs = abs(self.x + self.y)

    def __mul__(self, s):
        return Vector(self.p1, Point(self.p1.x + self.x * s, self.p1.y + self.y * s))

    def __lt__(self, other: "Vector"):
        return other.abs > self.abs

    def __gt__(self, other: "Vector"):
        return other.abs < self.abs

    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__":
    a = list(map(int, input().split()))
    p1, p2 = Point(*a[:2]), Point(*a[2:])
    vector1 = Vector(p1, p2)
    for p3 in (Point(*map(int, input().split())) for _ in [0]*int(input())):
        vector2 = Vector(p1, p3)
        if vector1.cross(vector2) > 0:
            print("COUNTER_CLOCKWISE")
        elif vector1.cross(vector2) < 0:
            print("CLOCKWISE")
        elif vector1.dot(vector2) < 0:
            print("ONLINE_BACK")
        elif vector1 < vector2:
            print("ONLINE_FRONT")
        else:
            print("ON_SEGMENT")

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")

Submission status: Accepted

Solution code:
#  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C&lang=jp
#  Counter-Clockwise : python3
#  2018.12.11 yonezawa

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

def crossProduct(s1,s2,s3):
    (x1,y1) = s1
    (x2,y2) = s2
    (x3,y3) = s3
    x1 = x1 - x2
    y1 = y1 - y2
    x3 = x3 - x2
    y3 = y3 - y2
    d = x1 * y3 - y1 * x3
    return d
def distance(s1,s2):
    (x1,y1) = s1
    (x2,y2) = s2
    return sqrt(pow((x1-x2),2) + pow((y1-y2),2))

    

    

def main():
    (x1,y1,x2,y2) = map(int,input().split())
    for i in range(int(input())):
        (x3,y3) = map(int,input().split())
        n = crossProduct((x1,y1),(x2,y2),(x3,y3))
        if n == 0:
            d1 = distance((x1,y1),(x2,y2))
            d2 = distance((x1,y1),(x3,y3))
            d3 = distance((x2,y2),(x3,y3))
            #print (d1,d2,d3,"p1:",x1,y1,"p2:",x2,y2,"p3:",x3,y3)
            #print (round(d1+d2,10),round(d3,10))
            if round(d1 + d2,5) == round(d3,5) and d1 != 0 and d2 != 0:
                print ("ONLINE_BACK")
            elif d1 >= d2 :
                print ("ON_SEGMENT")
            else:
                print ("ONLINE_FRONT")
        elif n > 0:
            print ("CLOCKWISE") 
        elif n < 0:
            print ("COUNTER_CLOCKWISE")


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

Submission status: Accepted

Solution code:
xp0, yp0, xp1, yp1 = map(int, input().split())
q = int(input())

xp1 = complex(xp1 - xp0, yp1 - yp0)
eps = 1e-8

for _ in range(q):
    x, y = map(int, input().split())
    xp2 = complex(x - xp0, y - yp0)

    temp = xp1.conjugate() * xp2
    dot = temp.real
    cross = temp.imag
    if abs(cross) < eps:
        if dot == 0:
            print("ON_SEGMENT")
        elif dot > 0:
            if abs(xp1) >= abs(xp2):
                print("ON_SEGMENT")
            else:
                print("ONLINE_FRONT")
        else:
            print("ONLINE_BACK")
    elif cross > 0:
        print("COUNTER_CLOCKWISE")
    else:
        print("CLOCKWISE")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# CGL_1_C: Points/Vectors - Counter-Clockwise

from enum import Enum
from math import sqrt


class Position(Enum):
    COUNTER_CLOCKWISE = 0
    CLOCKWISE = 1
    ONLINE_BACK = 2
    ONLINE_FRONT = 3
    ON_SEGMENT = 4


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


def length(v):
    x, y = v
    return sqrt(x**2 + y**2)


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


def ccw(v1, v2):
    d1 = dot(v1, v2)
    d2 = dot(orthogonal(v1), v2)

    if d2 > 0:
        return Position.COUNTER_CLOCKWISE
    elif d2 < 0:
        return Position.CLOCKWISE
    elif d1 < 0:
        return Position.ONLINE_BACK
    else:
        if length(v1) >= length(v2):
            return Position.ON_SEGMENT
        else:
            return Position.ONLINE_FRONT


def run():
    x0, y0, x1, y1 = [int(i) for i in input().split()]
    q = int(input())

    for _ in range(q):
        x2, y2 = [int(i) for i in input().split()]
        pos = ccw((x1-x0, y1-y0), (x2-x0, y2-y0))
        print(pos.name)


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
import math
from typing import Union


class Point(object):
    __slots__ = ['x', 'y']

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

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

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

    def __mul__(self, other: Union[int, float]):
        return Point(self.x * other, self.y * other)

    def __repr__(self):
        return f"({self.x},{self.y})"


class Vector(Point):
    __slots__ = ['x', 'y', 'pt1', 'pt2']

    def __init__(self, pt1: Point, pt2: Point):
        from_pt1_to_pt2 = pt2 - pt1
        super().__init__(from_pt1_to_pt2.x, from_pt1_to_pt2.y)
        self.pt1 = pt1
        self.pt2 = pt2

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

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

    def norm(self):
        return pow(self.x, 2) + pow(self.y, 2)

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

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


class Segment(Vector):
    __slots__ = ['x', 'y', 'pt1', 'pt2']

    def __init__(self, pt1: Point, pt2: Point):
        super().__init__(pt1, pt2)

    def projection(self, pt: Point)-> Point:
        t = self.dot(Vector(self.pt1, pt)) / pow(self.abs(), 2)
        return Point(self.pt1.x + t * self.x, self.pt1.y + t * self.y)

    def reflection(self, pt: Point) -> Point:
        return self.projection(pt) * 2 - pt

    def point_geometry(self, pt: Point):
        vec_pt1_to_pt = Vector(self.pt1, pt)
        cross = self.cross(vec_pt1_to_pt)
        if cross > 0:
            print('COUNTER_CLOCKWISE')
            return
        elif cross < 0:
            print('CLOCKWISE')
            return
        else:           # cross == 0
            dot = self.dot(vec_pt1_to_pt)
            if dot < 0:
                print('ONLINE_BACK')
                return
            else:       # dot > 0
                if self.abs() < vec_pt1_to_pt.abs():
                    print('ONLINE_FRONT')
                    return
                else:
                    print('ON_SEGMENT')
                    return

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


def main():
    p0_x, p0_y, p1_x, p1_y = map(int, input().split())
    p0 = Point(p0_x, p0_y)
    seg = Segment(p0, Point(p1_x, p1_y))
    num_query = int(input())

    for i in range(num_query):
        p2_x, p2_y = map(int, input().split())
        seg.point_geometry(Point(p2_x, p2_y))
    return


main()

Submission status: Accepted

Solution code:
from math import sqrt
x0,y0,x1,y1 = map(int, input().split())
a = [x0,y0]
b = [x1, y1]
q = int(input())

CCW = {1: 'COUNTER_CLOCKWISE',
       2: 'CLOCKWISE',
       3: 'ONLINE_BACK',
       4: 'ONLINE_FRONT',
       5: 'ON_SEGMENT',}

def dot(a,b):
  return sum ([i * j for i, j in zip(a,b)])

def sub(a,b):
  return [a[0] - b[0], a[1] - b[1]]

def cross(a,b):
  return a[0] * b[1] - a[1] * b[0]

def _abs(a):
  return sqrt(a[0] ** 2 + a[1] ** 2)

def ccw(a, b, c):
  x = sub(b,a)
  y = sub(c,a)
  if cross(x, y) > 0:
    return 1
  if cross(x, y) < 0:
    return 2
  if dot(x,y)<0:
    return 3
  if _abs(x) < _abs(y):
    return 4
  return 5

for i in range(q):
  c = list(map(int, input().split()))
  print(CCW[ccw(a,b,c)])