# base/rect (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/rect/

사각 영역.

## Rect extends Object

사각 영역.

예제: 점과 영역 검사, 겹친 영역 구하기

```js
import { Rect, Vector2 } from "./build/vanilla.js";

const panel = Rect.create(0, 0, 100, 50);
const button = Rect.create(80, 40, 40, 20);
const point = Vector2.create(10, 10);
console.log(panel.contains(point), panel.overlaps(button));
const overlap = panel.intersection(button);
console.log(overlap.left, overlap.top, overlap.width, overlap.height);
const bounds = panel.union(button);
console.log(bounds.right, bounds.bottom);
```

- `constructor()` (constructor, since 0.0.8): 생성.
- `equals(other)` (method, since 0.0.8): 동등성 비교.
  - `other` any: 비교할 값.
  - returns boolean: 같은 객체이거나 위치와 크기가 모두 같은 Rect 면 참.
- `clone()` (method, since 0.0.9): 복제.
  - returns this: 위치와 크기가 같은 새 사각 영역.
- `get/set position` (property, since 0.0.8): 위치 반환 프로퍼티.
  위치 설정 프로퍼티.
  - returns Vector2: 좌상단 위치. (내부 벡터를 그대로 돌려준다)
- `get/set size` (property, since 0.0.8): 크기 반환 프로퍼티.
  크기 설정 프로퍼티.
  - returns Vector2: 가로, 세로 크기. (내부 벡터를 그대로 돌려준다)
- `contains(value)` (method, since 0.0.8): 겹치는지 여부.
  - `value` Vector2: 검사할 점.
  - returns boolean: 점이 영역 안이나 변 위에 있으면 참. (Vector2 가 아니면 거짓)
- `overlaps(value)` (method, since 0.0.8): 겹치는지 여부.
  - `value` Rect: 검사할 사각 영역.
  - returns boolean: 겹치거나 변이 닿으면 참. (Rect 가 아니면 거짓)
- `get/set width` (property, since 0.0.8): 가로 크기 프로퍼티.
  - returns number: 가로 크기.
- `get/set height` (property, since 0.0.8): 세로 크기 프로퍼티.
  - returns number: 세로 크기.
- `get/set min` (property, since 0.0.8): 최소 좌표 프로퍼티 (좌상단).
  - returns Vector2: 좌상단 좌표의 새 벡터.
- `get/set max` (property, since 0.0.8): 최대 좌표 프로퍼티 (우하단).
  - returns Vector2: 우하단 좌표의 새 벡터.
- `intersects(other)` (method, since 0.4.0): 다른 사각형과 겹치는지. (변이 닿기만 한 것은 겹친 것이 아니다, overlaps 는 닿기만 해도 겹친 것으로 본다)
  - `other` Rect: 검사할 사각 영역.
  - returns boolean: 넓이가 있는 겹침이 있으면 참.

  예제: 변이 닿은 두 사각 영역 비교하기

  ```js
  import { Rect } from "./build/vanilla.js";
  
  const leftRect = Rect.create(0, 0, 10, 10);
  const rightRect = Rect.create(10, 0, 10, 10);
  console.log(leftRect.overlaps(rightRect), leftRect.intersects(rightRect));
  ```

- `intersection(other)` (method, since 0.0.8): 교집합 (두 사각형이 겹치는 영역 반환).
  - `other` Rect: 겹침을 구할 사각 영역.
  - returns Rect: 겹친 부분의 새 사각 영역. (겹치지 않으면 Rect.zero())
- `union(other)` (method, since 0.0.8): 합집합 (두 사각형을 모두 포함하는 최소 사각형 반환).
  - `other` Rect: 합칠 사각 영역.
  - returns Rect: 두 영역을 모두 담는 가장 작은 새 사각 영역.
- `get/set center` (property, since 0.0.8): 가운데 반환 프로퍼티.
  가운데 설정 프로퍼티.
  - returns Vector2: 가운데 좌표의 새 벡터.
- `get/set left` (property, since 0.0.8): 왼쪽 반환 프로퍼티.
  왼쪽 설정 프로퍼티.
  - returns number: 왼쪽 X 좌표.
- `get/set top` (property, since 0.0.8): 위쪽 반환 프로퍼티.
  위쪽 설정 프로퍼티.
  - returns number: 위쪽 Y 좌표.
- `get/set right` (property, since 0.0.8): 오른쪽 반환 프로퍼티.
  오른쪽 설정 프로퍼티.
  - returns number: 오른쪽 X 좌표.
- `get/set bottom` (property, since 0.0.8): 아래쪽 반환 프로퍼티.
  아래쪽 설정 프로퍼티.
  - returns number: 아래쪽 Y 좌표.
- `static create(x, y, width, height)` (static, since 0.0.8): 새로운 사각 영역 생성.
  - `x` number: 좌상단 X 좌표.
  - `y` number: 좌상단 Y 좌표.
  - `width` number: 가로 크기.
  - `height` number: 세로 크기.
  - returns Rect: 새 사각 영역.
- `isZero()` (method, since 0.3.2): 위치와 크기가 모두 0 인지. (Rect.zero() 와 같은지를 새 객체를 만들지 않고 본다)
  - returns boolean: 위치와 크기가 모두 0 이면 참.
- `static zero()` (static, since 0.0.8): 크기가 없는 빈 사각 영역 생성.
  - returns Rect: 위치와 크기가 모두 0 인 사각 영역.
- `static clamp(value, min, max)` (static, since 0.0.8): 범위 제한.
  - `value` Rect: 자를 사각 영역.
  - `min` Rect: 위치와 크기의 최솟값.
  - `max` Rect: 위치와 크기의 최댓값.
  - returns Rect: 위치와 크기를 성분마다 자른 새 사각 영역.
