# 평행

## **문제 설명**

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

* \[\[x1, y1], \[x2, y2], \[x3, y3], \[x4, y4]]

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

***

**제한사항**

* `dots`의 길이 = 4
* `dots`의 원소는 \[x, y] 형태이며 x, y는 정수입니다.
  * 0 ≤ x, y ≤ 100
* 서로 다른 두개 이상의 점이 겹치는 경우는 없습니다.
* 두 직선이 겹치는 경우(일치하는 경우)에도 1을 return 해주세요.
* 임의의 두 점을 이은 직선이 x축 또는 y축과 평행한 경우는 주어지지 않습니다.

***

**입출력 예**

| dots                                   | result |
| -------------------------------------- | ------ |
| \[\[1, 4], \[9, 2], \[3, 8], \[11, 6]] | 1      |
| \[\[3, 5], \[4, 1], \[2, 4], \[5, 10]] | 0      |

***

**입출력 예 설명**

입출력 예 #1

* 점 \[1, 4], \[3, 8]을 잇고 \[9, 2], \[11, 6]를 이으면 두 선분은 평행합니다.

입출력 예 #2

* 점을 어떻게 연결해도 평행하지 않습니다.

## 코드

* 두 점을 이은 선분이 평행인 경우 = 기울기가 같은 경우
* 기울기 = y증가량 / x증가량

```javascript
function solution(dots) {
    let answer = 0;
    const slope = (a, b, c, d) => {
        let abSlope = (b[1]-a[1]) / (b[0]-a[0])
        let cdSlope = (d[1]-c[1]) / (d[0]-c[0])
        return abSlope == cdSlope ? answer += 1 : answer;
    }
    slope(dots[0], dots[1], dots[2], dots[3])
    slope(dots[0], dots[2], dots[1], dots[3])
    slope(dots[0], dots[3], dots[1], dots[2])
    return answer > 0 ? 1 : 0;
}
```

### 다른 풀이

```javascript
function solution(dots) {
    if (calculateSlope(dots[0], dots[1]) === calculateSlope(dots[2], dots[3]))
        return 1;
    if (calculateSlope(dots[0], dots[2]) === calculateSlope(dots[1], dots[3]))
        return 1;
    if (calculateSlope(dots[0], dots[3]) === calculateSlope(dots[1], dots[2]))
        return 1;
    return 0;
}

function calculateSlope(arr1, arr2) {
    return (arr2[1] - arr1[1]) / (arr2[0] - arr1[0]);
}
```

answer 에 추가 안해주고 기울기가 같은 선분이 있으면 바로 return 하도록 해도 될 것 같다.👍


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://songeun.gitbook.io/coding-test/lv.0/62.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
