def vector_from_points(p0: Point, p1: Point):
return (p1[0] - p0[0], p1[1] - p0[1])
def cross_product(u: Vector, v: Vector) -> int:
return u[0] * v[1] - u[1] * v[0]
def signed_parallelogram_area(p: Point, q: Point, r: Point) -> int:
u = vector_from_points(p, q)
v = vector_from_points(p, r)
return cross_product(u, v)
def point_to_line_distance(p0: Point, p1: Point, q: Point) -> float:
return abs(signed_parallelogram_area(p0, p1, q)) / math.dist(p0, p1)
def point_to_line_distance(p0: Point, p1: Point, q: Point) -> float:
area = (p1[0] - p0[0]) * (q[1] - p0[1]) - (p1[1] - p0[1]) * (q[0] - p0[0])
return abs(area) / math.dist(p0, p1)
point = [int(x) for x in input().split()]
처럼 파싱해서 list[int]로 받았을때 이것을 다시 tuple로 변환하는 것은 번거롭다. 두개를 다 허용하도록 하자. 타입 힌트는 Sequence[int]로 주면 된다.Point: TypeAlias = Sequence[int]
Vector: TypeAlias = Sequence[int]
def vector_from_points(p0: Point, p1: Point):
return (p1[0] - p0[0], p1[1] - p0[1])
def cross_product(u: Vector, v: Vector) -> int:
return u[0] * v[1] - u[1] * v[0]
def signed_parallelogram_area(p: Point, q: Point, r: Point) -> int:
u = vector_from_points(p, q)
v = vector_from_points(p, r)
return cross_product(u, v)
def is_ccw(p: Point, q: Point, r: Point) -> bool:
return signed_parallelogram_area(p, q, r) > 0
def is_cw(p: Point, q: Point, r: Point) -> bool:
return signed_parallelogram_area(p, q, r) < 0
def is_collinear(p: Point, q: Point, r: Point) -> bool:
return signed_parallelogram_area(p, q, r) == 0
def triangle_area(p: Point, q: Point, r: Point) -> float:
return abs(signed_parallelogram_area(p, q, r)) / 2
def point_to_line_distance(p0: Point, p1: Point, q: Point) -> float
return abs(signed_parallelogram_area(p0, p1, q)) / math.dist(p0, p1)