기하 1


1085번 (직사각형에서 탈출)

const fs = require('fs');
const input = fs
.readFileSync('./dev/stdin')
.toString()
.trim()
.split(' ')
.map(Number);
const [x, y, w, h] = input;
const widthMin = Math.min(w - x, x);
const heightMin = Math.min(h - y, y);
console.log(Math.min(widthMin, heightMin));
  • 가로의 왼쪽, 오른쪽 중 짧은 거리를 widthMin에 넣고
  • 세로의 위쪽, 아래쪽 중 짧은 거리르 heightMin에 넣었다.
  • 둘 중 짧은 것을 출력하였다.
  • 3009번 (네 번째 점)

    const fs = require('fs');
    const input = fs.readFileSync('./dev/stdin').toString().trim().split('\n');
    const threePoint = [];
    const x = [];
    const y = [];
    for (let i = 0; i < 3; i++) {
    threePoint.push(input[i].split(' ').map(Number));
    }
    for (let i = 0; i < 3; i++) {
    if (x.includes(threePoint[i][0])) {
    x.splice(x.indexOf(threePoint[i][0]), 1);
    } else {
    x.push(threePoint[i][0]);
    }
    }
    for (let i = 0; i < 3; i++) {
    if (y.includes(threePoint[i][1])) {
    y.splice(y.indexOf(threePoint[i][1]), 1);
    } else {
    y.push(threePoint[i][1]);
    }
    }
    console.log(x[0], y[0]);
  • x좌표와 y좌표를 각각 다른 배열을 만들어 담는다.
  • 중복되는 좌표를 제거했다.
  • 각각 남은 좌표가 x와 y를 찍어야 할 위치이다.