개발/알고리즘
프로그래머스_평행
송디
2024. 4. 19. 11:51
■ 해결 방법
기울기를 구하는 공식을 이용해 점과 점의 기울기를 구해준다.
이 때, 점이 고정되어 있는 것이 아니라 어디에 있을 줄 모르므로 점이 올 수 있는 위치에 대한 경우의 수를 구한다.
이 때, 3C2 즉 3가지의 경우의 수가 있다. 이 3가지 위치에 점들의 기울기를 비교해주면 된다.
■ 코드
function func_lean(x1, y1, x2, y2){
var x;
var y;
if(x1 < x2) x = x2 - x1
else x = x1 - x2;
if(y1 < y2) y = y2 - y1
else y = y1 - y2;
return x / y;
}
function solution(dots) {
var answer = 0;
console.log(dots);
if(func_lean(dots[0][0], dots[0][1], dots[1][0], dots[1][1]) == func_lean(dots[2][0], dots[2][1], dots[3][0], dots[3][1])) answer = 1;
else if(func_lean(dots[0][0], dots[0][1], dots[2][0], dots[2][1]) == func_lean(dots[1][0], dots[1][1], dots[3][0], dots[3][1])) answer = 1;
else if(func_lean(dots[0][0], dots[0][1], dots[3][0], dots[3][1]) == func_lean(dots[1][0], dots[1][1], dots[2][0], dots[2][1])) answer = 1;
return answer;
}
728x90