# base/rect (vanilla-engine 0.4.0)

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

Rectangle. (top-left position + size, the original base/rect.js)
- The original's properties like width, min, center and left became get_... and set_... functions.
- intersects was made first in the Rust version and counts edges that merely touch as not overlapping.
  The original overlaps counts touching edges as overlapping, and intersection uses it.

## Rect (struct) (Clone, Copy, Debug, Default, PartialEq, GeometryKeyValue, ToReflectValue)

Rectangle. (a Copy value with the top-left position and size as public fields, y grows downward)

Example: Test points and areas, find the overlapping area

```rust
use vanilla::{Rect, Vector2};

let panel = Rect::create(0.0, 0.0, 100.0, 50.0);
let button = Rect::create(80.0, 40.0, 40.0, 20.0);
assert!(panel.contains(Vector2::create(10.0, 10.0)));
assert!(panel.overlaps(&button));

let overlap = panel.intersection(&button);
assert_eq!(overlap, Rect::create(80.0, 40.0, 20.0, 10.0));
let bounds = panel.union(&button);
assert_eq!((bounds.get_right(), bounds.get_bottom()), (120.0, 60.0));
```

- `pub position: Vector2` (field, since 0.3.2): Top-left position.
  - returns Vector2
- `pub size: Vector2` (field, since 0.3.2): Size. (x is the width, y the height)
  - returns Vector2
- `pub const fn create(x: f64, y: f64, width: f64, height: f64) -> Rect` (static, since 0.3.2): Constructor.
  - `x` f64: Left x.
  - `y` f64: Top y.
  - `width` f64: Width.
  - `height` f64: Height.
  - returns Rect: The created rectangle.
- `pub const fn zero() -> Rect` (static, since 0.3.2): Empty rectangle.
  - returns Rect: A rectangle with position and size all 0.
- `pub fn is_zero(&self) -> bool` (method, since 0.3.2): Whether every value is 0.
  - returns bool: true if all four values of position and size are 0.
- `pub fn get_width(&self) -> f64` (method, since 0.3.2): Width.
  - returns f64: Width. (size.x)
- `pub fn get_height(&self) -> f64` (method, since 0.3.2): Height.
  - returns f64: Height. (size.y)
- `pub fn set_width(&mut self, width: f64)` (method, since 0.4.0): Sets the width.
  - `width` f64: New width. (the top left stays)
- `pub fn set_height(&mut self, height: f64)` (method, since 0.4.0): Sets the height.
  - `height` f64: New height. (the top left stays)
- `pub fn get_min(&self) -> Vector2` (method, since 0.4.0): Top left. (the position)
  - returns Vector2: Top-left point.
- `pub fn set_min(&mut self, minimum: Vector2)` (method, since 0.4.0): Sets the top left. (keeps the bottom right and changes the size)
  - `minimum` Vector2: New top-left point.

  Example: Move only the top left while keeping the bottom right

  ```rust
  use vanilla::{Rect, Vector2};
  
  let mut rect = Rect::create(10.0, 20.0, 100.0, 50.0);
  rect.set_min(Vector2::zero());
  assert_eq!(rect, Rect::create(0.0, 0.0, 110.0, 70.0));
  ```

- `pub fn get_max(&self) -> Vector2` (method, since 0.4.0): Bottom right. (position + size)
  - returns Vector2: Bottom-right point.
- `pub fn set_max(&mut self, maximum: Vector2)` (method, since 0.4.0): Sets the bottom right. (keeps the position and changes the size)
  - `maximum` Vector2: New bottom-right point.
- `pub fn get_center(&self) -> Vector2` (method, since 0.4.0): Center.
  - returns Vector2: Center point.
- `pub fn set_center(&mut self, center: Vector2)` (method, since 0.4.0): Sets the center. (moves it, keeping the size)
  - `center` Vector2: New center point.
- `pub fn get_left(&self) -> f64` (method, since 0.4.0): Left x.
  - returns f64: Left x.
- `pub fn set_left(&mut self, left: f64)` (method, since 0.4.0): Sets the left x. (moves it, keeping the size)
  - `left` f64: New left x.
- `pub fn get_top(&self) -> f64` (method, since 0.4.0): Top y.
  - returns f64: Top y.
- `pub fn set_top(&mut self, top: f64)` (method, since 0.4.0): Sets the top y. (moves it, keeping the size)
  - `top` f64: New top y.
- `pub fn get_right(&self) -> f64` (method, since 0.4.0): Right x.
  - returns f64: Right x. (left x + width)
- `pub fn set_right(&mut self, right: f64)` (method, since 0.4.0): Sets the right x. (moves it, keeping the size)
  - `right` f64: New right x.
- `pub fn get_bottom(&self) -> f64` (method, since 0.4.0): Bottom y.
  - returns f64: Bottom y. (top y + height)
- `pub fn set_bottom(&mut self, bottom: f64)` (method, since 0.4.0): Sets the bottom y. (moves it, keeping the size)
  - `bottom` f64: New bottom y.
- `pub fn contains(&self, point: Vector2) -> bool` (method, since 0.3.2): Whether a point is inside.
  - `point` Vector2: Point to test.
  - returns bool: true if the point is inside or on an edge.
- `pub fn intersects(&self, other: &Rect) -> bool` (method, since 0.3.2): Whether it overlaps another rectangle.
  - `other` &Rect: Rectangle to check overlap with.
  - returns bool: true if the insides overlap. false if the edges merely touch.

  Example: Touching rectangles do not overlap with intersects but do with overlaps

  ```rust
  use vanilla::Rect;
  
  let left = Rect::create(0.0, 0.0, 10.0, 10.0);
  let touching = Rect::create(10.0, 0.0, 5.0, 5.0);
  assert!(!left.intersects(&touching));
  assert!(left.overlaps(&touching));
  ```

- `pub fn overlaps(&self, other: &Rect) -> bool` (method, since 0.4.0): Whether it overlaps another rectangle. (the original overlaps, counts merely touching edges as overlapping)
  - `other` &Rect: Rectangle to check overlap with.
  - returns bool: true if overlapping or the edges touch.
- `pub fn intersection(&self, other: &Rect) -> Rect` (method, since 0.4.0): Overlapping area. (an empty rectangle if not overlapping, width or height 0 if only the edges touch)
  - `other` &Rect: Rectangle to compute the overlapping area with.
  - returns Rect: The overlapping rectangle. Rect::zero() if not overlapping, a rectangle with width or height 0 if only the edges touch.

  Example: Overlapping, touching, apart

  ```rust
  use vanilla::Rect;
  
  let first = Rect::create(0.0, 0.0, 10.0, 10.0);
  assert_eq!(first.intersection(&Rect::create(5.0, 5.0, 10.0, 10.0)), Rect::create(5.0, 5.0, 5.0, 5.0));
  assert_eq!(first.intersection(&Rect::create(10.0, 0.0, 5.0, 5.0)), Rect::create(10.0, 0.0, 0.0, 5.0));
  assert!(first.intersection(&Rect::create(20.0, 20.0, 5.0, 5.0)).is_zero());
  ```

- `pub fn union(&self, other: &Rect) -> Rect` (method, since 0.4.0): The smallest rectangle covering both.
  - `other` &Rect: Rectangle to cover together.
  - returns Rect: The smallest rectangle covering both.
- `pub fn clamp(value: Rect, minimum: Rect, maximum: Rect) -> Rect` (static, since 0.4.0): Clamps position and size per component.
  - `value` Rect: Rectangle to clamp.
  - `minimum` Rect: Minimum of position and size. (position and size are checked separately)
  - `maximum` Rect: Maximum of position and size. (position and size are checked separately)
  - returns Rect: A new rectangle with position and size clamped per component.

  Example: Clamp the position to 0 ~ 100 and the size to 10 ~ 200

  ```rust
  use vanilla::Rect;
  
  let minimum = Rect::create(0.0, 0.0, 10.0, 10.0);
  let maximum = Rect::create(100.0, 100.0, 200.0, 200.0);
  let clamped = Rect::clamp(Rect::create(-5.0, 50.0, 500.0, 1.0), minimum, maximum);
  assert_eq!(clamped, Rect::create(0.0, 50.0, 200.0, 10.0));
  ```

