#[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
impl Rectangle
Sourcepub fn new(vertices: &[f32; 20]) -> Rectangle
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
2f32s (x, y) - a color made up of
3f32s (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);
}Sourcepub fn new_solid(vertices: &[f32; 8], color: &Color) -> Rectangle
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
2f32s (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);
}Sourcepub fn new_flat(
x: f32,
y: f32,
width: f32,
height: f32,
color: &Color,
) -> Rectangle
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:
- (
x,y) - (
x+base,y) - (
x+base,y+height) - (
x,y+height)
§Parameters
x: f32- the mininum (furthest left) X coordinate on the rectangley: f32- the minimum (furhest down) Y coordinate on the rectanglewidth: 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);
}Sourcepub fn draw(&self, shader_program: &ShaderProgram)
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);
}