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  x p0  y p0  x p1  y p1  q  x p2 0  y p2 0  x p2 1  y p2 1 ... x p2 q-1  y p2 q-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 ≤ x i , y i ≤ 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

Solution (Python):


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

Solution (Python):
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')

Solution (Python):
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")

Solution (Python):
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())



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


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


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

Solution (Python):





import sys
input = sys.stdin.readline

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

Solution (Python):
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")

Solution (Python):



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

Solution (Python):
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:           
            dot = self.dot(vec_pt1_to_pt)
            if dot < 0:
                print('ONLINE_BACK')
                return
            else:       
                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()

Solution (Python):
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)])

Solution (Python):
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 ccw(p0, p1, p2):
    a = p1 - p0
    b = p2 - p0
    if cross(a, b) > EPS:
        return 'COUNTER_CLOCKWISE'
    if cross(a, b) < -EPS:
        return 'CLOCKWISE'
    if dot(a, b) < -EPS:
        return 'ONLINE_BACK'
    if a.norm() < b.norm():
        return 'ONLINE_FRONT'
    return 'ON_SEGMENT'


if __name__ == '__main__':
    x0, y0, x1, y1 = [int(v) for v in input().split()]
    p0 = Point(x0, y0)
    p1 = Point(x1, y1)
    q = int(input())
    ans = []
    for i in range(q):
        x2, y2 = [int(v) for v in input().split()]
        p2 = Point(x2, y2)
        ans.append(ccw(p0, p1, p2))

    for v in ans:
        print(v)

Solution (Python):


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

sys.setrecursionlimit(10000000)

x0, y0, x1, y1 = map(int, raw_input().split())
a1, b1 = [x1 - x0, y1 - y0]
q = input()

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

    if a1 * b2 - b1 * a2 > 0:
        print "COUNTER_CLOCKWISE"
    elif a1 * b2 - b1 * a2 < 0:
        print "CLOCKWISE"
    elif a1 * a2 + b1 * b2 < 0:
        print "ONLINE_BACK"
    elif abs(a2) <= abs(a1) and abs(b2) <= abs(b1):
        print "ON_SEGMENT"
    else:
        print "ONLINE_FRONT"