pub struct Rect {
pub top: i32,
pub bottom: i32,
pub left: i32,
pub right: i32,
}Fields§
§top: i32§bottom: i32§left: i32§right: i32Implementations§
Source§impl Rect
impl Rect
Sourcepub fn new<C: Into<Coord>>(dimensions: C) -> Self
pub fn new<C: Into<Coord>>(dimensions: C) -> Self
Constructs a Rect at (0, 0).
Examples found in repository?
More 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}Sourcepub fn with_corners<C1, C2>(corner1: C1, corner2: C2) -> Self
pub fn with_corners<C1, C2>(corner1: C1, corner2: C2) -> Self
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.
pub fn dimensions(&self) -> Coord
pub fn offset(&self) -> Coord
pub fn area(&self) -> i32
Sourcepub fn width(&self) -> i32
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}Sourcepub fn height(&self) -> i32
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}pub fn partition_vertical(&self, partition: i32) -> (Self, Self)
pub fn partition_horizontal(&self, partition: i32) -> (Self, Self)
Sourcepub fn bsp(
&self,
orientation: Orientation,
splitter: &dyn Fn(Rect, Orientation) -> Option<(i32, Orientation)>,
) -> BspTree
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}Sourcepub fn translate<C: Into<Coord>>(&self, coord: C) -> Self
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}pub fn contains<C: Into<Coord>>(&self, coord: C) -> bool
pub fn x_range(&self) -> Range<i32>
pub fn y_range(&self) -> Range<i32>
Sourcepub fn iter(&self) -> impl Iterator<Item = Coord>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more