Untitled

mail@pastecode.io avatar
unknown
python
2 months ago
4.8 kB
1
Indexable
Never
from math import sqrt


def sign(n):
    if n == 0:
        return 0
    if n > 0:
        return '+'
    if n < 0:
        return '-'


def find_cos(point1, point2, point3, point4):
    x1 = abs(point1[0] - point2[0])
    y1 = abs(point1[1] - point2[1])
    x2 = abs(point3[0] - point4[0])
    y2 = abs(point3[1] - point4[1])

    return (x1 * x2 + y1 * y2) / (point_to_point(point1, point2) + point_to_point(point3, point4))


def line_intersection(l1, l2, l3, l4):
    x1, y1 = l1
    x2, y2 = l2
    x3, y3 = l3
    x4, y4 = l4

    px = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / (
                (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))
    py = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / (
                (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))
    return [px, py]


def point_to_point(point1, point2):
    return sqrt((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2)


def point_to_segment(point, segment_p1, segment_p2):
    x3, y3 = point
    x1, y1 = segment_p1
    x2, y2 = segment_p2
    px = x2 - x1
    py = y2 - y1

    norm = px * px + py * py

    u = ((x3 - x1) * px + (y3 - y1) * py) / float(norm)

    if u > 1:
        u = 1
    elif u < 0:
        u = 0

    x = x1 + u * px
    y = y1 + u * py

    dx = x - x3
    dy = y - y3

    dist = (dx * dx + dy * dy) ** .5

    return dist


def point_to_line(point, line1, line2):
    x0, y0 = point
    x1, y1 = line1
    x2, y2 = line2

    return abs((y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1) / sqrt((y2 - y1) ** 2 + (x2 - x1) ** 2)


def point_to_ray(point, ray1, ray2):
    cos = find_cos(point, ray1, ray1, ray2)
    if cos < 0:
        return point_to_point(point, ray1)

    return point_to_line(point, ray1, ray2)


def line_to_line(line1, line2, line3, line4):
    x1, y1 = line1
    x2, y2 = line2
    x3, y3 = line3
    x4, y4 = line4

    k1 = abs(y2 - y1) / abs(x2 - x1)
    k2 = abs(y4 - y3) / abs(x4 - x3)

    if k1 == k2:
        return point_to_line(line1, line3, line4)

    return 0


def segment_to_line(segment1, segment2, line1, line2):
    x1, y1 = segment1
    x2, y2 = segment2
    x3, y3 = line1
    x4, y4 = line2

    k1 = abs(y2 - y1) / abs(x2 - x1)
    k2 = abs(y4 - y3) / abs(x4 - x3)

    if k1 == k2:
        return point_to_line(segment1, line1, line2)

    return min(point_to_line(segment1, line1, line2), point_to_line(segment2, line1, line2))


def segment_to_segment(s1, s2, s3, s4):
    x1, y1 = s1
    x2, y2 = s2
    x3, y3 = s3
    x4, y4 = s4

    return min(point_to_segment(s1, s3, s4), point_to_segment(s2, s3, s4))


def segment_to_ray(s1, s2, r1, r2):
    return min(point_to_ray(s1, r1, r2), point_to_ray(s2, r1, r2))


def ray_to_ray(r1, r2, r3, r4):
    x1, y1 = r1
    x2, y2 = r2
    x3, y3 = r3
    x4, y4 = r4

    k1 = abs(y2 - y1) / abs(x2 - x1)
    k2 = abs(y4 - y3) / abs(x4 - x3)

    if k1 == k2:
        return point_to_ray(r1, r3, r4)

    return 0


def ray_to_line(r1, r2, l1, l2):
    x1, y1 = r1
    x2, y2 = r2
    x3, y3 = l1
    x4, y4 = l2

    k1 = abs(y2 - y1) / abs(x2 - x1)
    k2 = abs(y4 - y3) / abs(x4 - x3)

    intersection = line_intersection(r1, r2, l1, l2)
    if k1 == k2 or not phantom_intersection(intersection, r1, r2):
        return point_to_line(r1, l1, l2)

    return 0


def phantom_intersection(intersection, start, end):
    x, y = intersection
    x1, y1 = start
    x2, y2 = end

    return sign(x1 - x2) == sign(x1 - x) and sign(y1 - y2) == sign(y1 - y)


def f(n):
    return f'{n:.{6}f}'


a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
d = [int(i) for i in input().split()]

from_a_to_c = point_to_point(a, c)
from_a_to_cd = point_to_segment(a, c, d)
from_a_to_cd_ray = point_to_ray(a, c, d)
from_a_to_cd_line = point_to_line(a, c, d)
from_ab_to_c = point_to_segment(c, a, b)
from_ab_to_cd = segment_to_segment(a, b, c, d)
from_ab_to_cd_ray = segment_to_ray(a, b, c, d)
from_ab_to_cd_line = segment_to_line(a, b, c, d)
from_ab_ray_to_c = point_to_ray(c, a, b)
from_ab_ray_to_cd = segment_to_ray(c, d, a, b)
from_ab_ray_to_cd_ray = ray_to_ray(a, b, c, d)
from_ab_ray_to_cd_line = ray_to_line(a, b, c, d)
from_ab_line_to_c = point_to_line(c, a, b)
from_ab_line_to_cd = segment_to_line(c, d, a, b)
from_ab_line_to_cd_ray = ray_to_line(c, d, a, b)
from_ab_line_to_cd_line = line_to_line(a, b, c, d)

print(*list(map(f, [
    from_a_to_c,
    from_a_to_cd,
    from_a_to_cd_ray,
    from_a_to_cd_line,
    from_ab_to_c,
    from_ab_to_cd,
    from_ab_to_cd_ray,
    from_ab_to_cd_line,
    from_ab_ray_to_c,
    from_ab_ray_to_cd,
    from_ab_ray_to_cd_ray,
    from_ab_ray_to_cd_line,
    from_ab_line_to_c,
    from_ab_line_to_cd,
    from_ab_line_to_cd_ray,
    from_ab_line_to_cd_line
])),
      sep='\n')
Leave a Comment