DolphinDash

[Python] 1925 - 삼각형 본문

PS/Solve

[Python] 1925 - 삼각형

DolphinDash 2025. 9. 16. 18:22

문제

평면상에 세 개의 점이 주어지면, 그 세 점으로 이루어지는 삼각형은 유일하게 결정된다. 또는, 삼각형이 이루어지지 않기도 한다. 세 점의 좌표가 주어졌을 때 다음에 따라 이 삼각형의 종류를 판단하는 프로그램을 작성하시오.

세 점이 일직선 위에 있으면 - ‘삼각형이 아님’  출력할 때는 X
세 변의 길이가 같으면 - ‘정삼각형’ 출력할 때는 JungTriangle
두 변의 길이가 같으면

    가장 큰 각이 90°보다 크면 - ‘둔각이등변삼각형’ 출력할 때는 Dunkak2Triangle
    가장 큰 각이 90°이면 - ‘직각이등변삼각형’ 출력할 때는 Jikkak2Triangle
    가장 큰 각이 90°보다 작으면 - ‘예각이등변삼각형’ 출력할 때는 Yeahkak2Triangle


세 변의 길이가 모두 다르면

    가장 큰 각이 90°보다 크면 - ‘둔각삼각형’ 출혁할 때는 DunkakTriangle
    가장 큰 각이 90°이면 - ‘직각삼각형’ 출력할 때는 JikkakTriangle
    가장 큰 각이 90°보다 작으면 - ‘예각삼각형’ 출력할 때는 YeahkakTriangle

입력

첫째 줄부터 셋째 줄까지 삼각형을 이루는 점의 x좌표와 y좌표가 빈칸을 사이에 두고 주어진다. 입력되는 수는 절댓값이 10,000을 넘지 않는 정수이다. 입력으로 주어지는 세 좌표는 중복되지 않는다.

출력

위의 경우에 따라 삼각형의 종류가 무엇인지 출력한다.

해설

부동소수점 오차 때문에 많이 틀렸다.

소스 코드

import math

epsilon = 1e-7

def is_equal(a, b):
    return math.isclose(a, b, abs_tol=epsilon)

def distance(xa, ya, xb, yb):
    return math.hypot(xa - xb, ya - yb)

def clamp(val, minimum, maximum):
    return max(minimum, min(maximum, val))

def angle(a, b, c):
    val = (b*b + c*c - a*a) / (2 * b * c)
    val = clamp(val, -1, 1)
    return math.degrees(math.acos(val))

x1, y1 = map(int, input().split())
x2, y2 = map(int, input().split())
x3, y3 = map(int, input().split())

A = distance(x1, y1, x2, y2)
B = distance(x2, y2, x3, y3)
C = distance(x3, y3, x1, y1)

sides = [A, B, C]
sides.sort()

if sides[2] >= sides[0] + sides[1] - epsilon:
    print("X")
    exit()

a = angle(A, B, C)
b = angle(B, C, A)
c = angle(C, A, B)

angles = [a, b, c]
max_angle = max(angles)

unique_sides = len(set([round(s, 7) for s in sides]))

if unique_sides == 1:
    print("JungTriangle")
elif unique_sides == 2:
    if max_angle > 90 + epsilon:
        print("Dunkak2Triangle")
    elif abs(max_angle - 90) < epsilon:
        print("Jikkak2Triangle")
    else:
        print("Yeahkak2Triangle")
else:
    if max_angle > 90 + epsilon:
        print("DunkakTriangle")
    elif abs(max_angle - 90) < epsilon:
        print("JikkakTriangle")
    else:
        print("YeahkakTriangle")

'PS > Solve' 카테고리의 다른 글

[Python] 2166 - 다각형의 면적  (0) 2025.09.17
[Python] 2467 - 용액  (0) 2025.09.17
[Python] 22986 - Flat Earth  (0) 2025.09.15
[Python] 2447 - 별 찍기 - 10  (0) 2025.09.11
[Python] 1476 - 날짜 계산  (0) 2025.09.09