Problem:


Problem G: Malfatti Circles

The configuration of three circles packed inside a triangle such that each circle is tangent to the
other two circles and to two of the edges of the triangle has been studied by many mathematicians
for more than two centuries. Existence and uniqueness of such circles for an arbitrary triangle
are easy to prove. Many methods of numerical calculation or geometric construction of such
circles from an arbitrarily given triangle have been discovered. Today, such circles are called the
Malfatti circles.

Figure 7 illustrates an example. The Malfatti circles of the triangle with the vertices (20, 80),
(-40, -20) and (120, -20) are approximately

    
 the circle with the center (24.281677, 45.219486) and the radius 21.565935,

    
 the circle with the center (3.110950, 4.409005) and the radius 24.409005, and

    
 the circle with the center (54.556724, 7.107493) and the radius 27.107493.

Figure 8 illustrates another example. The Malfatti circles of the triangle with the vertices
(20, -20), (120, -20) and (-40, 80) are approximately

    
 the circle with the center (25.629089, −10.057956) and the radius 9.942044,

    
 the circle with the center (53.225883, −0.849435) and the radius 19.150565, and

    
 the circle with the center (19.701191, 19.203466) and the radius 19.913790.

Your mission is to write a program to calculate the radii of the Malfatti circles of the given
triangles.

Input

The input is a sequence of datasets. A dataset is a line containing six integers x1, y1 , x2 , y2, x3
and y3 in this order, separated by a space. The coordinates of the vertices of the given triangle
are (x1 , y1 ), (x2 , y2 ) and (x3 , y3 ), respectively. You can assume that the vertices form a triangle
counterclockwise. You can also assume that the following two conditions hold.

    
 All of the coordinate values are greater than −1000 and less than 1000.

    
 None of the Malfatti circles of the triangle has a radius less than 0.1.

The end of the input is indicated by a line containing six zeros separated by a space.

Output

For each input dataset, three decimal fractions r1 , r2 and r3 should be printed in a line in this
order separated by a space. The radii of the Malfatti circles nearest to the vertices with the
coordinates (x1 , y1 ), (x2 , y2 ) and (x3 , y3 ) should be r1 , r2 and r3 , respectively.

None of the output values may have an error greater than 0.0001. No extra character should
appear in the output.

Sample Input

20 80 -40 -20 120 -20
20 -20 120 -20 -40 80
0 0 1 0 0 1
0 0 999 1 -999 1
897 -916 847 -972 890 -925
999 999 -999 -998 -998 -999
-999 -999 999 -999 0 731
-999 -999 999 -464 -464 999
979 -436 -955 -337 157 -439
0 0 0 0 0 0

Output for the Sample Input

21.565935 24.409005 27.107493
9.942044 19.150565 19.913790
0.148847 0.207107 0.207107
0.125125 0.499750 0.499750
0.373458 0.383897 0.100456
0.706768 0.353509 0.353509
365.638023 365.638023 365.601038
378.524085 378.605339 378.605339
21.895803 22.052921 5.895714



Sample input:
20 80 -40 -20 120 -20
20 -20 120 -20 -40 80
0 0 1 0 0 1
0 0 999 1 -999 1
897 -916 847 -972 890 -925
999 999 -999 -998 -998 -999
-999 -999 999 -999 0 731
-999 -999 999 -464 -464 999
979 -436 -955 -337 157 -439
0 0 0 0 0 0


Sample output:
21.565935 24.409005 27.107493
9.942044 19.150565 19.913790
0.148847 0.207107 0.207107
0.125125 0.499750 0.499750
0.373458 0.383897 0.100456
0.706768 0.353509 0.353509
365.638023 365.638023 365.601038
378.524085 378.605339 378.605339
21.895803 22.052921 5.895714

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Problems 1301
Problem G: Malfatti Circles
"""
import math
def main():
    while True:
        x1, y1, x2, y2, x3, y3 = map(int,input().split())
        if  x1 == y1 == x2 == y2 == x3 == y3 == 0:
            break
        
        a = math.sqrt( (x2-x3)*(x2-x3) + (y2-y3)*(y2-y3) ) # aの長さ
        b = math.sqrt( (x1-x3)*(x1-x3) + (y1-y3)*(y1-y3) ) # bの長さ
        c = math.sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) # cの長さ

        cosA = round((b*b + c*c - a*a),10) / round(2*b*c,10) # 余弦定理
        cosB = round((a*a + c*c - b*b),10) / round(2*a*c,10) # 余弦定理
        cosC = round((a*a + b*b - c*c),6) / round(2*a*b,10) # 余弦定理
        
        A = math.degrees(math.acos(cosA)) # bcの角度
        B = math.degrees(math.acos(cosB)) # acの角度
        C = math.degrees(math.acos(cosC)) # abの角度
        
        S = 1/2*a*c*math.sin(math.radians(B)) # 三角形の面積
        r = 2*S / (a+b+c) # 内接円半径
        
        tanA4 = round(math.tan(math.radians(A)/4),10) # tan(A/4)
        tanB4 = round(math.tan(math.radians(B)/4),10) # tan(B/4)
        tanC4 = round(math.tan(math.radians(C)/4),10) # tan(C/4)
        
        r1 = ((1+tanB4)*(1+tanC4))/(2*(1+tanA4))*r
        r2 = ((1+tanC4)*(1+tanA4))/(2*(1+tanB4))*r
        r3 = ((1+tanA4)*(1+tanB4))/(2*(1+tanC4))*r
        
        print(r1, r2, r3)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math

def length(x1, y1, x2, y2):
    return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5

while True:
    p = list(map(int, input().split()))
    if not list(filter(lambda x: x, p)):
        break

    a, b, c = length(p[0], p[1], p[2], p[3]), length(p[0], p[1], p[4], p[5]), length(p[2], p[3], p[4], p[5])
    S = 0.5 * a * b * (1 - ((a ** 2 + b ** 2 - c ** 2) / (a * b * 2)) ** 2) ** 0.5
    R = (2 * S) / (a + b + c)

    s = (a + b + c) / 2
    d = (((- a + b + c) / 2) ** 2 + R ** 2) ** 0.5
    e = (((a - b + c) / 2) ** 2 + R ** 2) ** 0.5
    f = (((a + b - c) / 2) ** 2 + R ** 2) ** 0.5

    print(R / (2 * (s - c)) * (s + f - R - e - d), end=" ")
    print(R / (2 * (s - b)) * (s + e - R - d - f), end=" ")
    print(R / (2 * (s - a)) * (s + d - R - e - f))

Submission status: Accepted

Solution code:
from functools import reduce
import math


def length(p1, p2):
    sub2 = [(a - b)**2 for a, b in zip(p1, p2)]
    return math.sqrt(sum(sub2))


def r_center(lines):
    lines_sum = reduce(lambda a, b: a + b, lines)
    s = lines_sum / 2
    area = math.sqrt(s * (s - lines[0]) * (s - lines[1]) * (s - lines[2]))
    return 2 * area / (lines_sum)


def r_length(lines, r_center):
    x = (lines[0] + lines[2] - lines[1]) / 2
    b_l = math.sqrt(x**2 + r_center**2)
    a_l = math.sqrt((lines[2] - x)**2 + r_center**2)
    c_l = math.sqrt((lines[0] - x)**2 + r_center**2)
    return [a_l, b_l, c_l]


while True:
    positions = list(map(int, input().split()))
    if reduce(lambda a, b: a | b, positions) == 0:
        break
    p1, p2, p3 = [[i, j] for i, j in zip(positions[::2], positions[1::2])]

    lines = [length(a, b) for a, b in ((p1, p2), (p2, p3), (p3, p1))]
    lines_sum = reduce(lambda a, b: a + b, lines)

    r_c = r_center(lines)
    round_half = lines_sum / 2
    rc_len = r_length(lines, r_c)

    r1 = r_c * (round_half + rc_len[0] - r_c - rc_len[1] -
                rc_len[2]) / (2 * (round_half - lines[0]))
    r2 = r_c * (round_half + rc_len[1] - r_c - rc_len[0] -
                rc_len[2]) / (2 * (round_half - lines[1]))
    r3 = r_c * (round_half + rc_len[2] - r_c - rc_len[1] -
                rc_len[0]) / (2 * (round_half - lines[2]))

    print(r2, r3, r1)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
write = sys.stdout.write
def solve():
    x1, y1, x2, y2, x3, y3 = map(int, readline().split())
    if x1 == y1 == x2 == y2 == x3 == y3 == 0:
        return False
    d12 = ((x1-x2)**2 + (y1-y2)**2)**.5
    d23 = ((x2-x3)**2 + (y2-y3)**2)**.5
    d31 = ((x3-x1)**2 + (y3-y1)**2)**.5
    e1 = ((x1, y1), ((x2-x1)/d12, (y2-y1)/d12), ((x3-x1)/d31, (y3-y1)/d31))
    e2 = ((x2, y2), ((x3-x2)/d23, (y3-y2)/d23), ((x1-x2)/d12, (y1-y2)/d12))
    e3 = ((x3, y3), ((x1-x3)/d31, (y1-y3)/d31), ((x2-x3)/d23, (y2-y3)/d23))
    def calc(e, x):
        p0, p1, p2 = e
        x0, y0 = p0; x1, y1 = p1; x2, y2 = p2
        xc = (x1 + x2)/2; yc = (y1 + y2)/2; dc = (xc**2 + yc**2)**.5
        cv = (x1*xc + y1*yc)/dc
        sv = (x1*yc - y1*xc)/dc
        d = x / cv
        return (x0 + xc * d / dc, y0 + yc * d / dc), x * sv / cv

    EPS = 1e-8
    def check(p0, r0):
        x0, y0 = p0
        left = 0; right = min(d12, d23)
        while right - left > EPS:
            mid = (left + right) / 2
            (x1, y1), r1 = calc(e2, mid)
            if (x0-x1)**2 + (y0-y1)**2 < (r0+r1)**2:
                right = mid
            else:
                left = mid
        (x1, y1), r1 = calc(e2, left)
        left = 0; right = min(d23, d31)
        while right - left > EPS:
            mid = (left + right) / 2
            (x2, y2), r2 = calc(e3, mid)
            if (x0-x2)**2 + (y0-y2)**2 < (r0+r2)**2:
                right = mid
            else:
                left = mid
        (x2, y2), r2 = calc(e3, left)
        return (x1-x2)**2 + (y1-y2)**2 < (r1+r2)**2, r1, r2

    left = 0; right = min(d12, d31)
    while right - left > EPS:
        mid = (left + right) / 2
        p0, r0 = calc(e1, mid)
        if check(p0, r0)[0]:
            left = mid
        else:
            right = mid
    p0, r0 = calc(e1, right)
    e, r1, r2 = check(p0, r0)

    write("%.16f %.16f %.16f\n" % (r0, r1, r2))
    return True
while solve():
    ...

Submission status: Accepted

Solution code:
def solve():
    from sys import stdin
    file_input = stdin
    
    while True:
        x1, y1, x2, y2, x3, y3 = map(int, file_input.readline().split())
        if x1 == y1 == x2 == y2 == 0:
            break
        
        A = x1 + y1 * 1j
        B = x2 + y2 * 1j
        C = x3 + y3 * 1j
        
        a = abs(B - C)
        b = abs(C - A)
        c = abs(A - B)
        
        s = (a + b + c) / 2
        
        r = (s * (s - a) * (s - b) * (s - c)) ** 0.5 / s
        
        D = (b * B + c * C) / (c + b)
        BD = abs(D - B)
        
        I = (BD * A + c * D) / (c + BD)
        
        d = abs(A - I)
        e = abs(B - I)
        f = abs(C - I)
        
        r1 = r / 2 / (s - a) * (s + d - r - e - f)
        r2 = r / 2 / (s - b) * (s + e - r - d - f)
        r3 = r / 2 / (s - c) * (s + f - r - d - e)
        
        print(r1, r2, r3)

solve()