pub struct Rect { /* private fields */ }
Expand description
A rectangle defined by left, top, right and bottom edges.
Can have zero width and/or height. But not a negative one.
§Guarantees
- All values are finite.
- Left edge is <= right.
- Top edge is <= bottom.
- Width and height are <= f32::MAX.
Implementations§
Source§impl Rect
impl Rect
Sourcepub fn from_ltrb(left: f32, top: f32, right: f32, bottom: f32) -> Option<Rect>
pub fn from_ltrb(left: f32, top: f32, right: f32, bottom: f32) -> Option<Rect>
Creates new Rect
.
Examples found in repository?
30fn create_triangle() -> Pixmap {
31 let mut paint = Paint::default();
32 paint.set_color_rgba8(50, 127, 150, 200);
33 paint.anti_alias = true;
34
35 let mut pb = PathBuilder::new();
36 pb.move_to(0.0, 200.0);
37 pb.line_to(200.0, 200.0);
38 pb.line_to(100.0, 0.0);
39 pb.close();
40 let path = pb.finish().unwrap();
41
42 let mut pixmap = Pixmap::new(200, 200).unwrap();
43
44 pixmap.fill_path(
45 &path,
46 &paint,
47 FillRule::Winding,
48 Transform::identity(),
49 None,
50 );
51
52 let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
53 let stroke = Stroke::default();
54 paint.set_color_rgba8(200, 0, 0, 220);
55
56 pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect
57
58 pixmap
59}
Sourcepub fn from_xywh(x: f32, y: f32, w: f32, h: f32) -> Option<Rect>
pub fn from_xywh(x: f32, y: f32, w: f32, h: f32) -> Option<Rect>
Creates new Rect
.
Examples found in repository?
3fn main() {
4 let clip_path = {
5 let mut pb = PathBuilder::new();
6 pb.push_circle(250.0, 250.0, 200.0);
7 pb.push_circle(250.0, 250.0, 100.0);
8 pb.finish().unwrap()
9 };
10
11 let clip_path = clip_path
12 .transform(Transform::from_row(1.0, -0.3, 0.0, 1.0, 0.0, 75.0))
13 .unwrap();
14
15 let mut mask = Mask::new(500, 500).unwrap();
16 mask.fill_path(&clip_path, FillRule::EvenOdd, true, Transform::default());
17
18 let mut paint = Paint::default();
19 paint.anti_alias = false;
20 paint.set_color_rgba8(50, 127, 150, 200);
21
22 let mut pixmap = Pixmap::new(500, 500).unwrap();
23 pixmap.fill_rect(
24 Rect::from_xywh(0.0, 0.0, 500.0, 500.0).unwrap(),
25 &paint,
26 Transform::identity(),
27 Some(&mask),
28 );
29 pixmap.save_png("image.png").unwrap();
30}
More examples
6fn main() {
7 let path1 = {
8 let mut pb = PathBuilder::new();
9 pb.move_to(1200.0, 1200.0);
10 pb.line_to(3200.0, 18800.0);
11 pb.cubic_to(7600.0, 16800.0, 13200.0, 16000.0, 18800.0, 16000.0);
12 pb.cubic_to(14800.0, 9200.0, 8800.0, 3200.0, 1200.0, 1200.0);
13 pb.close();
14 pb.finish().unwrap()
15 };
16
17 let path2 = {
18 let mut pb = PathBuilder::new();
19 pb.move_to(18800.0, 1200.0);
20 pb.line_to(16800.0, 18800.0);
21 pb.cubic_to(12400.0, 16800.0, 6800.0, 16000.0, 1200.0, 16000.0);
22 pb.cubic_to(5200.0, 9200.0, 11200.0, 3200.0, 18800.0, 1200.0);
23 pb.close();
24 pb.finish().unwrap()
25 };
26
27 let mut pixmap = Pixmap::new(20000, 20000).unwrap();
28
29 let clip_path = {
30 let mut pb = PathBuilder::new();
31 pb.push_circle(10000.0, 10000.0, 7000.0);
32 pb.finish().unwrap()
33 };
34
35 let mut mask = Mask::new(20000, 20000).unwrap();
36 mask.fill_path(&clip_path, FillRule::Winding, true, Transform::default());
37
38 let mut paint = Paint::default();
39 paint.set_color_rgba8(90, 175, 100, 150);
40 paint.anti_alias = true;
41 let large_rect = Rect::from_xywh(500.0, 500.0, 19000.0, 19000.0).unwrap();
42 pixmap.fill_rect(large_rect, &paint, Transform::identity(), None);
43
44 paint.set_color_rgba8(50, 127, 150, 200);
45 paint.anti_alias = true;
46 pixmap.fill_path(
47 &path1,
48 &paint,
49 FillRule::Winding,
50 Transform::default(),
51 Some(&mask),
52 );
53
54 paint.set_color_rgba8(220, 140, 75, 180);
55 paint.anti_alias = false;
56 pixmap.fill_path(
57 &path2,
58 &paint,
59 FillRule::Winding,
60 Transform::default(),
61 None,
62 );
63
64 paint.set_color_rgba8(255, 10, 15, 180);
65 paint.anti_alias = true;
66 let mut stroke = Stroke::default();
67 stroke.width = 0.8; // hairline
68 pixmap.stroke_path(&path2, &paint, &stroke, Transform::default(), None);
69
70 pixmap.save_png("image.png").unwrap();
71}
Sourcepub fn round(&self) -> Option<IntRect>
pub fn round(&self) -> Option<IntRect>
Converts into an IntRect
by adding 0.5 and discarding the fractional portion.
Width and height are guarantee to be >= 1.
Sourcepub fn round_out(&self) -> Option<IntRect>
pub fn round_out(&self) -> Option<IntRect>
Converts into an IntRect
rounding outwards.
Width and height are guarantee to be >= 1.
Sourcepub fn intersect(&self, other: &Rect) -> Option<Rect>
pub fn intersect(&self, other: &Rect) -> Option<Rect>
Returns an intersection of two rectangles.
Returns None
otherwise.
Sourcepub fn from_points(points: &[Point]) -> Option<Rect>
pub fn from_points(points: &[Point]) -> Option<Rect>
Creates a Rect from Point array.
Returns None if count is zero or if Point array contains an infinity or NaN.
Sourcepub fn inset(&self, dx: f32, dy: f32) -> Option<Rect>
pub fn inset(&self, dx: f32, dy: f32) -> Option<Rect>
Insets the rectangle by the specified offset.
Sourcepub fn outset(&self, dx: f32, dy: f32) -> Option<Rect>
pub fn outset(&self, dx: f32, dy: f32) -> Option<Rect>
Outsets the rectangle by the specified offset.
Sourcepub fn transform(&self, ts: Transform) -> Option<Rect>
pub fn transform(&self, ts: Transform) -> Option<Rect>
Transforms the rect using the provided Transform
.
This method is expensive.
Sourcepub fn bbox_transform(&self, bbox: NonZeroRect) -> Rect
pub fn bbox_transform(&self, bbox: NonZeroRect) -> Rect
Applies a bounding box transform.
Sourcepub fn to_non_zero_rect(&self) -> Option<NonZeroRect>
pub fn to_non_zero_rect(&self) -> Option<NonZeroRect>
Converts into NonZeroRect
.