Skip to main content

Rect

Struct Rect 

Source
pub struct Rect { /* private fields */ }
Expand description

Utilies for working with Image type. As with other APIs, the (0, 0) position is left top of the screen, then it grows downward and rightward.

Implementations§

Source§

impl Rect

Source

pub fn new(x: i32, y: i32, w: u32, h: u32) -> Rect

Examples found in repository?
examples/image_bench.rs (line 66)
22fn main() {
23    //let (width, height) = orbclient::get_display_size().unwrap();
24
25    let mut window = Window::new(10, 10, 800, 600, "IMAGE BENCHMARK").unwrap();
26
27    window.set(Color::rgb(255, 255, 255));
28
29    //create image data : a green square
30    let data = vec![Color::rgba(100, 200, 10, 3); 412500];
31    let mut data2 = vec![Color::rgba(200, 100, 10, 3); 412500];
32    let mut data3 = vec![Color::rgba(10, 100, 100, 3); 412500];
33    let mut data4 = vec![Color::rgba(10, 100, 200, 3); 800 * 400];
34
35    //draw image benchmarking
36    println!("Benchmarking implementations to draw an image on window:");
37
38    time!("image_legacy", {
39        for _i in 0..TIMES {
40            window.image_legacy(15, 15, 750, 550, &data[..]);
41        }
42    });
43
44    time!("image_over", {
45        for _i in 0..TIMES {
46            window.image_over(50, &data4[..]);
47        }
48    });
49
50    time!("image_fast", {
51        for _i in 0..TIMES {
52            window.image_fast(20, 20, 750, 550, &data2[..]);
53        }
54    });
55
56    time!("image_opaque", {
57        for _i in 0..TIMES {
58            window.image_opaque(30, 30, 750, 550, &data3[..]);
59        }
60    });
61
62    time!("image_roi_mut_blend", {
63        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
64        for _i in 0..TIMES {
65            ImageRef::from_renderer(&mut window)
66                .roi_mut(&Rect::new(40, 40, 750, 550))
67                .blend(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
68        }
69    });
70
71    time!("image_roi_mut_blit_mask", {
72        let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
73        for _i in 0..TIMES {
74            ImageRef::from_renderer(&mut window)
75                .roi_mut(&Rect::new(40, 40, 750, 550))
76                .blit_mask(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
77        }
78    });
79
80    time!("image_roi_mut_blit", {
81        let data3_roi = ImageRef::from_data(750, 550, &mut data3[..]);
82        for _i in 0..TIMES {
83            ImageRef::from_renderer(&mut window)
84                .roi_mut(&Rect::new(50, 50, 750, 550))
85                .blit(&data3_roi.roi(&Rect::new(0, 0, 750, 550)));
86        }
87    });
88
89    time!("image_roi_mut_blit_over", {
90        let data4_roi = ImageRef::from_data(800, 400, &mut data4[..]);
91        for _i in 0..TIMES {
92            ImageRef::from_renderer(&mut window)
93                // .roi_mut(&Rect::new(10, 120, 790, 400)) // to test blit_over does not trigger
94                .roi_mut(&Rect::new(0, 120, 800, 400))
95                .blit(&data4_roi.roi(&Rect::new(0, 0, 800, 400)));
96        }
97    });
98
99    println!("------------------------------------------------");
100
101    window.sync();
102
103    'events: loop {
104        for event in window.events() {
105            match event.to_option() {
106                EventOption::Quit(_quit_event) => break 'events,
107                EventOption::Mouse(evt) => println!(
108                    "At position {:?} pixel color is : {:?}",
109                    (evt.x, evt.y),
110                    window.getpixel(evt.x, evt.y)
111                ),
112                event_option => println!("{:?}", event_option),
113            }
114        }
115    }
116}
Source

pub fn area(&self) -> usize

Return (width * height) as usize, which you can use it as a buffer length in an array.

Source

pub fn left(&self) -> i32

Source

pub fn right(&self) -> i32

Source

pub fn top(&self) -> i32

Source

pub fn bottom(&self) -> i32

Source

pub fn width(&self) -> u32

Source

pub fn height(&self) -> u32

Source

pub fn iwidth(&self) -> i32

A convenient type for width() with i32 for rect calculations. Consider to use other utilities functions if it suitable.

Source

pub fn iheight(&self) -> i32

A convenient type for height() with i32 for rect calculations. Consider to use other utilities functions if it suitable.

Source

pub fn container(&self, other: &Rect) -> Rect

Create a union between two Rectangles. If the two does not overlap, the returned Rect will be larger than two rectangle areas.

Source

pub fn contains(&self, x: i32, y: i32) -> bool

Check if a point is in or at the edge of Rectangle. To check if two Rectangle overlaps, use !a.intersection(b).is_empty().

Source

pub fn is_empty(&self) -> bool

Check if this rectangle is not empty.

Source

pub fn intersection(&self, other: &Rect) -> Rect

Create an intersection between two Rectangles. If the two does not overlap, the returned Rect will be empty.

Source

pub fn translate(self, x: i32, y: i32) -> Rect

Return a new Rect with new moved position

Source

pub fn resize(self, w: u32, h: u32, align: RectAlignment) -> Rect

Return a new Rect with new size. The position will be adjusted depending to alignment. If you want to leave the position unchanged, use RectAlignment::TopLeft

Source

pub fn edge(self, inset: u32, outset: u32, align: RectEdge) -> Rect

Return a new Rect with new width or height depending on alignment edge. The new width or height is calculated as (inset + outset), the difference between the two is whether you want the rectangle grows inward or outward.

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 Copy for Rect

Source§

impl Debug for Rect

Source§

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

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

impl Default for Rect

Source§

fn default() -> Rect

Returns the “default value” for a type. Read more

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.