[백준 - 1064] 평행사변형

kldaji·2022년 2월 27일
1

백준

목록 보기
18/76

문제링크

import kotlin.math.sqrt

fun main() {
    val bufferedReader = System.`in`.bufferedReader()
    val bufferedWriter = System.out.bufferedWriter()

    val positions = bufferedReader.readLine().split(" ").map { it.toInt() }
    if (isSameGradient(positions)) bufferedWriter.write("-1.0")
    else {
        val distances = mutableListOf<Double>()
        distances.add(getDistance(positions[0], positions[1], positions[2], positions[3]))
        distances.add(getDistance(positions[2], positions[3], positions[4], positions[5]))
        distances.add(getDistance(positions[4], positions[5], positions[0], positions[1]))
        distances.sort()

        // (distance[2] + distance[1]) * 2 - (distance[1] + distance[0]) * 2 = (distances[2] - distances[0]) * 2
        bufferedWriter.write("${(distances[2] - distances[0]) * 2}")
    }

    bufferedReader.close()
    bufferedWriter.close()
}

fun isSameGradient(positions: List<Int>): Boolean {
    return getGradient(positions[0], positions[1], positions[2], positions[3]) ==
            getGradient(positions[2], positions[3], positions[4], positions[5])
}

fun getGradient(x1: Int, y1: Int, x2: Int, y2: Int): Double {
    if (y1 - y2 == 0) return 0.0
    return (x1 - x2).toDouble() / (y1 - y2)
}

fun getDistance(x1: Int, y1: Int, x2: Int, y2: Int): Double {
    return sqrt(((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)).toDouble())
}

주석 없는 코드를 만들기 위해 노력하는 개발자입니다.

혹시라도 의도가 분명하지 않아보이는 (이해가 되지 않는) 코드가 있으시다면 편하게 답변 달아주시면 정말 감사하겠습니다.

profile
다양한 관점에서 다양한 방법으로 문제 해결을 지향하는 안드로이드 개발자 입니다.

0개의 댓글