프로그래머스 (JS)/Lv. 0

[Programmers] 120875번 - 평행

hodo- 2023. 3. 11. 16:37

Problem

문제 보기

점 네 개의 좌표를 담은 이차원 배열  dots가 다음과 같이 매개변수로 주어집니다.

- [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]


주어진 네 개의 점을 두 개씩 이었을 때, 두 직선이 평행이 되는 경우가 있으면 1을 없으면 0을 return 하도록 solution 함수를 완성해보세요.


Solution

function solution(dots) {
    let count = 0;
    
    function diff(a, b, c, d){
        let x = (b[1] - a[1]) / (b[0] - a[0]);
        let y = (d[1] - c[1]) / (d[0] - c[0]);
        
        if(x === y){
            count++;
        }
    }
    diff(dots[0], dots[1], dots[2], dots[3]);
    diff(dots[0], dots[2], dots[1], dots[3]);
    diff(dots[0], dots[3], dots[1], dots[2]);
    
    return count > 0 ? 1 : 0;
}

선분 평행 = 기울기가 같다

기울기 = Y변화량(b[1] - a[1]) / X변화량(b[0] - a[0])

경우의 수 12 34 / 13 24 / 14 23