# base/rect (vanilla.js 0.4.0)

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

Rectangular area.

## Rect extends Object

Rectangular area.

Example: Test points and areas, find the overlapping area

```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): Constructor.
- `equals(other)` (method, since 0.0.8): Equality comparison.
  - `other` any: Value to compare.
  - returns boolean: True if it is the same object or a Rect with the same position and size.
- `clone()` (method, since 0.0.9): Clone.
  - returns this: A new rectangle with the same position and size.
- `get/set position` (property, since 0.0.8): Position getter property.
  Position setter property.
  - returns Vector2: Top left position. (returns the internal vector as is)
- `get/set size` (property, since 0.0.8): Size getter property.
  Size setter property.
  - returns Vector2: Width and height. (returns the internal vector as is)
- `contains(value)` (method, since 0.0.8): Whether it overlaps.
  - `value` Vector2: Point to test.
  - returns boolean: True if the point is inside the area or on its edge. (false if not a Vector2)
- `overlaps(value)` (method, since 0.0.8): Whether it overlaps.
  - `value` Rect: Rectangle to test.
  - returns boolean: True if they overlap or their edges touch. (false if not a Rect)
- `get/set width` (property, since 0.0.8): Width property.
  - returns number: Width.
- `get/set height` (property, since 0.0.8): Height property.
  - returns number: Height.
- `get/set min` (property, since 0.0.8): Minimum coordinate property (top-left).
  - returns Vector2: A new vector of the top left coordinates.
- `get/set max` (property, since 0.0.8): Maximum coordinate property (bottom-right).
  - returns Vector2: A new vector of the bottom right coordinates.
- `intersects(other)` (method, since 0.4.0): Whether it overlaps another rectangle. (Edges merely touching do not count as overlapping, overlaps counts mere touching as overlapping)
  - `other` Rect: Rectangle to test.
  - returns boolean: True if they overlap with a nonzero area.

  Example: Compare two rectangles whose edges touch

  ```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): Intersection (returns the area where the two rectangles overlap).
  - `other` Rect: Rectangle to intersect with.
  - returns Rect: A new rectangle of the overlapping part. (Rect.zero() if they do not overlap)
- `union(other)` (method, since 0.0.8): Union (returns the smallest rectangle containing both rectangles).
  - `other` Rect: Rectangle to merge.
  - returns Rect: The smallest new rectangle that contains both areas.
- `get/set center` (property, since 0.0.8): Center getter property.
  Center setter property.
  - returns Vector2: A new vector of the center coordinates.
- `get/set left` (property, since 0.0.8): Left getter property.
  Left setter property.
  - returns number: Left X coordinate.
- `get/set top` (property, since 0.0.8): Top getter property.
  Top setter property.
  - returns number: Top Y coordinate.
- `get/set right` (property, since 0.0.8): Right getter property.
  Right setter property.
  - returns number: Right X coordinate.
- `get/set bottom` (property, since 0.0.8): Bottom getter property.
  Bottom setter property.
  - returns number: Bottom Y coordinate.
- `static create(x, y, width, height)` (static, since 0.0.8): Creates a new rectangular area.
  - `x` number: Top left X coordinate.
  - `y` number: Top left Y coordinate.
  - `width` number: Width.
  - `height` number: Height.
  - returns Rect: A new rectangle.
- `isZero()` (method, since 0.3.2): Whether position and size are both 0. (Checks equality with Rect.zero() without creating a new object)
  - returns boolean: True if both position and size are 0.
- `static zero()` (static, since 0.0.8): Creates an empty rectangular area with no size.
  - returns Rect: A rectangle whose position and size are all 0.
- `static clamp(value, min, max)` (static, since 0.0.8): Clamps to a range.
  - `value` Rect: Rectangle to clamp.
  - `min` Rect: Minimum for position and size.
  - `max` Rect: Maximum for position and size.
  - returns Rect: A new rectangle with position and size clamped per component.
