Skip to main content

Rectangle

Struct Rectangle 

Source
#[non_exhaustive]
pub struct Rectangle { /* private fields */ }
Expand description

The Rectangle struct represents any 3 points in the 2d plane. Each point (vertex) is made up of:

  • 2 position components (x, y)
  • 3 color components (r, g, b)

There are many different new methods for a Rectangle, each providing an easy way to create a certain type of rectangle. For examples, please see the different functions that Rectangle implements.

Implementations§

Source§

impl Rectangle

Source

pub fn new(vertices: &[f32; 20]) -> Rectangle

Create a new Rectangle from a list of 15 f32s. The list is a set of 3 vertices, each containing two components:

  • a position made up of 2 f32s (x, y)
  • a color made up of 3 f32s (r, g, b)
§Example usage
let shader = shader_2d();
let rectangle = Rectangle::new(&[
    -0.5, -0.5, 1.0, 0.0, 0.0,
     0.5, -0.5, 0.0, 1.0, 0.0,
     0.5,  0.5, 1.0, 1.0, 1.0,
    -0.5,  0.5, 0.0, 0.0, 1.0,
]);
while w.is_running() {
    ...
    rectangle.draw(&shader);
}
Source

pub fn new_solid(vertices: &[f32; 8], color: &Color) -> Rectangle

Create a new Rectangle from a list of 8 f32s and a color for the entire rectangle. The list is a set of 4 vertices, each containing one component:

  • a position made up of 2 f32s (x, y)

As the name implies, this function will create a new rectangle with a single, solid color for the entire rectangle. If you need to interpolate (blend) between different colors and have different colors for each vertex, use the Rectangle::new function.

§Example usage
let shader = shader_2d();
let rectangle = Rectangle::new_solid(&[
    -0.5, -0.5,
     0.5, -0.5,
     0.5,  0.5,
    -0.5,  0.5,
], &Color::rgb(255, 127, 31));
while w.is_running() {
    ...
    rectangle.draw(&shader);
}
Source

pub fn new_flat( x: f32, y: f32, width: f32, height: f32, color: &Color, ) -> Rectangle

Create a new Rectangle from an x position, y position, width and height.

The coordinates for the rectangle are calculated like this:

  1. (x, y)
  2. (x+base, y)
  3. (x+base, y+height)
  4. (x, y+height)
§Parameters
  • x: f32 - the mininum (furthest left) X coordinate on the rectangle
  • y: f32 - the minimum (furhest down) Y coordinate on the rectangle
  • width: f32 - the width of the rectangle (the difference in X position between the furthest right point and the furthest left point)
  • height: f32 - the height of the rectangle (the difference in Y position between the highest point and the lowest point)
§Example usage
let shader = shader_2d();
let rectangle = Rectangle::new_flat(
    -0.5, -0.5, 1.0, 1.0,
    Color::new(63, 191, 91)
);
while w.is_running() {
    ...
    rectangle.draw(&shader);
}
Source

pub fn draw(&self, shader_program: &ShaderProgram)

Draw the rectangle to the screen. You must pass in a reference to the shader program returned by the shader_2d() function, or a compatible shader program, or else this method will fail silently.

This method currently simply calls the draw method of the vertex_buffer field.

§Example usage
let shader = shader_2d();
let rectangle = Rectangle::new_solid(&[
    -0.5, -0.5,
     0.5, -0.5,
     0.5,  0.5,
    -0.5,  0.5,
], &Color::rgb(255, 127, 31));
while w.is_running() {
    ...
    rectangle.draw(&shader);
}

Auto Trait Implementations§

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> 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, 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.