[프로그래머스] 직사각형 넓이 구하기 - JS

Lenny·2023년 7월 5일

문제

풀이

function solution(dots) {
    const xArr = [dots[0][0],dots[1][0],dots[2][0],dots[3][0]];
    const yArr = [dots[0][1],dots[1][1],dots[2][1],dots[3][1]]; 
    
     const xMax = Math.max(...xArr);
     const xMin = Math.min(...xArr);
    
    
     const yMax = Math.max(...yArr);
     const yMin = Math.min(...yArr);
    
    const width = xMax-xMin;
    const height = yMax-yMin;
    
    
    return Math.abs(width * height);
}

x축 점들과 y축 점들을 따로 모은 후

x축 점들의 최댓값과 최솟값을 구했다.
y축도 마찬가지로 최댓값과 최솟값을 구했다.

두 점 사이의 거리는 AB =|x2 −x1| 이다. (x2 > x1)

이렇게 가로와 세로길이를 구한 후 곱해줘서 넓이를 구했다.

profile
🧑‍💻

0개의 댓글