Problem:
Convex Cut   As shown in the figure above, cut a convex polygon g by a line p1p2 and print the area of the cut polygon which is on the left-hand side of the line.  g is represented by a sequence of points p 1 , p 2 ,..., p n where line segments connecting p i and p i+1 (1 ≤ i ≤ n−1 ) are sides of the convex polygon. The line segment connecting p n and p 1 is also a side of the polygon. Input The input is given in the following format:  g (the sequence of the points of the polygon) q (the number of queries = the number of target lines)
1st query
2nd query
: q th query  g is given as a sequence of points p 1 ,..., p n in the following format:  n  x 1  y 1  x 2  y 2 : x n  y n  The first integer n is the number of points. The coordinate of the i -th point p i is given by two integers x i and y i . The coordinates of points are given in the order of counter-clockwise visit of them. Note that all interior angles of given convex polygons are less than or equal to 180. For each query, a line represented by two points p1 and p2 is given. The coordinates of the points are given by four integers p1x , p1y , p2x and p2y . Output For each query, print the area of the cut polygon. The output values should be in a decimal fraction with an error less than 0.00001. Constraints  3 ≤ n ≤ 100  1 ≤ q ≤ 100  -10000 ≤ x i , y i ≤ 10000  -10000 ≤ p1x , p1y , p2x , p2y ≤ 10000  No point in g will occur more than once.  p1 ≠ p2  Sample Input 4
1 1
4 1
4 3
1 3
2
2 0 2 4
2 4 2 0 Sample Output 2.00000000
4.00000000

Solution (Python):
def cross(a: complex, b: complex) -> float:
    return a.real * b.imag - a.imag * b.real


def cross_point(c: complex, d: complex) -> complex:
    global lt, lv
    vec = d - c
    v1 = cross(lv, vec)
    v2 = cross(lv, lt - c)
    return c + v2 / v1 * vec


if __name__ == "__main__":
    n = int(input())
    points = [complex(*map(int, input().split())) for _ in range(n)]
    point0 = points.pop(0)
    points.append(point0)

    q = int(input())
    while q:
        x1, y1, x2, y2 = map(int, input().split())
        ls, lt = (x1 + 1j * y1, x2 + 1j * y2)
        lv = lt - ls

        area = 0.0
        prev = point0
        prev_flag = 0 <= cross(lv, prev - ls)
        cp1, cp2 = None, None
        for p in points:
            curr_flag = 0 <= cross(lv, p - ls)
            if prev_flag and curr_flag:
                area += cross(prev, p)
            elif prev_flag != curr_flag:
                cp = cross_point(prev, p)
                if prev_flag:
                    area += cross(prev, cp)
                    cp1 = cp
                else:
                    area += cross(cp, p)
                    cp2 = cp
            prev, prev_flag = p, curr_flag
        if cp1 is not None and cp2 is not None:
            area += cross(cp1, cp2)
        print(area / 2)
        q -= 1

Solution (Python):
import cmath
EPS = 1e-6


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


def IsOnLine(point, begin, end):
	return abs(OuterProduct(begin-point, end-point)) <= EPS



def CCW(p, q, r):
	one, two = q-p, r-q
	if OuterProduct(one, two) > EPS:
		return True
	else:
		return False

def Crosspoint(a, b, c, d):
	if abs(OuterProduct(b-a, d-c)) <= EPS:
		return False
	else:
		u = OuterProduct(c-a, d-a) / OuterProduct(b-a, d-c)
		return (1-u)*a + u*b


def Area(dots):
	res = 0
	for i in range(len(dots)-1):
		res += OuterProduct(dots[i], dots[i+1])
	res += OuterProduct(dots[-1], dots[0])
	return res/2

n = int(input())
dots = []
for _ in range(n):
	x, y = map(float, input().split())
	dots.append(complex(x, y))

q = int(input())
for _ in range(q):
	x, y, z, w = map(int, input().split())
	p1, p2 = complex(x, y), complex(z, w)
	res = []
	for i in range(-1, n-1):
		if IsOnLine(dots[i], p1, p2) or CCW(p1, p2, dots[i]):
			res.append(dots[i])
		if not IsOnLine(dots[i], p1, p2) and not IsOnLine(dots[i+1], p1, p2) and (CCW(p1, p2, dots[i]) != CCW(p1, p2, dots[i+1])):
			res.append(Crosspoint(dots[i], dots[i+1], p1, p2))
	if not res:
		print(0)
	else:
		print(Area(res))

Solution (Python):




def cut(ps, p0, p1):
    polygon = []
    for p2, p3 in zip(ps, ps[1:] + [ps[0]]):
        if cross(p0, p1, p0, p2) >= 0:
            polygon.append(p2)
        if intersect(p0, p1, p2, p3):
            p = intersect_point(p0, p1, p2, p3)
            if not same(p, p2):
                polygon.append(p)

    return polygon


def intersect(p0, p1, p2, p3):
    if cross(p0, p1, p0, p2) >= 0 and cross(p0, p1, p0, p3) < 0:
        return True
    elif cross(p0, p1, p0, p2) <= 0 and cross(p0, p1, p0, p3) > 0:
        return True

    return False


def same(p0, p1):
    x0, y0 = p0
    x1, y1 = p1
    return abs(x0 - x1) < 1e-10 and abs(y0 - y1) < 1e-10


def intersect_point(p0, p1, p2, p3):
    d1 = abs(cross(p0, p1, p0, p2))
    d2 = abs(cross(p0, p1, p0, p3))
    t = d1 / (d1 + d2)
    x2, y2 = p2
    x3, y3 = p3
    return x2 + (x3 - x2) * t, y2 + (y3 - y2) * t


def area(ps):
    """Calculate area of a polygon.

    >>> area([(0, 0), (2, 2), (-1, 1)])
    2.0
    >>> area([])
    0.0
    >>> area([(0, 0)])
    0.0
    >>> area([(0, 0), (1, 0)])
    0.0
    """
    area = 0.0

    if ps:
        for p0, p1 in zip(ps, ps[1:]+[ps[0]]):
            area += cross((0, 0), p0, p0, p1)
    return area / 2


def cross(p0, p1, p2, p3):
    x0, y0 = p0
    x1, y1 = p1
    x2, y2 = p2
    x3, y3 = p3
    return (x1-x0)*(y3-y2) - (x3-x2)*(y1-y0)


def run():
    n = int(input())
    ps = []

    for _ in range(n):
        x, y = [int(i) for i in input().split()]
        ps.append((x, y))

    q = int(input())

    for _ in range(q):
        x1, y1, x2, y2 = [int(i) for i in input().split()]
        print("{:.10f}".format(area(cut(ps, (x1, y1), (x2, y2)))))


if __name__ == '__main__':
    run()