Skip to main content

Rect

Struct Rect 

Source
pub struct Rect {
    pub top: i32,
    pub bottom: i32,
    pub left: i32,
    pub right: i32,
}

Fields§

§top: i32§bottom: i32§left: i32§right: i32

Implementations§

Source§

impl Rect

Source

pub fn new<C: Into<Coord>>(dimensions: C) -> Self

Constructs a Rect at (0, 0).

Examples found in repository?
examples/life.rs (line 24)
22    fn random<C: Into<Coord>>(dimensions: C) -> Self {
23        Self {
24            grid: Grid::with_generator(Rect::new(dimensions), |(_x, _y)| {
25                rand::random::<LifeState>()
26            }),
27        }
28    }
More examples
Hide additional examples
examples/dungeon.rs (line 10)
9fn main() {
10    let rect = Rect::new((64, 64));
11    // The minimum distance a partition can get to the edge of a Rect.
12    let min_size = 8;
13
14    let mut grid = Grid::<LifeState>::new(rect);
15    let room_tree = grid
16        .bounds
17        .bsp(Orientation::Horizontal, &|rect, orientation| {
18            let dimension = match orientation {
19                Orientation::Horizontal => rect.width(),
20                Orientation::Vertical => rect.height(),
21            };
22            let max_size = dimension - min_size;
23            // No valid partitions; any cut would make the leaves too small.
24            if max_size - min_size <= 0 {
25                return None;
26            }
27            let partition = rand::thread_rng().gen_range(min_size..=max_size);
28            Some((partition, orientation.orthogonal()))
29        });
30    let room_bounds = room_tree.leaves();
31    let rooms = room_bounds.iter().flat_map(|room| {
32        shrink_randomly(
33            Rect {
34                bottom: room.bottom + 1,
35                left: room.left + 1,
36                top: room.top,
37                right: room.right,
38            },
39            4,
40        )
41        .iter()
42    });
43
44    // room_tree
45
46    let layers = cluster_layers(rooms.collect());
47    for result in grid.selection_iter_mut(layers.interior.into_iter()) {
48        if let Ok((_coord, cell)) = result {
49            *cell = LifeState::Floor;
50        }
51    }
52    for result in grid.selection_iter_mut(layers.internal_border.into_iter()) {
53        if let Ok((_coord, cell)) = result {
54            *cell = LifeState::Wall;
55        }
56    }
57    println!("{}", grid);
58}
Source

pub fn with_corners<C1, C2>(corner1: C1, corner2: C2) -> Self
where C1: Into<Coord>, C2: Into<Coord>,

Constructs a Rect, given any two corners.

It is advisable to use this over creating a RectBounds literal, because this will prevent invalid states, such as left being less than right.

Source

pub fn dimensions(&self) -> Coord

Source

pub fn offset(&self) -> Coord

Source

pub fn area(&self) -> i32

Source

pub fn width(&self) -> i32

Examples found in repository?
examples/dungeon.rs (line 19)
9fn main() {
10    let rect = Rect::new((64, 64));
11    // The minimum distance a partition can get to the edge of a Rect.
12    let min_size = 8;
13
14    let mut grid = Grid::<LifeState>::new(rect);
15    let room_tree = grid
16        .bounds
17        .bsp(Orientation::Horizontal, &|rect, orientation| {
18            let dimension = match orientation {
19                Orientation::Horizontal => rect.width(),
20                Orientation::Vertical => rect.height(),
21            };
22            let max_size = dimension - min_size;
23            // No valid partitions; any cut would make the leaves too small.
24            if max_size - min_size <= 0 {
25                return None;
26            }
27            let partition = rand::thread_rng().gen_range(min_size..=max_size);
28            Some((partition, orientation.orthogonal()))
29        });
30    let room_bounds = room_tree.leaves();
31    let rooms = room_bounds.iter().flat_map(|room| {
32        shrink_randomly(
33            Rect {
34                bottom: room.bottom + 1,
35                left: room.left + 1,
36                top: room.top,
37                right: room.right,
38            },
39            4,
40        )
41        .iter()
42    });
43
44    // room_tree
45
46    let layers = cluster_layers(rooms.collect());
47    for result in grid.selection_iter_mut(layers.interior.into_iter()) {
48        if let Ok((_coord, cell)) = result {
49            *cell = LifeState::Floor;
50        }
51    }
52    for result in grid.selection_iter_mut(layers.internal_border.into_iter()) {
53        if let Ok((_coord, cell)) = result {
54            *cell = LifeState::Wall;
55        }
56    }
57    println!("{}", grid);
58}
59
60fn shrink_randomly(rect: Rect, min_dimension: i32) -> Rect {
61    if min_dimension >= rect.width() || min_dimension >= rect.height() {
62        return rect;
63    }
64    let horizontal_shrink = rand::thread_rng().gen_range(0..rect.width() - min_dimension);
65    let vertical_shrink = rand::thread_rng().gen_range(0..rect.height() - min_dimension);
66    let new_x = if horizontal_shrink > 0 {
67        rand::thread_rng().gen_range(0..horizontal_shrink)
68    } else {
69        0
70    };
71    let new_y = if vertical_shrink > 0 {
72        rand::thread_rng().gen_range(0..vertical_shrink)
73    } else {
74        0
75    };
76    Rect {
77        top: rect.top - vertical_shrink,
78        right: rect.right - horizontal_shrink,
79        ..rect
80    }
81    .translate((new_x, new_y))
82}
Source

pub fn height(&self) -> i32

Examples found in repository?
examples/dungeon.rs (line 20)
9fn main() {
10    let rect = Rect::new((64, 64));
11    // The minimum distance a partition can get to the edge of a Rect.
12    let min_size = 8;
13
14    let mut grid = Grid::<LifeState>::new(rect);
15    let room_tree = grid
16        .bounds
17        .bsp(Orientation::Horizontal, &|rect, orientation| {
18            let dimension = match orientation {
19                Orientation::Horizontal => rect.width(),
20                Orientation::Vertical => rect.height(),
21            };
22            let max_size = dimension - min_size;
23            // No valid partitions; any cut would make the leaves too small.
24            if max_size - min_size <= 0 {
25                return None;
26            }
27            let partition = rand::thread_rng().gen_range(min_size..=max_size);
28            Some((partition, orientation.orthogonal()))
29        });
30    let room_bounds = room_tree.leaves();
31    let rooms = room_bounds.iter().flat_map(|room| {
32        shrink_randomly(
33            Rect {
34                bottom: room.bottom + 1,
35                left: room.left + 1,
36                top: room.top,
37                right: room.right,
38            },
39            4,
40        )
41        .iter()
42    });
43
44    // room_tree
45
46    let layers = cluster_layers(rooms.collect());
47    for result in grid.selection_iter_mut(layers.interior.into_iter()) {
48        if let Ok((_coord, cell)) = result {
49            *cell = LifeState::Floor;
50        }
51    }
52    for result in grid.selection_iter_mut(layers.internal_border.into_iter()) {
53        if let Ok((_coord, cell)) = result {
54            *cell = LifeState::Wall;
55        }
56    }
57    println!("{}", grid);
58}
59
60fn shrink_randomly(rect: Rect, min_dimension: i32) -> Rect {
61    if min_dimension >= rect.width() || min_dimension >= rect.height() {
62        return rect;
63    }
64    let horizontal_shrink = rand::thread_rng().gen_range(0..rect.width() - min_dimension);
65    let vertical_shrink = rand::thread_rng().gen_range(0..rect.height() - min_dimension);
66    let new_x = if horizontal_shrink > 0 {
67        rand::thread_rng().gen_range(0..horizontal_shrink)
68    } else {
69        0
70    };
71    let new_y = if vertical_shrink > 0 {
72        rand::thread_rng().gen_range(0..vertical_shrink)
73    } else {
74        0
75    };
76    Rect {
77        top: rect.top - vertical_shrink,
78        right: rect.right - horizontal_shrink,
79        ..rect
80    }
81    .translate((new_x, new_y))
82}
Source

pub fn partition_vertical(&self, partition: i32) -> (Self, Self)

Source

pub fn partition_horizontal(&self, partition: i32) -> (Self, Self)

Source

pub fn bsp( &self, orientation: Orientation, splitter: &dyn Fn(Rect, Orientation) -> Option<(i32, Orientation)>, ) -> BspTree

Executes a binary space partition with the given splitter algorithm.

Examples found in repository?
examples/dungeon.rs (lines 17-29)
9fn main() {
10    let rect = Rect::new((64, 64));
11    // The minimum distance a partition can get to the edge of a Rect.
12    let min_size = 8;
13
14    let mut grid = Grid::<LifeState>::new(rect);
15    let room_tree = grid
16        .bounds
17        .bsp(Orientation::Horizontal, &|rect, orientation| {
18            let dimension = match orientation {
19                Orientation::Horizontal => rect.width(),
20                Orientation::Vertical => rect.height(),
21            };
22            let max_size = dimension - min_size;
23            // No valid partitions; any cut would make the leaves too small.
24            if max_size - min_size <= 0 {
25                return None;
26            }
27            let partition = rand::thread_rng().gen_range(min_size..=max_size);
28            Some((partition, orientation.orthogonal()))
29        });
30    let room_bounds = room_tree.leaves();
31    let rooms = room_bounds.iter().flat_map(|room| {
32        shrink_randomly(
33            Rect {
34                bottom: room.bottom + 1,
35                left: room.left + 1,
36                top: room.top,
37                right: room.right,
38            },
39            4,
40        )
41        .iter()
42    });
43
44    // room_tree
45
46    let layers = cluster_layers(rooms.collect());
47    for result in grid.selection_iter_mut(layers.interior.into_iter()) {
48        if let Ok((_coord, cell)) = result {
49            *cell = LifeState::Floor;
50        }
51    }
52    for result in grid.selection_iter_mut(layers.internal_border.into_iter()) {
53        if let Ok((_coord, cell)) = result {
54            *cell = LifeState::Wall;
55        }
56    }
57    println!("{}", grid);
58}
Source

pub fn translate<C: Into<Coord>>(&self, coord: C) -> Self

Examples found in repository?
examples/dungeon.rs (line 81)
60fn shrink_randomly(rect: Rect, min_dimension: i32) -> Rect {
61    if min_dimension >= rect.width() || min_dimension >= rect.height() {
62        return rect;
63    }
64    let horizontal_shrink = rand::thread_rng().gen_range(0..rect.width() - min_dimension);
65    let vertical_shrink = rand::thread_rng().gen_range(0..rect.height() - min_dimension);
66    let new_x = if horizontal_shrink > 0 {
67        rand::thread_rng().gen_range(0..horizontal_shrink)
68    } else {
69        0
70    };
71    let new_y = if vertical_shrink > 0 {
72        rand::thread_rng().gen_range(0..vertical_shrink)
73    } else {
74        0
75    };
76    Rect {
77        top: rect.top - vertical_shrink,
78        right: rect.right - horizontal_shrink,
79        ..rect
80    }
81    .translate((new_x, new_y))
82}
Source

pub fn contains<C: Into<Coord>>(&self, coord: C) -> bool

Source

pub fn x_range(&self) -> Range<i32>

Source

pub fn y_range(&self) -> Range<i32>

Source

pub fn iter(&self) -> impl Iterator<Item = Coord>

Examples found in repository?
examples/dungeon.rs (line 41)
9fn main() {
10    let rect = Rect::new((64, 64));
11    // The minimum distance a partition can get to the edge of a Rect.
12    let min_size = 8;
13
14    let mut grid = Grid::<LifeState>::new(rect);
15    let room_tree = grid
16        .bounds
17        .bsp(Orientation::Horizontal, &|rect, orientation| {
18            let dimension = match orientation {
19                Orientation::Horizontal => rect.width(),
20                Orientation::Vertical => rect.height(),
21            };
22            let max_size = dimension - min_size;
23            // No valid partitions; any cut would make the leaves too small.
24            if max_size - min_size <= 0 {
25                return None;
26            }
27            let partition = rand::thread_rng().gen_range(min_size..=max_size);
28            Some((partition, orientation.orthogonal()))
29        });
30    let room_bounds = room_tree.leaves();
31    let rooms = room_bounds.iter().flat_map(|room| {
32        shrink_randomly(
33            Rect {
34                bottom: room.bottom + 1,
35                left: room.left + 1,
36                top: room.top,
37                right: room.right,
38            },
39            4,
40        )
41        .iter()
42    });
43
44    // room_tree
45
46    let layers = cluster_layers(rooms.collect());
47    for result in grid.selection_iter_mut(layers.interior.into_iter()) {
48        if let Ok((_coord, cell)) = result {
49            *cell = LifeState::Floor;
50        }
51    }
52    for result in grid.selection_iter_mut(layers.internal_border.into_iter()) {
53        if let Ok((_coord, cell)) = result {
54            *cell = LifeState::Wall;
55        }
56    }
57    println!("{}", grid);
58}

Trait Implementations§

Source§

impl Clone for Rect

Source§

fn clone(&self) -> Rect

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Rect

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for Rect

Auto Trait Implementations§

§

impl Freeze for Rect

§

impl RefUnwindSafe for Rect

§

impl Send for Rect

§

impl Sync for Rect

§

impl Unpin for Rect

§

impl UnsafeUnpin for Rect

§

impl UnwindSafe for Rect

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.